同时使用分块传输编码和 gzip
我最近开始在我的网站上使用 gzip,它在除 Opera 之外的所有浏览器上都表现得非常出色,它会给出错误,指出由于数据损坏而无法解压缩内容。根据我从测试和谷歌搜索中收集到的信息,使用 gzip 和分块传输编码可能会出现问题。请求像 css 文件这样的小文件时没有错误的事实也表明了这个方向。
这是一个已知问题还是还有其他我没有考虑过的问题?
有人还提到这可能与发送 Content-Length 标头有关。
这是我的代码最相关部分的简化版本:
$contents = ob_get_contents();
ob_end_clean();
header('Content-Encoding: '.$encoding);
print("\x1f\x8b\x08\x00\x00\x00\x00\x00");
$size = strlen($contents);
$contents = gzcompress($contents, 9);
$contents = substr($contents, 0, $size);
print($contents);
exit();
I recently started using gzip on my site and it worked like a charm on all browsers except Opera which gives an error saying it can not decompress the content due to damaged data. From what I can gather from testing and googling it might be a problem with using both gzip and chunked transfer encoding. The fact that there is no error when requesting small files like css-files also points in that direction.
Is this a known issue or is there something else that I havent thought about?
Someone also mentioned that it could have something to do with sending a Content-Length header.
Here is a simplified version of the most relevant part of my code:
$contents = ob_get_contents();
ob_end_clean();
header('Content-Encoding: '.$encoding);
print("\x1f\x8b\x08\x00\x00\x00\x00\x00");
$size = strlen($contents);
$contents = gzcompress($contents, 9);
$contents = substr($contents, 0, $size);
print($contents);
exit();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
GZip 和分块编码在 Web 上一直一起使用,所以我怀疑问题是单独造成的。
如果使用分块编码,则不应发送 Content-Length 标头。
另外,当您协商 gzip 时,您应该发送 Vary: Accept-Encoding(针对压缩和未压缩响应),并且如果您发送 ETag,则压缩和未压缩响应的它们需要不同。
尝试通过 http://redbot.org/ 运行 URL - 它会检查 gzip 编码的一些常见问题。
GZip and chunked encoding are used together all of the time on the Web, so I doubt the problem is caused by that alone.
You shouldn't send a Content-Length header if chunked encoding is in use.
Also, when you negotiate for gzip, you should send Vary: Accept-Encoding (on compressed and uncompressed responses), and if you send ETags, they need to be different for the compressed and uncompressed responses.
Try running the URL through http://redbot.org/ -- it checks for a few common problems with gzip encoding.
理想情况下,您应该检查客户端的请求标头,以确保它支持编码响应。要查找的标头是:“Accept-Encoding:gzip,deflate”。您还应该检查客户端是否使用 HTTP 1.1。
Ideally, you should check the request header from the client to make sure it supports an encoded response. The header to look for is : "Accept-Encoding: gzip,deflate". You should also probably check whether the client is using HTTP 1.1.