简体   繁体   English

使用分块传输编码和gzip

[英]Using both chunked transfer encoding and gzip

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. 我最近开始在我的网站上使用gzip,它在除Opera之外的所有浏览器上都起到了一种魅力作用,它给出了一个错误,即由于数据损坏而无法解压缩内容。 From what I can gather from testing and googling it might be a problem with using both gzip and chunked transfer encoding. 从我可以从测试和谷歌搜索中收集到的,使用gzip和chunked传输编码可能是一个问题。 The fact that there is no error when requesting small files like css-files also points in that direction. 请求像css文件这样的小文件时没有错误这一事实也指向了这个方向。

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. 有人还提到它可能与发送Content-Length标头有关。

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();

GZip and chunked encoding are used together all of the time on the Web, so I doubt the problem is caused by that alone. GZip和chunked编码在Web上一直使用,所以我怀疑问题是由单独引起的。

You shouldn't send a Content-Length header if chunked encoding is in use. 如果正在使用分块编码,则不应发送Content-Length标头。

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. 此外,当你协商gzip时,你应该发送Vary:Accept-Encoding(关于压缩和未压缩的响应),如果你发送ETag,它们需要对压缩和未压缩的响应有所不同。

Try running the URL through http://redbot.org/ -- it checks for a few common problems with gzip encoding. 尝试通过http://redbot.org/运行URL - 它检查gzip编码的一些常见问题。

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". 要查找的标题是:“Accept-Encoding:gzip,deflate”。 You should also probably check whether the client is using HTTP 1.1. 您还应该检查客户端是否使用HTTP 1.1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM