Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #16035 -- Appended the Etag response header if the GZipMiddlewa…

…re is in use to follow RFC2616 better. Thanks, ext and dracos2.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17471 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit b926765a7c04e88a85f2360a238efde9ffe98244 1 parent 4b71c99
@jezdez jezdez authored
View
3  django/middleware/gzip.py
@@ -37,6 +37,9 @@ def process_response(self, request, response):
if len(compressed_content) >= len(response.content):
return response
+ if response.has_header('ETag'):
+ response['ETag'] = re.sub('"$', ';gzip"', response['ETag'])
@pierrei
pierrei added a note

I ran into problems with this when using both the gzip_page and etag decorators. The etag decorator uses a user-defined function that calculates the etag (check here) that always will return the etag without the trailing ";gzip". The comparison (made here) will never match because of this. Is this intended behavior? How do I make the gzip and etag decorators work together?

@dracos
dracos added a note

Yes, there are issues in this area; this is ticket #19705.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+
response.content = compressed_content
response['Content-Encoding'] = 'gzip'
response['Content-Length'] = str(len(response.content))
View
33 tests/regressiontests/middleware/tests.py
@@ -13,8 +13,8 @@
from django.middleware.common import CommonMiddleware
from django.middleware.http import ConditionalGetMiddleware
from django.middleware.gzip import GZipMiddleware
-from django.test import TestCase
-
+from django.test import TestCase, RequestFactory
+from django.test.utils import override_settings
class CommonMiddlewareTest(TestCase):
def setUp(self):
@@ -582,3 +582,32 @@ def test_no_compress_uncompressible_response(self):
r = GZipMiddleware().process_response(self.req, self.resp)
self.assertEqual(r.content, self.uncompressible_string)
self.assertEqual(r.get('Content-Encoding'), None)
+
+
+@override_settings(USE_ETAGS=True)
+class ETagGZipMiddlewareTest(TestCase):
+ """
+ Tests if the ETag middleware behaves correctly with GZip middleware.
+ """
+ compressible_string = 'a' * 500
+
+ def setUp(self):
+ self.rf = RequestFactory()
+
+ def test_compress_response(self):
+ """
+ Tests that ETag is changed after gzip compression is performed.
+ """
+ request = self.rf.get('/', HTTP_ACCEPT_ENCODING='gzip, deflate')
+ response = GZipMiddleware().process_response(request,
+ CommonMiddleware().process_response(request,
+ HttpResponse(self.compressible_string)))
+ gzip_etag = response.get('ETag')
+
+ request = self.rf.get('/', HTTP_ACCEPT_ENCODING='')
+ response = GZipMiddleware().process_response(request,
+ CommonMiddleware().process_response(request,
+ HttpResponse(self.compressible_string)))
+ nogzip_etag = response.get('ETag')
+
+ self.assertNotEqual(gzip_etag, nogzip_etag)
Please sign in to comment.
Something went wrong with that request. Please try again.