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
|
|
|
"""
|
2017-01-25 04:37:33 +08:00
|
|
|
Handle conditional GET operations. If the response has an ETag or
|
|
|
|
Last-Modified header and the request has If-None-Match or If-Modified-Since,
|
|
|
|
replace the response with HttpNotModified. Add an ETag header 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')
|
2018-01-04 07:52:12 +08:00
|
|
|
last_modified = last_modified and 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):
|
2017-01-25 04:37:33 +08:00
|
|
|
"""Return True if an ETag header should be added to response."""
|
2016-04-03 18:15:10 +08:00
|
|
|
cache_control_headers = cc_delim_re.split(response.get('Cache-Control', ''))
|
|
|
|
return all(header.lower() != 'no-store' for header in cache_control_headers)
|