1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
"""
WSGI middleware
Gzip-encodes the response.
"""
import gzip
import wsgilib
class GzipOutput(object):
pass
class middleware(object):
def __init__(self, application, compress_level=5):
self.application = application
self.compress_level = compress_level
def __call__(self, environ, start_response):
if 'gzip' not in environ.get('HTTP_ACCEPT_ENCODING'):
# nothing for us to do, so this middleware will
# be a no-op:
return self.application(environ, start_response)
response = GzipResponse(start_response, self.compress_level)
app_iter = self.application(environ,
response.gzip_start_response)
try:
if app_iter:
response.finish_response(app_iter)
finally:
response.close()
return None
class GzipResponse(object):
def __init__(self, start_response, compress_level):
self.start_response = start_response
self.compress_level = compress_level
self.gzip_fileobj = None
def gzip_start_response(self, status, headers, exc_info=None):
# This isn't part of the spec yet:
if wsgilib.has_header(headers, 'content-encoding'):
# we won't double-encode
return self.start_response(status, headers, exc_info)
headers.append(('content-encoding', 'gzip'))
raw_writer = self.start_response(status, headers, exc_info)
dummy_fileobj = GzipOutput()
dummy_fileobj.write = raw_writer
self.gzip_fileobj = gzip.GzipFile('', 'wb', self.compress_level,
dummy_fileobj)
return self.gzip_fileobj.write
def finish_response(self, app_iter):
try:
for s in app_iter:
self.gzip_fileobj.write(s)
finally:
if hasattr(app_iter, 'close'):
app_iter.close()
def close(self):
self.gzip_fileobj.close()
|