diff options
| author | Denis Cornehl <syphar@fastmail.fm> | 2016-04-03 12:15:10 +0200 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2016-10-10 14:55:59 -0400 |
| commit | a840710e1e38bc9e55412bb36eca92eff94ebd2c (patch) | |
| tree | b6ab8b8456eea42645949cb46114fbab50aae0fa /django/middleware | |
| parent | 46a3d7604e7fecde8df02ec363200ec5e0ce894e (diff) | |
Fixed #26447 -- Deprecated settings.USE_ETAGS in favor of ConditionalGetMiddleware.
Diffstat (limited to 'django/middleware')
| -rw-r--r-- | django/middleware/common.py | 13 | ||||
| -rw-r--r-- | django/middleware/http.py | 17 |
2 files changed, 26 insertions, 4 deletions
diff --git a/django/middleware/common.py b/django/middleware/common.py index dc89bcb9fa..820609e3a5 100644 --- a/django/middleware/common.py +++ b/django/middleware/common.py @@ -1,4 +1,5 @@ import re +import warnings from django import http from django.conf import settings @@ -8,7 +9,7 @@ from django.urls import is_valid_path from django.utils.cache import ( cc_delim_re, get_conditional_response, set_response_etag, ) -from django.utils.deprecation import MiddlewareMixin +from django.utils.deprecation import MiddlewareMixin, RemovedInDjango21Warning from django.utils.encoding import force_text from django.utils.six.moves.urllib.parse import urlparse @@ -34,7 +35,8 @@ class CommonMiddleware(MiddlewareMixin): - ETags: If the USE_ETAGS setting is set, ETags will be calculated from the entire page content and Not Modified responses will be returned - appropriately. + appropriately. USE_ETAGS is deprecated in favor of + ConditionalGetMiddleware. """ response_redirect_class = http.HttpResponsePermanentRedirect @@ -115,6 +117,13 @@ class CommonMiddleware(MiddlewareMixin): return self.response_redirect_class(self.get_full_path_with_slash(request)) if settings.USE_ETAGS and self.needs_etag(response): + warnings.warn( + "The USE_ETAGS setting is deprecated in favor of " + "ConditionalGetMiddleware which sets the ETag regardless of " + "the setting. CommonMiddleware won't do ETag processing in " + "Django 2.1.", + RemovedInDjango21Warning + ) if not response.has_header('ETag'): set_response_etag(response) 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) |
