您的位置:首页 > 理论基础 > 计算机网络

http: 支持 Content-Encoding: gzip

2015-08-24 19:42 711 查看
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 -w

eval '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 files
use 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: