| 38 | # Gzip-encodes the response. |
| 39 | |
| 40 | class GZipMiddleWare(object): |
| 41 | |
| 42 | def __init__(self, application, compress_level=6): |
| 43 | self.application = application |
| 44 | self.compress_level = int(compress_level) |
| 45 | |
| 46 | def __call__(self, environ, start_response): |
| 47 | if 'gzip' not in environ.get('HTTP_ACCEPT_ENCODING', ''): |
| 48 | # nothing for us to do, so this middleware will |
| 49 | # be a no-op: |
| 50 | return self.application(environ, start_response) |
| 51 | response = GzipResponse(start_response, self.compress_level) |
| 52 | app_iter = self.application(environ, |
| 53 | response.gzip_start_response) |
| 54 | if app_iter is not None: |
| 55 | response.finish_response(app_iter) |
| 56 | |
| 57 | return response.write() |
| 58 | |
| 59 | def header_value(headers, key): |
| 60 | for header, value in headers: |