I've been messing around a little with HTTP compression for AS.
Apache has this feature with mod_gzip.
Here's a hacky TCL thing to do it that works fine:
proc rl_returnz {status type string} {
if {[string first gzip [ns_set iget [ns_conn headers] "Accept-Encoding"]] >= 0} {
set file [ns_tmpnam]
set fd [open $file w]
puts -nonewline $fd $string
close $fd
exec gzip $file
ns_set put [ns_conn outputheaders] "Content-Encoding" "gzip"
ns_returnfile $status $type $file.gz
ns_unlink $file.gz
} else {
ns_return $status $type $string
}
}
I have a C module started that uses the gzip compression routine, but:
A) it's not returning the right stuff; the buffer compression routines
do not return a complete gzip file, which is what is expected by the
browser
B) it crashed the server a few times; not sure if the gzip library
is thread-safe, etc.
Here is a link to the C module: http://www.rubylane.com/public/rlreturnz
If anyone has more time to pursue this, feel free to download it.
Here is the output of gzip for the string "hi":
$ od -tx1 y.gz
0000000 1f 8b 08 08 f1 91 c7 3a 00 03 79 00 cb c8 04 00
0000020 ac 2a 93 d8 02 00 00 00
Here is the output of the gzip "compress2" routine:
(gdb) p outlen
$3 = 10
(gdb) x/10xb c
0x7ffffcf4: 0x78 0x9c 0xcb 0xc8 0x04 0x00 0x01 0x3b
0x7ffffcfc: 0x00 0xd2
Here is a link to a PHP page that shows what they do:
http://php.weblogs.com/stories/http_compression
print "\x1f\x8b\x08\x00\x00\x00\x00\x00";
$Size = strlen($Contents);
$Crc = crc32($Contents);
$Contents = gzcompress($Contents,$level);
$Contents = substr($Contents, 0, strlen($Contents) - 4);
print $Contents;
print pack('V',$Crc);
print pack('V',$Size);
They add an 8-byte header, use all but the last 4 bytes of the compress
output, add 4 CRC bytes (little-endian order) and 4 "original length"
bytes, again in LE order. The CRC comes from crc32, part of the
gzip library (libz.so). But looking at what I get from compress2,
this doesn't look exactly right. Not sure if gzcompress is exactly
equivalent to compress2 or not.
The PHP routine also sends out Content-Encoding: gzip or x-gzip,
depending on what the browser uses for Accept-Encoding. Not sure
what the difference is, if any.
Some other useful pages:
Source of mod_gzip:
http://www.remotecommunications.com/apache/mod_gzip/src/1.3.19.1a/mod_gzip.txt
zlib web site:
http://www.info-zip.org/pub/infozip/zlib/
Manual for zlib:
http://www.info-zip.org/pub/infozip/zlib/manual.html
PHP man pages for gzencode, which creates a gzip file in a string:
http://www.php.net/manual/en/function.gzencode.php
Our use for this is such that the TCL hack is okay, though the module
would obviously be better. Figured I'd at least publish what I did
so far in case anyone is interested.
Jim