c++ - 我如何发送 gzip 而不是通过跳过 header 缩小

标签 c++ gzip zlib deflate

我写了一个 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;
}     

 .....

最佳答案

deflategzip 编码并不完全相同,尽管差异很小。

当您发送 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.”

http://www.gzip.org/zlib/zlib_faq.html#faq38

关于c++ - 我如何发送 gzip 而不是通过跳过 header 缩小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20949329/

相关文章:

c++ - 我怎样才能知道 OpenCV 的 VideoCapture 初始化时出了什么问题?

c++ - dynamic_bit 设置打印?

java - 改造 - 解压 gzip

C++/C多线程同时读取gz文件

在 std::array 中使用默认参数调用 C++ 函数?

c++ - boost 非结构化字符串的 regex_search

java - 如何使用 Netty deflator 压缩发送到浏览器的响应

java - 如何使用 GZIP 优化 Web 服务消息的大小?

ruby-on-rails - 无法运行 gem 命令并使用 zlib 支持构建 ruby

php - pylibmc 存储的数据无法在 php memcached 中解压缩