summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
Diffstat (limited to 'django')
-rw-r--r--django/conf/global_settings.py2
-rw-r--r--django/middleware/common.py13
-rw-r--r--django/middleware/http.py17
-rw-r--r--django/utils/cache.py9
4 files changed, 37 insertions, 4 deletions
diff --git a/django/conf/global_settings.py b/django/conf/global_settings.py
index 9dc4cdbade..9dbbdc9f88 100644
--- a/django/conf/global_settings.py
+++ b/django/conf/global_settings.py
@@ -23,6 +23,8 @@ DEBUG = False
DEBUG_PROPAGATE_EXCEPTIONS = False
# Whether to use the "ETag" header. This saves bandwidth but slows down performance.
+# Deprecated (RemovedInDjango21Warning) in favor of ConditionalGetMiddleware
+# which sets the ETag regardless of this setting.
USE_ETAGS = False
# People who get code error notifications.
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)
diff --git a/django/utils/cache.py b/django/utils/cache.py
index e74bfbea5d..8d0ce4dac5 100644
--- a/django/utils/cache.py
+++ b/django/utils/cache.py
@@ -22,10 +22,12 @@ import hashlib
import logging
import re
import time
+import warnings
from django.conf import settings
from django.core.cache import caches
from django.http import HttpResponse, HttpResponseNotModified
+from django.utils.deprecation import RemovedInDjango21Warning
from django.utils.encoding import force_bytes, force_text, iri_to_uri
from django.utils.http import (
http_date, parse_etags, parse_http_date_safe, quote_etag,
@@ -242,6 +244,13 @@ def patch_response_headers(response, cache_timeout=None):
if cache_timeout < 0:
cache_timeout = 0 # Can't have max-age negative
if settings.USE_ETAGS and not response.has_header('ETag'):
+ warnings.warn(
+ "The USE_ETAGS setting is deprecated in favor of "
+ "ConditionalGetMiddleware which sets the ETag regardless of the "
+ "setting. patch_response_headers() won't do ETag processing in "
+ "Django 2.1.",
+ RemovedInDjango21Warning
+ )
if hasattr(response, 'render') and callable(response.render):
response.add_post_render_callback(set_response_etag)
else: