diff options
Diffstat (limited to 'module/web/middlewares.py')
-rw-r--r-- | module/web/middlewares.py | 27 |
1 files changed, 14 insertions, 13 deletions
diff --git a/module/web/middlewares.py b/module/web/middlewares.py index e0e6c3102..ae0911cc3 100644 --- a/module/web/middlewares.py +++ b/module/web/middlewares.py @@ -1,7 +1,11 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -import gzip +# gzip is optional on some platform +try: + import gzip +except ImportError: + gzip = None try: from cStringIO import StringIO @@ -31,9 +35,6 @@ class PrefixMiddleware(object): # (c) 2005 Ian Bicking and contributors; written for Paste (http://pythonpaste.org) # Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php -# (c) 2005 Ian Bicking and contributors; written for Paste (http://pythonpaste.org) -# Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php - # WSGI middleware # Gzip-encodes the response. @@ -86,18 +87,18 @@ class GzipResponse(object): ct = header_value(headers,'content-type') ce = header_value(headers,'content-encoding') cl = header_value(headers, 'content-length') - if cl: - cl = int(cl) - else: - cl = 201 - self.compressible = False - if ct and (ct.startswith('text/') or ct.startswith('application/')) \ - and 'zip' not in ct and cl > 200: - self.compressible = True + + # don't compress on unknown size, it may be too huge + cl = int(cl) if cl else 0 + if ce: self.compressible = False - if self.compressible: + elif gzip is not None and ct and (ct.startswith('text/') or ct.startswith('application/')) \ + and 'zip' not in ct and 200 < cl < 1024*1024: + self.compressible = True headers.append(('content-encoding', 'gzip')) + headers.append(('vary', 'Accept-Encoding')) + remove_header(headers, 'content-length') self.headers = headers self.status = status |