Ticket #17552: middleware_gzip_patch_with_docs.diff

File middleware_gzip_patch_with_docs.diff, 6.2 KB (added by cannona, 4 years ago)

Patch, includes docs and tests

  • AUTHORS

     
    109109    Chris Cahoon <chris.cahoon@gmail.com>
    110110    Juan Manuel Caicedo <juan.manuel.caicedo@gmail.com>
    111111    Trevor Caira <trevor@caira.com>
     112    Aaron Cannon <cannona@gmail.com>
    112113    Brett Cannon <brett@python.org>
    113114    Ricardo Javier Cárdenes Medina <ricardo.cardenes@gmail.com>
    114115    Jeremy Carbaugh <jcarbaugh@gmail.com>
  • django/middleware/gzip.py

     
    44from django.utils.cache import patch_vary_headers
    55
    66re_accepts_gzip = re.compile(r'\bgzip\b')
     7# Matches Internet Explorer versions 4, 5 and 6
     8re_is_ie_456 = re.compile(r'MSIE [456]')
     9# Matches netscape version 4 but not MSIE, as MSIE masquerades as Netscape
     10re_is_netscape_4 = re.compile(r'^Mozilla/4(?!.*?MSIE)')
    711
    812class GZipMiddleware(object):
    913    """
     
    1620        if len(response.content) < 200:
    1721            return response
    1822
    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.
    2224        if response.has_header('Content-Encoding'):
    2325            return response
    2426
    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 response
    30 
    3127        ae = request.META.get('HTTP_ACCEPT_ENCODING', '')
    3228        if not re_accepts_gzip.search(ae):
    3329            return response
    3430
     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
    3536        # Return the compressed content only if it's actually shorter.
    3637        compressed_content = compress_string(response.content)
    3738        if len(compressed_content) >= len(response.content):
     
    4041        response.content = compressed_content
    4142        response['Content-Encoding'] = 'gzip'
    4243        response['Content-Length'] = str(len(response.content))
     44        patch_vary_headers(response, ('Accept-Encoding',))
    4345        return response
  • docs/ref/middleware.txt

     
    100100``Content-Encoding`` header is already set, or when the browser does not send
    101101an ``Accept-Encoding`` header containing ``gzip``.
    102102
    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.
     103Content will also not be compressed when the browser is Internet Explorer 5 or
     1046, or Netscape 4. This is done because these browsers don't properly handle
     105compressed content, but request it anyway. Fortunately these browsers make up
     106less than 1.5% of the browser market.
    108107
    109108GZip compression can be applied to individual views using the
    110109: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
    
     
    564564        self.assertEqual(r.content, self.compressible_string)
    565565        self.assertEqual(r.get('Content-Encoding'), 'deflate')
    566566
    567     def test_no_compress_ie_js_requests(self):
     567    def test_no_compress_ie5(self):
    568568        """
    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.
    570570        """
    571571        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'
    573572        r = GZipMiddleware().process_response(self.req, self.resp)
    574573        self.assertEqual(r.content, self.compressible_string)
    575574        self.assertEqual(r.get('Content-Encoding'), None)
    576575
     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
    577604    def test_no_compress_uncompressible_response(self):
    578605        """
    579606        Tests that compression isn't performed on responses with uncompressible content.
Back to Top