diff options
author | Robert Brewer <fumanchu@aminus.org> | 2007-10-28 18:33:34 +0000 |
---|---|---|
committer | Robert Brewer <fumanchu@aminus.org> | 2007-10-28 18:33:34 +0000 |
commit | 191f3ed4cce6cd4fe2e53a21c3626ddccce8d1b0 (patch) | |
tree | 374eb597166579c9fbbedaa8df289b444cef7765 /cherrypy/lib/encoding.py | |
parent | a3677089a850e7527e51a8cf5381db89dd04ee9e (diff) | |
download | cherrypy-git-191f3ed4cce6cd4fe2e53a21c3626ddccce8d1b0.tar.gz |
Forward port to trunk from 3.0.x [1704]. Responses were being gzipped twice when served from cache.
Diffstat (limited to 'cherrypy/lib/encoding.py')
-rw-r--r-- | cherrypy/lib/encoding.py | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/cherrypy/lib/encoding.py b/cherrypy/lib/encoding.py index b21c5f88..aaf7cc43 100644 --- a/cherrypy/lib/encoding.py +++ b/cherrypy/lib/encoding.py @@ -196,12 +196,29 @@ def compress(body, compress_level): yield struct.pack("<l", crc) yield struct.pack("<L", size & 0xFFFFFFFFL) +def decompress(body): + import gzip, StringIO + + zbuf = StringIO.StringIO() + zbuf.write(body) + zbuf.seek(0) + zfile = gzip.GzipFile(mode='rb', fileobj=zbuf) + data = zfile.read() + zfile.close() + return data + + def gzip(compress_level=9, mime_types=['text/html', 'text/plain']): response = cherrypy.response if not response.body: # Response body is empty (might be a 304 for instance) return + # If returning cached content (which should already have been gzipped), + # don't re-zip. + if getattr(cherrypy.request, "cached", False): + return + acceptable = cherrypy.request.headers.elements('Accept-Encoding') if not acceptable: # If no Accept-Encoding field is present in a request, |