使用http压缩时的内容长度

发布于 2024-09-25 07:54:28 字数 137 浏览 1 评论 0原文

客户端向 http 服务器发出 0-1023 范围的请求。它更喜欢 gzip 压缩 接受编码:gzip;q=1.0,身份; q=0.5,*;q=0 在请求中。

响应标头中的内容长度是多少?是1024还是压缩数据的大小。

谢谢,

The client is making a range request 0-1023 to the http server. It prefers gzip compression with
Accept-Encoding: gzip;q=1.0, identity; q=0.5, *;q=0
in the request.

What would be the content-length in the response header? Will it be 1024 or the size of the compressed data.

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

眼藏柔 2024-10-02 07:54:28

它取决于内容编码

RFC 2616 对 Content-Length 有这样的规定(除其他外):

应用程序应该使用此字段来指示传输长度
消息正文,除非第 1 节中的规则禁止这样做
4.4.

所以我们必须弄清楚传输长度是什么; 第 4.4 节(消息长度)说了这两件事关于传输长度:

消息的传输长度是消息正文的长度,如下所示
它出现在消息中;也就是说,在任何传输编码之后
已应用。

如果存在 Content-Length 标头字段(第 14.13 节),则其
OCTET 中的十进制值同时表示实体长度和实体长度
传输长度。如果满足以下条件,则不得发送 Content-Length 标头字段:
这两个长度不同

好的,所以我们知道在这种情况下,transfer-length、entity-length 和 Content-Length 都具有相同的值,并且都指的是“消息正文中出现的长度” ”,所以我们必须确定消息体是什么。 第 4.3 节介绍了有关消息正文的内容:

HTTP 消息的消息体(如果有)用于携带
与请求或响应关联的实体主体。”

那么什么是实体主体?为此,您必须基本上引用所有 第 7 节。(其中还定义了实体长度。)最重要的是,有:

实体主体:=内容编码(内容类型(数据))

实体主体的长度(因此我们根据 4.4 规定的 Content-Length 值)是内容编码后数据的长度。

It depends on the Content-Encoding.

RFC 2616 has this to say (amongst other things) about Content-Length:

Applications SHOULD use this field to indicate the transfer-length of
the message-body, unless this is prohibited by the rules in section
4.4.

So we have to figure out what transfer-length is; Section 4.4 (Message Length) says these two things about transfer-length:

The transfer-length of a message is the length of the message-body as
it appears in the message; that is, after any transfer-codings have
been applied.

If a Content-Length header field (section 14.13) is present, its
decimal value in OCTETs represents both the entity-length and the
transfer-length. The Content-Length header field MUST NOT be sent if
these two lengths are different

Okay, so we know that in this case transfer-length, entity-length, and Content-Length all have the same value, and all refer to "the length of the message-body as it appears in the message", and so we have to determine what message-body is. Section 4.3 says this about message-body:

The message-body (if any) of an HTTP message is used to carry the
entity-body associated with the request or response."

So what's an entity-body? For that you have to refer to basically all of Section 7. (Which also defines entity-length.) Most importantly, there this:

entity-body := Content-Encoding( Content-Type( data ) )

The length of the entity-body (and therefore our value for Content-Length per 4.4) is the length of the data after content-coding.

终止放荡 2024-10-02 07:54:28

实际内容长度取决于传输编码和数据:如果使用identity,则不应用压缩,内容长度为1024;如果您使用gzip,则实际内容长度取决于要压缩的数据。

The actual content length depends on the transfer encoding and data: If you use identity, no compression is applied and the content length is 1024; if you use gzip, the actual content length depends on the data that is to be compressed.

音盲 2024-10-02 07:54:28

实际上它是 1024,这是压缩数据的大小。

Actually it will be 1024 which is the size of compressed data.

翻身的咸鱼 2024-10-02 07:54:28

Content-Length 响应标头的目的是让客户端准确地知道要读取多少字节,直到获得完整的响应。因此,长度必须是压缩后的长度(浏览器在解压缩之前从套接字接收到的长度)。

The purpose of Content-Length response headers is to allow the client to know the precisely how many bytes to read until it has the entire response. Hence the length must be the compressed one (what the browser will receive from the socket before decompression).

念﹏祤嫣 2024-10-02 07:54:28

我正在为未来的读者回答一个老问题,因为所有旧答案都是不正确的。

假设选择gzip内容编码并且压缩内容的大小大于1024,则范围请求的响应的内容长度将为1024,这是编码内容的前1024字节部分的大小(根据 RFC 9110 选择的表示形式)。

14.1.2 部分。 RFC 9110 的字节范围规定如下:

如果表示数据应用了内容编码,则每个字节范围是根据编码的字节序列计算的,而不是根据解码后获得的底层字节序列计算的。

还有 15.3.7 部分。 206 部分内容 内容如下:

206 响应中存在的 Content-Length 标头字段指示该消息内容中的八位字节数,这通常不是所选表示的完整长度。每个 Content-Range 标头字段都包含有关所选表示的完整长度的信息。

I'm answering an old question for future readers because all of the old answers are incorrect.

Assuming the gzip content encoding is chosen and the size of the compressed content is larger than 1024, the content length of the response of the range request will be 1024, which is the size of the first 1024 byte part of the encoded content(the selected representation in terms of RFC 9110).

The section 14.1.2. Byte Ranges of RFC 9110 says the following:

If the representation data has a content coding applied, each byte range is calculated with respect to the encoded sequence of bytes, not the sequence of underlying bytes that would be obtained after decoding.

Also the section 15.3.7. 206 Partial Content says the following:

A Content-Length header field present in a 206 response indicates the number of octets in the content of this message, which is usually not the complete length of the selected representation. Each Content-Range header field includes information about the selected representation's complete length.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文