Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 5 additions & 13 deletions django/middleware/gzip.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.utils.cache import patch_vary_headers
from django.utils.deprecation import MiddlewareMixin
from django.utils.regex_helper import _lazy_re_compile
from django.utils.text import compress_sequence, compress_string
from django.utils.text import acompress_sequence, compress_sequence, compress_string

re_accepts_gzip = _lazy_re_compile(r"\bgzip\b")

Expand Down Expand Up @@ -32,18 +32,10 @@ def process_response(self, request, response):

if response.streaming:
if response.is_async:
# pull to lexical scope to capture fixed reference in case
# streaming_content is set again later.
original_iterator = response.streaming_content

async def gzip_wrapper():
async for chunk in original_iterator:
yield compress_string(
chunk,
max_random_bytes=self.max_random_bytes,
)

response.streaming_content = gzip_wrapper()
response.streaming_content = acompress_sequence(
response.streaming_content,
max_random_bytes=self.max_random_bytes,
)
else:
response.streaming_content = compress_sequence(
response.streaming_content,
Expand Down
16 changes: 16 additions & 0 deletions django/utils/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,22 @@ def compress_sequence(sequence, *, max_random_bytes=None):
yield buf.read()


async def acompress_sequence(sequence, *, max_random_bytes=None):
buf = StreamingBuffer()
filename = _get_random_filename(max_random_bytes) if max_random_bytes else None
with GzipFile(
filename=filename, mode="wb", compresslevel=6, fileobj=buf, mtime=0
) as zfile:
# Output headers...
yield buf.read()
async for item in sequence:
zfile.write(item)
data = buf.read()
if data:
yield data
yield buf.read()


# Expression to match some_token and some_token="with spaces" (and similarly
# for single-quoted strings).
smart_split_re = _lazy_re_compile(
Expand Down
5 changes: 3 additions & 2 deletions tests/middleware/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import random
import re
import struct
import zlib
from io import BytesIO
from unittest import mock
from urllib.parse import quote
Expand Down Expand Up @@ -880,8 +881,8 @@ def get_response(self, request):

@staticmethod
def decompress(gzipped_string):
with gzip.GzipFile(mode="rb", fileobj=BytesIO(gzipped_string)) as f:
return f.read()
# Use zlib to ensure gzipped_string contains exactly one gzip stream.
return zlib.decompress(gzipped_string, zlib.MAX_WBITS | 16)

@staticmethod
def get_mtime(gzipped_string):
Expand Down
Loading