1
# $Id: gzipfilter.py,v 1.1 2002/10/04 21:31:05 grisha Exp $
4
# <Directory /where/ever>
5
# PythonOutputFilter gzipfilter
6
# SetOutputFilter gzipfilter
9
from mod_python import apache
15
from mod_python import apache
18
sio = cStringIO.StringIO()
19
f = gzip.GzipFile(mode='wb', fileobj=sio)
24
def accepts_gzip(req):
25
if req.headers_in.has_key('accept-encoding'):
26
encodings = req.headers_in['accept-encoding']
27
return (encodings.find("gzip") != -1)
31
### main filter function
33
def outputfilter(filter):
35
if (filter.req.main or
36
not accepts_gzip(filter.req)):
38
# Presense of filter.req.main tells us that
39
# we are in a subrequest. We don't want to compress
40
# the data more than once, so we pass_on() in
41
# subrequests. We also pass_on() if the client
42
# does not accept gzip encoding, of course.
47
if not filter.req.sent_bodyct:
49
# the above test allows us to set the encoding once
50
# rather than every time the filter is invoked
52
filter.req.headers_out['content-encoding'] = 'gzip'
54
# loop through content, compressing
65
# this means we received an EOS, so we pass it on
66
# by closing the filter