summaryrefslogtreecommitdiff
path: root/django/middleware/gzip.py
diff options
context:
space:
mode:
authordjango-bot <ops@djangoproject.com>2022-02-03 20:24:19 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2022-02-07 20:37:05 +0100
commit9c19aff7c7561e3a82978a272ecdaad40dda5c00 (patch)
treef0506b668a013d0063e5fba3dbf4863b466713ba /django/middleware/gzip.py
parentf68fa8b45dfac545cfc4111d4e52804c86db68d3 (diff)
Refs #33476 -- Reformatted code with Black.
Diffstat (limited to 'django/middleware/gzip.py')
-rw-r--r--django/middleware/gzip.py19
1 files changed, 10 insertions, 9 deletions
diff --git a/django/middleware/gzip.py b/django/middleware/gzip.py
index 350466151d..6d27c1e335 100644
--- a/django/middleware/gzip.py
+++ b/django/middleware/gzip.py
@@ -3,7 +3,7 @@ from django.utils.deprecation import MiddlewareMixin
from django.utils.regex_helper import _lazy_re_compile
from django.utils.text import compress_sequence, compress_string
-re_accepts_gzip = _lazy_re_compile(r'\bgzip\b')
+re_accepts_gzip = _lazy_re_compile(r"\bgzip\b")
class GZipMiddleware(MiddlewareMixin):
@@ -12,18 +12,19 @@ class GZipMiddleware(MiddlewareMixin):
Set the Vary header accordingly, so that caches will base their storage
on the Accept-Encoding header.
"""
+
def process_response(self, request, response):
# It's not worth attempting to compress really short responses.
if not response.streaming and len(response.content) < 200:
return response
# Avoid gzipping if we've already got a content-encoding.
- if response.has_header('Content-Encoding'):
+ if response.has_header("Content-Encoding"):
return response
- patch_vary_headers(response, ('Accept-Encoding',))
+ patch_vary_headers(response, ("Accept-Encoding",))
- ae = request.META.get('HTTP_ACCEPT_ENCODING', '')
+ ae = request.META.get("HTTP_ACCEPT_ENCODING", "")
if not re_accepts_gzip.search(ae):
return response
@@ -31,21 +32,21 @@ class GZipMiddleware(MiddlewareMixin):
# Delete the `Content-Length` header for streaming content, because
# we won't know the compressed size until we stream it.
response.streaming_content = compress_sequence(response.streaming_content)
- del response.headers['Content-Length']
+ del response.headers["Content-Length"]
else:
# Return the compressed content only if it's actually shorter.
compressed_content = compress_string(response.content)
if len(compressed_content) >= len(response.content):
return response
response.content = compressed_content
- response.headers['Content-Length'] = str(len(response.content))
+ response.headers["Content-Length"] = str(len(response.content))
# If there is a strong ETag, make it weak to fulfill the requirements
# of RFC 7232 section-2.1 while also allowing conditional request
# matches on ETags.
- etag = response.get('ETag')
+ etag = response.get("ETag")
if etag and etag.startswith('"'):
- response.headers['ETag'] = 'W/' + etag
- response.headers['Content-Encoding'] = 'gzip'
+ response.headers["ETag"] = "W/" + etag
+ response.headers["Content-Encoding"] = "gzip"
return response