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

Changeset 1797

Show
Ignore:
Timestamp:
10/28/07 13:33:34
Author:
fumanchu
Message:

Forward port to trunk from 3.0.x [1704]. Responses were being gzipped twice when served from cache.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/cherrypy/lib/encoding.py

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

    r1796 r1797  
    106106        # The previous request should have invalidated the cache, 
    107107        # so this request will recalc the response. 
    108         zbuf = StringIO.StringIO() 
    109         zfile = gzip.GzipFile(mode='wb', fileobj=zbuf, compresslevel=9) 
    110         zfile.write("visit #5") 
    111         zfile.close() 
    112          
    113108        self.getPage("/", method="GET", headers=[('Accept-Encoding', 'gzip')]) 
    114109        self.assertHeader('Content-Encoding', 'gzip') 
    115         self.assertInBody(zbuf.getvalue()[:3]
     110        self.assertEqual(cherrypy.lib.encoding.decompress(self.body), "visit #5"
    116111         
    117112        # Now check that a second request gets the gzip header and gzipped body 
     113        # This also tests a bug in 3.0 to 3.0.2 whereby the cached, gzipped 
     114        # response body was being gzipped a second time. 
    118115        self.getPage("/", method="GET", headers=[('Accept-Encoding', 'gzip')]) 
    119116        self.assertHeader('Content-Encoding', 'gzip') 
    120         self.assertInBody(zbuf.getvalue()[:3]
     117        self.assertEqual(cherrypy.lib.encoding.decompress(self.body), "visit #5"
    121118         
    122119        # Now check that a third request that doesn't accept gzip 
    123         # gets another hit
     120        # skips the cache (because the 'Vary' header denies it)
    124121        self.getPage("/", method="GET") 
    125122        self.assertNoHeader('Content-Encoding') 

Hosted by WebFaction

Log in as guest/cpguest to create tickets