gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 17:16:46 25 4
gpt4 key购买 nike

我目前正在用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com