Ticket #17552: middleware_gzip_patch_with_docs.diff
File middleware_gzip_patch_with_docs.diff, 6.2 KB (added by , 13 years ago) |
---|
-
AUTHORS
109 109 Chris Cahoon <chris.cahoon@gmail.com> 110 110 Juan Manuel Caicedo <juan.manuel.caicedo@gmail.com> 111 111 Trevor Caira <trevor@caira.com> 112 Aaron Cannon <cannona@gmail.com> 112 113 Brett Cannon <brett@python.org> 113 114 Ricardo Javier Cárdenes Medina <ricardo.cardenes@gmail.com> 114 115 Jeremy Carbaugh <jcarbaugh@gmail.com> -
django/middleware/gzip.py
4 4 from django.utils.cache import patch_vary_headers 5 5 6 6 re_accepts_gzip = re.compile(r'\bgzip\b') 7 # Matches Internet Explorer versions 4, 5 and 6 8 re_is_ie_456 = re.compile(r'MSIE [456]') 9 # Matches netscape version 4 but not MSIE, as MSIE masquerades as Netscape 10 re_is_netscape_4 = re.compile(r'^Mozilla/4(?!.*?MSIE)') 7 11 8 12 class GZipMiddleware(object): 9 13 """ … … 16 20 if len(response.content) < 200: 17 21 return response 18 22 19 patch_vary_headers(response, ('Accept-Encoding',)) 20 21 # Avoid gzipping if we've already got a content-encoding. 23 # Avoid gzipping if we've already got a content-encoding. 22 24 if response.has_header('Content-Encoding'): 23 25 return response 24 26 25 # MSIE have issues with gzipped response of various content types.26 if "msie" in request.META.get('HTTP_USER_AGENT', '').lower():27 ctype = response.get('Content-Type', '').lower()28 if not ctype.startswith("text/") or "javascript" in ctype:29 return response30 31 27 ae = request.META.get('HTTP_ACCEPT_ENCODING', '') 32 28 if not re_accepts_gzip.search(ae): 33 29 return response 34 30 31 # Some browsers have issues with gzipped responses 32 ua = request.META.get('HTTP_USER_AGENT', '') 33 if re_is_ie_456.search(ua) or re_is_netscape_4.search(ua): 34 return response 35 35 36 # Return the compressed content only if it's actually shorter. 36 37 compressed_content = compress_string(response.content) 37 38 if len(compressed_content) >= len(response.content): … … 40 41 response.content = compressed_content 41 42 response['Content-Encoding'] = 'gzip' 42 43 response['Content-Length'] = str(len(response.content)) 44 patch_vary_headers(response, ('Accept-Encoding',)) 43 45 return response -
docs/ref/middleware.txt
100 100 ``Content-Encoding`` header is already set, or when the browser does not send 101 101 an ``Accept-Encoding`` header containing ``gzip``. 102 102 103 Content will also not be compressed when the browser is Internet Explorer and 104 the ``Content-Type`` header contains ``javascript`` or starts with anything 105 other than ``text/``. This is done to overcome a bug present in early versions 106 of Internet Explorer which caused decompression not to be performed on certain 107 content types. 103 Content will also not be compressed when the browser is Internet Explorer 5 or 104 6, or Netscape 4. This is done because these browsers don't properly handle 105 compressed content, but request it anyway. Fortunately these browsers make up 106 less than 1.5% of the browser market. 108 107 109 108 GZip compression can be applied to individual views using the 110 109 :func:`~django.views.decorators.http.gzip_page()` decorator. -
tests/regressiontests/middleware/tests.py
Property changes on: docs\ref\middleware.txt ___________________________________________________________________ Added: svn:eol-style + native
564 564 self.assertEqual(r.content, self.compressible_string) 565 565 self.assertEqual(r.get('Content-Encoding'), 'deflate') 566 566 567 def test_no_compress_ie _js_requests(self):567 def test_no_compress_ie5(self): 568 568 """ 569 Tests that compression isn't performed on JavaScript requests from Internet Explorer.569 Tests that compression isn't performed on requests from Internet Explorer 5. 570 570 """ 571 571 self.req.META['HTTP_USER_AGENT'] = 'Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)' 572 self.resp['Content-Type'] = 'application/javascript; charset=UTF-8'573 572 r = GZipMiddleware().process_response(self.req, self.resp) 574 573 self.assertEqual(r.content, self.compressible_string) 575 574 self.assertEqual(r.get('Content-Encoding'), None) 576 575 576 def test_no_compress_ie6(self): 577 """ 578 Tests that compression isn't performed on requests from Internet Explorer 6. 579 """ 580 self.req.META['HTTP_USER_AGENT'] = 'Mozilla/4.0 (compatible; MSIE 6.1; Windows XP)' 581 r = GZipMiddleware().process_response(self.req, self.resp) 582 self.assertEqual(r.content, self.compressible_string) 583 self.assertEqual(r.get('Content-Encoding'), None) 584 585 def test_no_compress_netscape4(self): 586 """ 587 Tests that compression isn't performed on requests from Netscape 4. 588 """ 589 self.req.META['HTTP_USER_AGENT'] = 'Mozilla/4.8 [en] (X11; U; Linux 2.6.12-1.1372_FC3 i686; Nav)' 590 r = GZipMiddleware().process_response(self.req, self.resp) 591 self.assertEqual(r.content, self.compressible_string) 592 self.assertEqual(r.get('Content-Encoding'), None) 593 594 def test_compress_IE7(self): 595 """ 596 Tests that compression is performed on requests from Internet Explorer 7. 597 IE7 sometimes masquerades as Netscape 4. 598 """ 599 self.req.META['HTTP_USER_AGENT'] = 'Mozilla /4.0 (compatible;MSIE 7.0;Windows NT6.0)' 600 r = GZipMiddleware().process_response(self.req, self.resp) 601 self.assertEqual(self.decompress(r.content), self.compressible_string) 602 self.assertEqual(r.get('Content-Encoding'), 'gzip') 603 577 604 def test_no_compress_uncompressible_response(self): 578 605 """ 579 606 Tests that compression isn't performed on responses with uncompressible content.