Search code examples
httpchunked-encodingchunkinghttp-chunked

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?


Solution

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