我写了一个 C++ http 服务器(Microsoft http 服务器 api)
以gzip格式发送html页面文件
gzip文件是静态的
例如文件 page1.htm 和 page1.htm.gz 在同一目录中
根据 http://en.wikipedia.org/wiki/Gzip
我知道 gzip 压缩了额外的标题 deflate 是 gzip 的一部分
我如何通过跳过 header 发送 gzip instade deflate
fileHandle=CreateFile( "page1.htm.gz" ,dwAccess,dwShare,NULL,dwCreationFlags,dwAttributes,NULL);
....
ADD_KNOWN_HEADER(response, HttpHeaderContentEncoding, "deflate" );
HTTP_DATA_CHUNK dataChunk;
{
HTTP_DATA_CHUNK dataChunk;
response.EntityChunkCount = 1;
dataChunk.DataChunkType = HttpDataChunkFromFileHandle;
dataChunk.FromFileHandle.FileHandle =fileHandle;
dataChunk.FromFileHandle.ByteRange.StartingOffset.QuadPart =9;// 9 is gzip header len
dataChunk.FromFileHandle.ByteRange.Length.QuadPart = HTTP_BYTE_RANGE_TO_EOF;
response.pEntityChunks=&dataChunk;
}
.....
最佳答案
deflate 和gzip 编码并不完全相同,尽管差异很小。
当您发送 gzip 时,将您的代码更改为:
ADD_KNOWN_HEADER(response, HttpHeaderContentEncoding, "gzip" );
如果 Accept-Encoding
中列出了 gzip,您当然应该这样做。
以下是 gzip 常见问题解答的摘录:
“ What's the difference between the "gzip" and "deflate" HTTP 1.1 encodings?
"gzip" is the gzip format, and "deflate" is the zlib format. They should probably have called the second one "zlib" instead to avoid confusion with the raw deflate compressed data format. While the HTTP 1.1 RFC 2616 correctly points to the zlib specification in RFC 1950 for the "deflate" transfer encoding, there have been reports of servers and browsers that incorrectly produce or expect raw deflate data per the deflate specficiation in RFC 1951, most notably Microsoft. So even though the "deflate" transfer encoding using the zlib format would be the more efficient approach (and in fact exactly what the zlib format was designed for), using the "gzip" transfer encoding is probably more reliable due to an unfortunate choice of name on the part of the HTTP 1.1 authors.”
关于c++ - 我如何发送 gzip 而不是通过跳过 header 缩小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20949329/