2016-04-03 18:15:10 +08:00
|
|
|
from django.utils.cache import (
|
|
|
|
cc_delim_re, get_conditional_response, set_response_etag,
|
|
|
|
)
|
2015-11-07 23:12:37 +08:00
|
|
|
from django.utils.deprecation import MiddlewareMixin
|
2016-10-14 20:02:19 +08:00
|
|
|
from django.utils.http import parse_http_date_safe
|
2005-10-09 08:55:08 +08:00
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2015-11-07 23:12:37 +08:00
|
|
|
class ConditionalGetMiddleware(MiddlewareMixin):
|
2005-10-09 08:55:08 +08:00
|
|
|
"""
|
2014-05-29 08:39:14 +08:00
|
|
|
Handles conditional GET operations. If the response has an ETag or
|
2005-10-09 08:55:08 +08:00
|
|
|
Last-Modified header, and the request has If-None-Match or
|
2016-04-03 18:15:10 +08:00
|
|
|
If-Modified-Since, the response is replaced by an HttpNotModified. An ETag
|
|
|
|
header is added if needed.
|
2005-10-09 08:55:08 +08:00
|
|
|
"""
|
|
|
|
def process_response(self, request, response):
|
2016-10-13 12:31:48 +08:00
|
|
|
# It's too late to prevent an unsafe request with a 412 response, and
|
|
|
|
# for a HEAD request, the response body is always empty so computing
|
|
|
|
# an accurate ETag isn't possible.
|
|
|
|
if request.method != 'GET':
|
|
|
|
return response
|
|
|
|
|
2016-04-03 18:15:10 +08:00
|
|
|
if self.needs_etag(response) and not response.has_header('ETag'):
|
|
|
|
set_response_etag(response)
|
|
|
|
|
2015-06-05 21:26:48 +08:00
|
|
|
etag = response.get('ETag')
|
|
|
|
last_modified = response.get('Last-Modified')
|
|
|
|
if last_modified:
|
|
|
|
last_modified = parse_http_date_safe(last_modified)
|
2005-10-09 08:55:08 +08:00
|
|
|
|
2015-06-05 21:26:48 +08:00
|
|
|
if etag or last_modified:
|
|
|
|
return get_conditional_response(
|
|
|
|
request,
|
2016-09-01 21:32:20 +08:00
|
|
|
etag=etag,
|
2015-06-05 21:26:48 +08:00
|
|
|
last_modified=last_modified,
|
|
|
|
response=response,
|
|
|
|
)
|
2005-10-09 08:55:08 +08:00
|
|
|
|
|
|
|
return response
|
2016-04-03 18:15:10 +08:00
|
|
|
|
|
|
|
def needs_etag(self, response):
|
|
|
|
"""
|
|
|
|
Return True if an ETag header should be added to response.
|
|
|
|
"""
|
|
|
|
cache_control_headers = cc_delim_re.split(response.get('Cache-Control', ''))
|
|
|
|
return all(header.lower() != 'no-store' for header in cache_control_headers)
|