summaryrefslogtreecommitdiff
path: root/django/middleware/http.py
diff options
context:
space:
mode:
authorDenis Cornehl <syphar@fastmail.fm>2015-06-05 14:26:48 +0100
committerTim Graham <timograham@gmail.com>2015-08-15 09:08:45 -0400
commit7a40fef17ab7918cbb1ddc3ba080f42b420f7a48 (patch)
treed5514a1ea0e059da9a9338beb0c1236a05a6b5bd /django/middleware/http.py
parent1f7b25c1a7a85426675a04380f37b180edc08bbc (diff)
Fixed #24935 -- Refactored common conditional GET handling.
Diffstat (limited to 'django/middleware/http.py')
-rw-r--r--django/middleware/http.py34
1 files changed, 12 insertions, 22 deletions
diff --git a/django/middleware/http.py b/django/middleware/http.py
index 60fb194704..7a6f237c96 100644
--- a/django/middleware/http.py
+++ b/django/middleware/http.py
@@ -1,3 +1,4 @@
+from django.utils.cache import get_conditional_response
from django.utils.http import http_date, parse_http_date_safe
@@ -14,28 +15,17 @@ class ConditionalGetMiddleware(object):
if not response.streaming and not response.has_header('Content-Length'):
response['Content-Length'] = str(len(response.content))
- # If-None-Match must be ignored if original result would be anything
- # other than a 2XX or 304 status. 304 status would result in no change.
- # http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.26
- if 200 <= response.status_code < 300 and response.has_header('ETag'):
- if_none_match = request.META.get('HTTP_IF_NONE_MATCH')
- if if_none_match == response['ETag']:
- # Setting the status is enough here. The response handling path
- # automatically removes content for this status code (in
- # http.conditional_content_removal()).
- response.status_code = 304
+ etag = response.get('ETag')
+ last_modified = response.get('Last-Modified')
+ if last_modified:
+ last_modified = parse_http_date_safe(last_modified)
- # If-Modified-Since must be ignored if the original result was not a 200.
- # http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.25
- if response.status_code == 200 and response.has_header('Last-Modified'):
- if_modified_since = request.META.get('HTTP_IF_MODIFIED_SINCE')
- if if_modified_since is not None:
- if_modified_since = parse_http_date_safe(if_modified_since)
- if if_modified_since is not None:
- last_modified = parse_http_date_safe(response['Last-Modified'])
- if last_modified is not None and last_modified <= if_modified_since:
- # Setting the status code is enough here (same reasons as
- # above).
- response.status_code = 304
+ if etag or last_modified:
+ return get_conditional_response(
+ request,
+ etag=etag,
+ last_modified=last_modified,
+ response=response,
+ )
return response