diff options
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, |