1
0
mirror of https://github.com/django/django.git synced 2025-10-25 14:46:09 +00:00

Fixed #10762, #17514 -- Prevented the GZip middleware from returning a response longer than the original content, allowed compression of non-200 responses, and added tests (there were none). Thanks cannona for the initial patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17365 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Aymeric Augustin
2012-01-09 21:42:03 +00:00
parent 62766f4248
commit 4288c8831b
3 changed files with 109 additions and 10 deletions

View File

@@ -12,8 +12,8 @@ class GZipMiddleware(object):
on the Accept-Encoding header.
"""
def process_response(self, request, response):
# It's not worth compressing non-OK or really short responses.
if response.status_code != 200 or len(response.content) < 200:
# It's not worth attempting to compress really short responses.
if len(response.content) < 200:
return response
patch_vary_headers(response, ('Accept-Encoding',))
@@ -32,7 +32,12 @@ class GZipMiddleware(object):
if not re_accepts_gzip.search(ae):
return response
response.content = compress_string(response.content)
# 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['Content-Encoding'] = 'gzip'
response['Content-Length'] = str(len(response.content))
return response