HTTP分块编码。需要一个在SPEC中提到的“拖车”的例子。

时间:2021-01-21 23:46:50

I am writing an HTTP parser for a transparent proxy. What is stumping me is the Trailer: mentioned in the specs for Transfer-Encoding: chunked. What does it look like?

我正在为一个透明的代理编写一个HTTP解析器。让我感到困惑的是预告片:在转换编码的规范中提到:chunked。它看起来像什么?

Normally, a HTTP chunked ends like this.

正常情况下,HTTP块是这样的。

0\r\n
\r\n

What I am confused about is how to detect the end of the chunk if there is some sort of trailing headers...

我困惑的是,如果有某种尾随头,如何检测块的末端……

UPDATE: I believe that a simple \r\n\r\n i.e. an empty line is enough to detect the end of trailing headers... Is that correct?

更新:我相信一个简单的\ \ \ \ \n,即一个空行就足以检测到尾标头的结尾……那是正确的吗?

3 个解决方案

#1


15  

0\r\n
SomeAfterHeader: TheData \r\n
\r\n

0\r\n SomeAfterHeader: TheData \r\n \r\n。

In other words, it is sufficient to look for a \r\n\r\n, in layman's terms: a blank line. To detect the end of a chunked transmission. But it is very important that each chunk is read before doing this. Because the chunked data itself can contain blank lines which would erroneously be detected as the end of the stream.

换句话说,在外行人的条件下,用一个空行来查找一个\ \n\r\n,就足够了。检测到一个分块传输的结束。但是,在做这些之前,读取每个块是非常重要的。由于块数据本身可以包含空行,因此错误地被检测为流的结尾。

#2


13  

Regarding trailer:

关于预告片:

The list of trailing headers should be specified in the Trailer header, as you note.

尾标头的列表应该在拖车头中指定,如您所注意到的。

The BNF in Section 14.40 of RFC 2616 is this:

RFC 2616第14.40节的BNF:

Trailer  = "Trailer" ":" 1#field-name

Gourley and Totty give this example:

Gourley和Totty给出了这个例子:

Trailer: Content-Length

(It's odd that they give this example, since Content-Length is explicitly forbidden to be a trailing header in 14.40.)

(他们给出这个例子是很奇怪的,因为内容长度被明确禁止在14.40中是一个尾随头。)

Shiflett gives this example:

Shiflett给这个例子:

Trailer: Date

Regarding end of message with trailing headers:

关于结尾的消息与尾随标题:

The BNF in Section 3.6.1 of RFC 2616 is what you're looking for. Here's part:

RFC 2616第3.6.1节中的BNF是您要找的。这是部分:

Chunked-Body = *chunk
               last-chunk
               trailer
               CRLF
last-chunk   = 1*("0") [ chunk-extension ] CRLF
trailer      = *(entity-header CRLF)

So the last chunk and 2 trailing headers might look like this:

所以最后一个块和两个尾部标题可能是这样的:

0<CRLF>
Date:Sun, 06 Nov 1994 08:49:37 GMT<CRLF>
Content-MD5:1B2M2Y8AsgTpgAmY7PhCfg==<CRLF>
<CRLF>

#3


13  

Below is a copy of an example trailer I copied from The TCP/IP Guide site. HTTP分块编码。需要一个在SPEC中提到的“拖车”的例子。

下面是我从TCP/IP指南网站复制的一个示例预告片。

As we can see, if we want to use trailer header, we need add a "Trailer:header_name" header field with a header name and then add the trailer header entity after chunked body area.

正如我们所看到的,如果我们想要使用拖车头,我们需要添加一个“拖车:header_name”头字段和一个标题名称,然后在chunked body area之后添加拖车头实体。

We can add 0 or more trailer headers in a HTTP body per the RFC. Section 4.1.2 of RFC7230 bans the use of following headers in trailer header area:

我们可以在每个RFC的HTTP主体中添加0或更多的拖车头。RFC7230的第4.1.2节禁止在拖车头区域使用下列标头:

A sender MUST NOT generate a trailer that contains a field necessary for message framing (e.g., Transfer-Encoding and Content-Length), routing (e.g., Host), request modifiers (e.g., controls and conditionals in Section 5 of RFC7231), authentication (e.g., see RFC7235 and RFC6265), response control data (e.g., see Section 7.1 of RFC7231), or determining how to process the payload (e.g., Content-Encoding, Content-Type, Content-Range, and Trailer).

发送者不能生成一个预告片,包含一个消息分帧所需字段(例如,传输编码和内容长度),路由(例如,主机),请求修饰符(例如,控制和条件RFC7231 5节),身份验证(例如,看到RFC7235 RFC6265),响应控制数据(例如,参见7.1节RFC7231),或决定如何处理负载(如内容编码、内容类型、含量和拖车)。

This means we can use other standard headers and custom headers in trailer header area.

这意味着我们可以在拖车头区域使用其他标准头和自定义标头。

#1


15  

0\r\n
SomeAfterHeader: TheData \r\n
\r\n

0\r\n SomeAfterHeader: TheData \r\n \r\n。

In other words, it is sufficient to look for a \r\n\r\n, in layman's terms: a blank line. To detect the end of a chunked transmission. But it is very important that each chunk is read before doing this. Because the chunked data itself can contain blank lines which would erroneously be detected as the end of the stream.

换句话说,在外行人的条件下,用一个空行来查找一个\ \n\r\n,就足够了。检测到一个分块传输的结束。但是,在做这些之前,读取每个块是非常重要的。由于块数据本身可以包含空行,因此错误地被检测为流的结尾。

#2


13  

Regarding trailer:

关于预告片:

The list of trailing headers should be specified in the Trailer header, as you note.

尾标头的列表应该在拖车头中指定,如您所注意到的。

The BNF in Section 14.40 of RFC 2616 is this:

RFC 2616第14.40节的BNF:

Trailer  = "Trailer" ":" 1#field-name

Gourley and Totty give this example:

Gourley和Totty给出了这个例子:

Trailer: Content-Length

(It's odd that they give this example, since Content-Length is explicitly forbidden to be a trailing header in 14.40.)

(他们给出这个例子是很奇怪的,因为内容长度被明确禁止在14.40中是一个尾随头。)

Shiflett gives this example:

Shiflett给这个例子:

Trailer: Date

Regarding end of message with trailing headers:

关于结尾的消息与尾随标题:

The BNF in Section 3.6.1 of RFC 2616 is what you're looking for. Here's part:

RFC 2616第3.6.1节中的BNF是您要找的。这是部分:

Chunked-Body = *chunk
               last-chunk
               trailer
               CRLF
last-chunk   = 1*("0") [ chunk-extension ] CRLF
trailer      = *(entity-header CRLF)

So the last chunk and 2 trailing headers might look like this:

所以最后一个块和两个尾部标题可能是这样的:

0<CRLF>
Date:Sun, 06 Nov 1994 08:49:37 GMT<CRLF>
Content-MD5:1B2M2Y8AsgTpgAmY7PhCfg==<CRLF>
<CRLF>

#3


13  

Below is a copy of an example trailer I copied from The TCP/IP Guide site. HTTP分块编码。需要一个在SPEC中提到的“拖车”的例子。

下面是我从TCP/IP指南网站复制的一个示例预告片。

As we can see, if we want to use trailer header, we need add a "Trailer:header_name" header field with a header name and then add the trailer header entity after chunked body area.

正如我们所看到的,如果我们想要使用拖车头,我们需要添加一个“拖车:header_name”头字段和一个标题名称,然后在chunked body area之后添加拖车头实体。

We can add 0 or more trailer headers in a HTTP body per the RFC. Section 4.1.2 of RFC7230 bans the use of following headers in trailer header area:

我们可以在每个RFC的HTTP主体中添加0或更多的拖车头。RFC7230的第4.1.2节禁止在拖车头区域使用下列标头:

A sender MUST NOT generate a trailer that contains a field necessary for message framing (e.g., Transfer-Encoding and Content-Length), routing (e.g., Host), request modifiers (e.g., controls and conditionals in Section 5 of RFC7231), authentication (e.g., see RFC7235 and RFC6265), response control data (e.g., see Section 7.1 of RFC7231), or determining how to process the payload (e.g., Content-Encoding, Content-Type, Content-Range, and Trailer).

发送者不能生成一个预告片,包含一个消息分帧所需字段(例如,传输编码和内容长度),路由(例如,主机),请求修饰符(例如,控制和条件RFC7231 5节),身份验证(例如,看到RFC7235 RFC6265),响应控制数据(例如,参见7.1节RFC7231),或决定如何处理负载(如内容编码、内容类型、含量和拖车)。

This means we can use other standard headers and custom headers in trailer header area.

这意味着我们可以在拖车头区域使用其他标准头和自定义标头。