java - HTTP 传输编码 "gzip, chunked"未正确解释

标签 java http encoding

我目前正在用 Java 实现一个 HTTP 服务器,但在传输编码方面遇到了一个问题。

同时

HTTP/1.1 200 OK
Content-Type: text/plain
Content-Encoding: gzip
Transfer-Encoding: chunked

工作正常,使用 gzipchunked 作为传输编码,就像这样:

Transfer-Encoding: gzip, chunked

导致浏览器无法正确显示响应。

当 Chrome 尝试将资源下载为 .gz 文件时,Firefox 尝试显示它,结果如下:

Expected output: Hello world!

奇怪的是,服务器生成的消息体与使用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/

相关文章:

linux - NTLM 身份验证无效字符

python - 阿拉伯语编码-django

java - Spring Integration Gateway 中的静态和动态 header

java - 当您进行单元测试时,仅实例化其他域对象就足够了吗?

iphone - iPhone 中的 HTTP 身份验证

postgresql - PostgreSQL 查询中的奇怪编码问题

javascript - 为什么我不能在 node.js 中对这个 URL 进行 url 编码?

java - 无法以 AES 加密/解密中的字符串形式获取解密文件

java - DATE_SUB和INTERVAL计算可能错误

http - 302 从 HTTPS 重定向到 HTTP