http: 支持 Content-Encoding: gzip

来源:互联网 发布:csgo控制台优化 编辑:程序博客网 时间:2024/05/23 19:18
curl -v --compressed http://localhost:8080/upload/a.out -o a.out
请求:
Accept-Encoding: gzip, deflate
响应:
Content-Encoding: gzip

gzip -9c xx.txt > xx.gz
开头是标记0x1f,0x8b,然后0x08表示使用deflate,前10个字节通常为:
1f 8b 08 00 00 00 00 00  00 03
中间是deflate的raw data
最后的8个字节是crc32和原始长度。

deflate 开头两个字节是0x78,0x9c, 参考文件[1]就是这种格式,
我写成gzip, curl可以收,但是chrome报错。
调用zlib,生成数据的差异只是在deflateInit(inflate)和deflateInit2(gzip)[2],其他相同。这点害的我调试了好久。
 
调试发现chsum 的crc根本对不上,下面的/bin/crc32和*.gz对得上:
#!/usr/bin/perl -weval 'exec /usr/bin/perl -w -S $0 ${1+"$@"}'    if 0; # not running under some shell# computes and prints to stdout the CRC-32 values of the given filesuse lib qw( blib/lib lib );use Archive::Zip;use FileHandle;my $totalFiles = scalar(@ARGV);foreach my $file (@ARGV) {    if ( -d $file ) {        warn "$0: ${file}: Is a directory\n";        next;    }    my $fh = FileHandle->new();    if ( !$fh->open( $file, 'r' ) ) {        warn "$0: $!\n";        next;    }    binmode($fh);    my $buffer;    my $bytesRead;    my $crc = 0;    while ( $bytesRead = $fh->read( $buffer, 32768 ) ) {        $crc = Archive::Zip::computeCRC32( $buffer, $crc );    }    printf( "%08x", $crc );    print("\t$file") if ( $totalFiles > 1 );    print("\n");}

[1] http://www.zlib.net/zpipe.c
[2] http://lxr.free-electrons.com/source/include/linux/zlib.h
0 0
原创粉丝点击