Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

Changeset 1704

Show
Ignore:
Timestamp:
08/22/07 00:50:17
Author:
fumanchu
Message:

3.0.2 bugfix: responses were being gzipped twice when served from cache. Needs port to trunk eventually.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/cherrypy-3.0.x/cherrypy/lib/encoding.py

    r1427 r1704  
    198198    yield struct.pack("<L", size & 0xFFFFFFFFL) 
    199199 
     200def decompress(body): 
     201    import gzip, StringIO 
     202     
     203    zbuf = StringIO.StringIO() 
     204    zbuf.write(body) 
     205    zbuf.seek(0) 
     206    zfile = gzip.GzipFile(mode='rb', fileobj=zbuf) 
     207    data = zfile.read() 
     208    zfile.close() 
     209    return data 
     210 
     211 
    200212def gzip(compress_level=9, mime_types=['text/html', 'text/plain']): 
    201213    response = cherrypy.response 
    202214    if not response.body: 
    203215        # Response body is empty (might be a 304 for instance) 
     216        return 
     217     
     218    # If returning cached content (which should already have been gzipped), 
     219    # don't re-zip. 
     220    if getattr(cherrypy.request, "cached", False): 
    204221        return 
    205222     
  • branches/cherrypy-3.0.x/cherrypy/test/test_caching.py

    r1703 r1704  
    9696        # The previous request should have invalidated the cache, 
    9797        # so this request will recalc the response. 
    98         zbuf = StringIO.StringIO() 
    99         zfile = gzip.GzipFile(mode='wb', fileobj=zbuf, compresslevel=9) 
    100         zfile.write("visit #5") 
    101         zfile.close() 
    102          
    10398        self.getPage("/", method="GET", headers=[('Accept-Encoding', 'gzip')]) 
    10499        self.assertHeader('Content-Encoding', 'gzip') 
    105         self.assertInBody(zbuf.getvalue()[:3]
     100        self.assertEqual(cherrypy.lib.encoding.decompress(self.body), "visit #5"
    106101         
    107         # Now check that a second request gets the gzip header and gzipped body 
     102        # Now check that a second request gets the gzip header and gzipped body. 
     103        # This also tests a bug in 3.0 to 3.0.2 whereby the cached, gzipped 
     104        # response body was being gzipped a second time. 
    108105        self.getPage("/", method="GET", headers=[('Accept-Encoding', 'gzip')]) 
    109106        self.assertHeader('Content-Encoding', 'gzip') 
    110         self.assertInBody(zbuf.getvalue()[:3]
     107        self.assertEqual(cherrypy.lib.encoding.decompress(self.body), "visit #5"
    111108         
    112109        # Now check that a third request that doesn't accept gzip 
    113         # gets another hit
     110        # skips the cache (because the 'Vary' header denies it)
    114111        self.getPage("/", method="GET") 
    115112        self.assertNoHeader('Content-Encoding') 

Hosted by WebFaction

Log in as guest/cpguest to create tickets