2005-10-09 08:55:08 +08:00
|
|
|
"""
|
2005-10-28 09:30:30 +08:00
|
|
|
Decorators for views based on HTTP headers.
|
2005-10-09 08:55:08 +08:00
|
|
|
"""
|
|
|
|
|
2009-03-22 15:58:29 +08:00
|
|
|
from calendar import timegm
|
2011-03-28 10:11:19 +08:00
|
|
|
from functools import wraps
|
2009-03-22 15:58:29 +08:00
|
|
|
|
2010-03-12 21:06:13 +08:00
|
|
|
from django.utils.decorators import decorator_from_middleware, available_attrs
|
2011-03-01 22:28:06 +08:00
|
|
|
from django.utils.http import http_date, parse_http_date_safe, parse_etags, quote_etag
|
2010-10-06 23:02:26 +08:00
|
|
|
from django.utils.log import getLogger
|
2005-10-09 08:55:08 +08:00
|
|
|
from django.middleware.http import ConditionalGetMiddleware
|
2009-03-22 15:58:29 +08:00
|
|
|
from django.http import HttpResponseNotAllowed, HttpResponseNotModified, HttpResponse
|
|
|
|
|
2005-10-09 08:55:08 +08:00
|
|
|
conditional_page = decorator_from_middleware(ConditionalGetMiddleware)
|
2005-10-28 09:30:30 +08:00
|
|
|
|
2010-10-06 23:02:26 +08:00
|
|
|
logger = getLogger('django.request')
|
2010-10-04 23:12:39 +08:00
|
|
|
|
|
|
|
|
2005-10-28 09:30:30 +08:00
|
|
|
def require_http_methods(request_method_list):
|
|
|
|
"""
|
|
|
|
Decorator to make a view only accept particular request methods. Usage::
|
2006-06-20 11:17:57 +08:00
|
|
|
|
2005-10-28 09:30:30 +08:00
|
|
|
@require_http_methods(["GET", "POST"])
|
|
|
|
def my_view(request):
|
|
|
|
# I can assume now that only GET or POST requests make it this far
|
2006-06-20 11:17:57 +08:00
|
|
|
# ...
|
|
|
|
|
2006-06-20 12:34:13 +08:00
|
|
|
Note that request methods should be in uppercase.
|
2005-10-28 09:30:30 +08:00
|
|
|
"""
|
|
|
|
def decorator(func):
|
2011-05-02 00:46:02 +08:00
|
|
|
@wraps(func, assigned=available_attrs(func))
|
2005-10-28 09:30:30 +08:00
|
|
|
def inner(request, *args, **kwargs):
|
2006-06-20 12:34:13 +08:00
|
|
|
if request.method not in request_method_list:
|
2010-10-04 23:12:39 +08:00
|
|
|
logger.warning('Method Not Allowed (%s): %s' % (request.method, request.path),
|
|
|
|
extra={
|
|
|
|
'status_code': 405,
|
|
|
|
'request': request
|
|
|
|
}
|
|
|
|
)
|
2006-06-20 11:17:57 +08:00
|
|
|
return HttpResponseNotAllowed(request_method_list)
|
2005-10-28 09:30:30 +08:00
|
|
|
return func(request, *args, **kwargs)
|
2011-05-02 00:46:02 +08:00
|
|
|
return inner
|
2005-10-28 09:30:30 +08:00
|
|
|
return decorator
|
|
|
|
|
|
|
|
require_GET = require_http_methods(["GET"])
|
|
|
|
require_GET.__doc__ = "Decorator to require that a view only accept the GET method."
|
|
|
|
|
|
|
|
require_POST = require_http_methods(["POST"])
|
2009-03-22 15:58:29 +08:00
|
|
|
require_POST.__doc__ = "Decorator to require that a view only accept the POST method."
|
|
|
|
|
2011-04-28 21:04:16 +08:00
|
|
|
require_safe = require_http_methods(["GET", "HEAD"])
|
|
|
|
require_safe.__doc__ = "Decorator to require that a view only accept safe methods: GET and HEAD."
|
|
|
|
|
2009-03-22 15:58:29 +08:00
|
|
|
def condition(etag_func=None, last_modified_func=None):
|
|
|
|
"""
|
|
|
|
Decorator to support conditional retrieval (or change) for a view
|
|
|
|
function.
|
|
|
|
|
|
|
|
The parameters are callables to compute the ETag and last modified time for
|
|
|
|
the requested resource, respectively. The callables are passed the same
|
|
|
|
parameters as the view itself. The Etag function should return a string (or
|
|
|
|
None if the resource doesn't exist), whilst the last_modified function
|
|
|
|
should return a datetime object (or None if the resource doesn't exist).
|
|
|
|
|
|
|
|
If both parameters are provided, all the preconditions must be met before
|
|
|
|
the view is processed.
|
|
|
|
|
|
|
|
This decorator will either pass control to the wrapped view function or
|
|
|
|
return an HTTP 304 response (unmodified) or 412 response (preconditions
|
|
|
|
failed), depending upon the request method.
|
|
|
|
|
|
|
|
Any behavior marked as "undefined" in the HTTP spec (e.g. If-none-match
|
|
|
|
plus If-modified-since headers) will result in the view function being
|
|
|
|
called.
|
|
|
|
"""
|
|
|
|
def decorator(func):
|
|
|
|
def inner(request, *args, **kwargs):
|
|
|
|
# Get HTTP request headers
|
|
|
|
if_modified_since = request.META.get("HTTP_IF_MODIFIED_SINCE")
|
2011-03-01 22:28:06 +08:00
|
|
|
if if_modified_since:
|
|
|
|
if_modified_since = parse_http_date_safe(if_modified_since)
|
2009-03-22 15:58:29 +08:00
|
|
|
if_none_match = request.META.get("HTTP_IF_NONE_MATCH")
|
|
|
|
if_match = request.META.get("HTTP_IF_MATCH")
|
|
|
|
if if_none_match or if_match:
|
|
|
|
# There can be more than one ETag in the request, so we
|
|
|
|
# consider the list of values.
|
2009-04-03 14:59:34 +08:00
|
|
|
try:
|
|
|
|
etags = parse_etags(if_none_match or if_match)
|
|
|
|
except ValueError:
|
|
|
|
# In case of invalid etag ignore all ETag headers.
|
|
|
|
# Apparently Opera sends invalidly quoted headers at times
|
|
|
|
# (we should be returning a 400 response, but that's a
|
|
|
|
# little extreme) -- this is Django bug #10681.
|
|
|
|
if_none_match = None
|
|
|
|
if_match = None
|
2009-03-22 15:58:29 +08:00
|
|
|
|
|
|
|
# Compute values (if any) for the requested resource.
|
|
|
|
if etag_func:
|
|
|
|
res_etag = etag_func(request, *args, **kwargs)
|
|
|
|
else:
|
|
|
|
res_etag = None
|
|
|
|
if last_modified_func:
|
|
|
|
dt = last_modified_func(request, *args, **kwargs)
|
|
|
|
if dt:
|
2011-03-01 22:28:06 +08:00
|
|
|
res_last_modified = timegm(dt.utctimetuple())
|
2009-03-22 15:58:29 +08:00
|
|
|
else:
|
|
|
|
res_last_modified = None
|
|
|
|
else:
|
|
|
|
res_last_modified = None
|
|
|
|
|
|
|
|
response = None
|
|
|
|
if not ((if_match and (if_modified_since or if_none_match)) or
|
|
|
|
(if_match and if_none_match)):
|
|
|
|
# We only get here if no undefined combinations of headers are
|
|
|
|
# specified.
|
|
|
|
if ((if_none_match and (res_etag in etags or
|
|
|
|
"*" in etags and res_etag)) and
|
|
|
|
(not if_modified_since or
|
2011-03-01 22:28:06 +08:00
|
|
|
(res_last_modified and if_modified_since and
|
|
|
|
res_last_modified <= if_modified_since))):
|
2009-03-22 15:58:29 +08:00
|
|
|
if request.method in ("GET", "HEAD"):
|
|
|
|
response = HttpResponseNotModified()
|
|
|
|
else:
|
2010-10-04 23:12:39 +08:00
|
|
|
logger.warning('Precondition Failed: %s' % request.path,
|
|
|
|
extra={
|
|
|
|
'status_code': 412,
|
|
|
|
'request': request
|
|
|
|
}
|
|
|
|
)
|
2009-03-22 15:58:29 +08:00
|
|
|
response = HttpResponse(status=412)
|
|
|
|
elif if_match and ((not res_etag and "*" in etags) or
|
|
|
|
(res_etag and res_etag not in etags)):
|
2010-10-04 23:12:39 +08:00
|
|
|
logger.warning('Precondition Failed: %s' % request.path,
|
|
|
|
extra={
|
|
|
|
'status_code': 412,
|
|
|
|
'request': request
|
|
|
|
}
|
|
|
|
)
|
2009-03-22 15:58:29 +08:00
|
|
|
response = HttpResponse(status=412)
|
2011-03-01 22:28:06 +08:00
|
|
|
elif (not if_none_match and request.method == "GET" and
|
|
|
|
res_last_modified and if_modified_since and
|
|
|
|
res_last_modified <= if_modified_since):
|
2009-03-22 15:58:29 +08:00
|
|
|
response = HttpResponseNotModified()
|
|
|
|
|
|
|
|
if response is None:
|
|
|
|
response = func(request, *args, **kwargs)
|
|
|
|
|
|
|
|
# Set relevant headers on the response if they don't already exist.
|
|
|
|
if res_last_modified and not response.has_header('Last-Modified'):
|
2011-03-01 22:28:06 +08:00
|
|
|
response['Last-Modified'] = http_date(res_last_modified)
|
2009-03-22 15:58:29 +08:00
|
|
|
if res_etag and not response.has_header('ETag'):
|
|
|
|
response['ETag'] = quote_etag(res_etag)
|
|
|
|
|
|
|
|
return response
|
|
|
|
|
|
|
|
return inner
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
# Shortcut decorators for common cases based on ETag or Last-Modified only
|
2009-03-24 11:01:46 +08:00
|
|
|
def etag(etag_func):
|
|
|
|
return condition(etag_func=etag_func)
|
2009-03-22 15:58:29 +08:00
|
|
|
|
2009-03-24 11:01:46 +08:00
|
|
|
def last_modified(last_modified_func):
|
|
|
|
return condition(last_modified_func=last_modified_func)
|
2009-03-22 15:58:29 +08:00
|
|
|
|