On Thu, 2002-03-14 at 09:57, Clark C . Evans wrote:
> I know this is an older question, but what do I have to do
> to send back gzippped content?  Here is what I figure:
> 
>   1.  A function needs to be written "supportsCompression"
>       which examines the headers sent to determine if the 
>       user agent accepts compressed content.
> 
>   2.  If the user agent supportsCompression, then before
>       the response is sent, it should be compressed and the
>       mime type should be updated respectively.

The encoding shows whether the content is compressed -- the mime type
isn't touched.

I posted some code before that was tested and worked -- it's simple, but
streaming and configuration weren't included.  Here it is again:



import gzip # standard module
from cStringIO import StringIO

class HTTPResponse(Response):
    #...
    def gzipContents(self):
        if self.hasHeader('Content-encoding'):
            return # I don't believe we can double-encode...?
        if not self.contentTypeCompressable(self.header('Content-type')):
            return
        self.setHeader('Content-encoding', 'gzip')
        compressed = StringIO()
        gzipfile = gzip.GzipFile('', 'wb', 9, compressed) # We are writing to an 
empty-named, binary file, compression level 9
        gzipfile.write(string.join(self._contents, ''))
        gzipfile.close()
        self._contents = [compressed.getvalue())
        compressed.close()
    def contentTypeCompressable(self, type):
        return type[:5] == 'text/'



_______________________________________________
Webware-discuss mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/webware-discuss

Reply via email to