Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP Chunked Encoding. Need an example of 'Trailer' mentioned in SPEC

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?

Normally, a HTTP chunked ends like this.

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?

like image 924
unixman83 Avatar asked Apr 08 '11 05:04

unixman83


People also ask

What are HTTP trailers?

The HTTP Trailer header is a response header that indicates the given set of header fields is present in the trailer of a message encoded with chunked transfer-coding and allows the sender to include additional fields at the end of chunked messages in order to supply metadata. Syntax: Trailer: header-names.

Why chunked encoding is used?

Chunked encoding allows the sender to send additional header fields after the message body. This is important in cases where values of a field cannot be known until the content has been produced, such as when the content of the message must be digitally signed.

How do I send a chunked request?

Use the WEB SEND command to send the first chunk of the message. Specify CHUNKING(CHUNKYES) to tell CICS that it is a chunk of a message. Use the FROM option to specify the first chunk of data from the body of the message. Use the FROMLENGTH option to specify the length of the chunk.


2 Answers

Below is a copy of an example trailer I copied from The TCP/IP Guide site. trailer sample

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.

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:

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).

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

like image 52
appleleaf Avatar answered Sep 19 '22 13:09

appleleaf


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.

like image 28
unixman83 Avatar answered Sep 21 '22 13:09

unixman83