summaryrefslogtreecommitdiff
path: root/django/middleware/http.py
diff options
context:
space:
mode:
authorDenis Cornehl <syphar@fastmail.fm>2016-04-03 12:15:10 +0200
committerTim Graham <timograham@gmail.com>2016-10-10 14:55:59 -0400
commita840710e1e38bc9e55412bb36eca92eff94ebd2c (patch)
treeb6ab8b8456eea42645949cb46114fbab50aae0fa /django/middleware/http.py
parent46a3d7604e7fecde8df02ec363200ec5e0ce894e (diff)
Fixed #26447 -- Deprecated settings.USE_ETAGS in favor of ConditionalGetMiddleware.
Diffstat (limited to 'django/middleware/http.py')
-rw-r--r--django/middleware/http.py17
1 files changed, 15 insertions, 2 deletions
diff --git a/django/middleware/http.py b/django/middleware/http.py
index 83c0f866e4..2f9186f82c 100644
--- a/django/middleware/http.py
+++ b/django/middleware/http.py
@@ -1,4 +1,6 @@
-from django.utils.cache import get_conditional_response
+from django.utils.cache import (
+ cc_delim_re, get_conditional_response, set_response_etag,
+)
from django.utils.deprecation import MiddlewareMixin
from django.utils.http import http_date, parse_http_date_safe
@@ -7,7 +9,8 @@ class ConditionalGetMiddleware(MiddlewareMixin):
"""
Handles conditional GET operations. If the response has an ETag or
Last-Modified header, and the request has If-None-Match or
- If-Modified-Since, the response is replaced by an HttpNotModified.
+ If-Modified-Since, the response is replaced by an HttpNotModified. An ETag
+ header is added if needed.
Also sets the Date and Content-Length response-headers.
"""
@@ -16,6 +19,9 @@ class ConditionalGetMiddleware(MiddlewareMixin):
if not response.streaming and not response.has_header('Content-Length'):
response['Content-Length'] = str(len(response.content))
+ if self.needs_etag(response) and not response.has_header('ETag'):
+ set_response_etag(response)
+
etag = response.get('ETag')
last_modified = response.get('Last-Modified')
if last_modified:
@@ -30,3 +36,10 @@ class ConditionalGetMiddleware(MiddlewareMixin):
)
return response
+
+ 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)