0

I have implemented a HTTP 1.1 server. It is an embedded server so I only support the mandatory features of the RFC. All responses are sent chunked-encoded. As HEAD is mandatory it is also supported.

HEAD is a GET without body. So the server is sending a response like following in response to a HEAD request:

HTTP/1.1 200 OK
Server: testServer
Connection: keep-alive
Transfer-Encoding: chunked

What I am wondering is do have to add a "0\r\n" as it required to signal the end of chunks:

 HTTP/1.1 200 OK
 Server: testServer
 Connection: keep-alive
 Transfer-Encoding: chunked

 0

I have tried to collect the relevant parts in the RFC:

"The HEAD method is identical to GET except that the server MUST NOT return a message-body in the response."

"All responses to the HEAD request method MUST NOT include a message-body, even though the presence of entity- header fields might lead one to believe they do."

"1.Any response message which "MUST NOT" include a message-body (such as the 1xx, 204, and 304 responses and any response to a HEAD request) is always terminated by the first empty line after the header fields, regardless of the entity-header fields present in the message."

So far I understand it that my first solution (without 0) is the correct one. But it seems to be strange to send a message with Transfer-encoding: chunked which does not terminate with the the chunk style 0\r\n.

tobias
  • 2,322
  • 3
  • 33
  • 53
  • The RFC explicitly tells you must not include a body "even though the presence of entity- header fields might lead one to believe they do". How much clearer than this can it get? – lanzz Sep 20 '12 at 20:54
  • You also tend to the first solution to not add the 0\r\n? Transfer-encoding is NO entity-header. It is a general-header. – tobias Sep 20 '12 at 20:58

1 Answers1

2

I guess the problem stated is solved as its quite old,

Recently I developed a test http server for streaming video and I wanted to put forth my learnings so that it might help others. First, "0\r\n" is not the marker for end of chunks, the chunked trailer is '0\r\n\r\n'.

And for the header the string should look like this

"HTTP/1.1 200 OK\r\n" \
"Server: testServer\r\n" \
"Connection: keep-alive\r\n" \
"Transfer-Encoding: chunked\r\n" \
"\r\n"

Note the last CRLF, its indicating end of header.

Hope this help.

explorer
  • 39
  • 1
  • 9