我目前正在用 Java 实现一个 HTTP 服务器,但在传输编码方面遇到了一个问题。
同时
HTTP/1.1 200 OK
Content-Type: text/plain
Content-Encoding: gzip
Transfer-Encoding: chunked
工作正常,使用 gzip
和 chunked
作为传输编码,就像这样:
Transfer-Encoding: gzip, chunked
导致浏览器无法正确显示响应。
当 Chrome 尝试将资源下载为 .gz
文件时,Firefox 尝试显示它,结果如下:
奇怪的是,服务器生成的消息体与使用gzip
作为Content-Encoding
时完全一样,因为RFC7230允许申请多个传输编码,如果最后一个应用是 chunked
。
For example,
Transfer-Encoding: gzip, chunked
indicates that the payload body has been compressed using the gzip coding and then chunked using the chunked coding while forming the message body.
这是服务器的原始响应:
HTTP/1.1 200 OK
Date: Tue, 09 Jul 2019 17:52:41 GMT
Server: jPuzzle
Content-Type: text/plain
Transfer-Encoding: gzip, chunked
1c
òHÍÉÉW(Ï/ÊIQ ÿÿ
a
0
正如人们所猜测的那样,之后 body 被 gzip 压缩并分块。
我将不胜感激任何帮助,因为我看不出哪里违反了规范。
最佳答案
您应该为 end to end compression 使用 content-encoding
header .
Transfer-Encoding is a hop-by-hop header, that is applied to a message between two nodes, not to a resource itself. Each segment of a multi-node connection can use different Transfer-Encoding values. If you want to compress data over the whole connection, use the end-to-end Content-Encoding header instead.
此外,发送Accept-Encoding: gzip
请求头来告诉服务器客户端期望什么。
关于java - HTTP 传输编码 "gzip, chunked"未正确解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56958342/