Changeset 1797
- Timestamp:
- 10/28/07 13:33:34
- Files:
-
- trunk/cherrypy/lib/encoding.py (modified) (1 diff)
- trunk/cherrypy/test/test_caching.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/cherrypy/lib/encoding.py
r1762 r1797 197 197 yield struct.pack("<L", size & 0xFFFFFFFFL) 198 198 199 def 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 199 211 def gzip(compress_level=9, mime_types=['text/html', 'text/plain']): 200 212 response = cherrypy.response 201 213 if not response.body: 202 214 # 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): 203 220 return 204 221 trunk/cherrypy/test/test_caching.py
r1796 r1797 106 106 # The previous request should have invalidated the cache, 107 107 # 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 113 108 self.getPage("/", method="GET", headers=[('Accept-Encoding', 'gzip')]) 114 109 self.assertHeader('Content-Encoding', 'gzip') 115 self.assert InBody(zbuf.getvalue()[:3])110 self.assertEqual(cherrypy.lib.encoding.decompress(self.body), "visit #5") 116 111 117 112 # 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. 118 115 self.getPage("/", method="GET", headers=[('Accept-Encoding', 'gzip')]) 119 116 self.assertHeader('Content-Encoding', 'gzip') 120 self.assert InBody(zbuf.getvalue()[:3])117 self.assertEqual(cherrypy.lib.encoding.decompress(self.body), "visit #5") 121 118 122 119 # 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). 124 121 self.getPage("/", method="GET") 125 122 self.assertNoHeader('Content-Encoding')

