zlib, gzip and HTTP compression.

Michael Ströder michael at stroeder.com
Sat Jan 12 10:00:46 EST 2002


Alan Kennedy wrote:
> 
> I'm trying to send compressed HTML over a HTTP 1.1 connection, and I
> just cannot get it to work.

Well, it also took me quite a while to figure out the following
issues:
1. Make sure the HTTP header is not compressed including the empty
line separating the header from the body.
2. Make sure the compression module you use does not write any
compression header to the output stream before the HTTP body starts.
The compression header MUST exactly start at the first byte of the
body.
3. Use the right HTTP header lines:
Content-Encoding: gzip
Vary: Accept-Encoding
4. If you really use HTTP 1.1 features with HTTP-Keepalives you have
to set the Content-Length: header to an appropriate value.

BTW http://web2ldap.de has this feature. I'm using a class derived
from gzip.GzipFile in module msgzip as file object for streamed
output. The headers are set by pyweblib.httphelper.SendHeader()
(Note PyWebLib is a separate package. Hmm, I should clean up some
things...).

You might also wanna look into a multi-language package called
cgibuffer or something which inspired my activities in this regard.
Note that it buffers the whole HTTP body before compressing it en
bloc and therefore does not have problems with compression header
being sent somewhere in the stream.

However you will experience that it does not make sense to use that
in all cases and that some browser still chokes on some data (e.g.
Netscape Navigator 4.x when delivering a JPEG image with
Content-Encoding: gzip).

Ciao, Michael.



More information about the Python-list mailing list