Changeset 1704
- Timestamp:
- 08/22/07 00:50:17
- Files:
-
- branches/cherrypy-3.0.x/cherrypy/lib/encoding.py (modified) (1 diff)
- branches/cherrypy-3.0.x/cherrypy/test/test_caching.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/cherrypy-3.0.x/cherrypy/lib/encoding.py
r1427 r1704 198 198 yield struct.pack("<L", size & 0xFFFFFFFFL) 199 199 200 def 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 200 212 def gzip(compress_level=9, mime_types=['text/html', 'text/plain']): 201 213 response = cherrypy.response 202 214 if not response.body: 203 215 # 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): 204 221 return 205 222 branches/cherrypy-3.0.x/cherrypy/test/test_caching.py
r1703 r1704 96 96 # The previous request should have invalidated the cache, 97 97 # 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 103 98 self.getPage("/", method="GET", headers=[('Accept-Encoding', 'gzip')]) 104 99 self.assertHeader('Content-Encoding', 'gzip') 105 self.assert InBody(zbuf.getvalue()[:3])100 self.assertEqual(cherrypy.lib.encoding.decompress(self.body), "visit #5") 106 101 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. 108 105 self.getPage("/", method="GET", headers=[('Accept-Encoding', 'gzip')]) 109 106 self.assertHeader('Content-Encoding', 'gzip') 110 self.assert InBody(zbuf.getvalue()[:3])107 self.assertEqual(cherrypy.lib.encoding.decompress(self.body), "visit #5") 111 108 112 109 # 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). 114 111 self.getPage("/", method="GET") 115 112 self.assertNoHeader('Content-Encoding')

