mirror of
https://github.com/django/django.git
synced 2025-10-24 22:26:08 +00:00
Fixed #26447 -- Deprecated settings.USE_ETAGS in favor of ConditionalGetMiddleware.
This commit is contained in:
committed by
Tim Graham
parent
46a3d7604e
commit
a840710e1e
@@ -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.
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user