Skip to content
This repository was archived by the owner on Nov 6, 2022. It is now read-only.

Fix HTTP 1.1 read until EOF / connection close behavior #79

Closed
wants to merge 2 commits into from

Conversation

gwik
Copy link

@gwik gwik commented Jan 24, 2012

The parser should not close the connection nor try to read until EOF if the protocol is 1.1 and the Connection: close header is not set according to the RFC[1]. This was making the parser to close the connection for every message without a body if the Content-Length was not 0.

For example:

HTTP/1.1 200 OK\r\n\r\n

Should not close the connection.

HTTP/1.1 200 OK\r\n
Connection: close\r\n
\r\n
Hello world!

Should read until EOF and close the connection.

[1] http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.4 (.5)

@koichik
Copy link
Contributor

koichik commented Jan 24, 2012

HTTP/1.1 200 OK\r\n\r\n

Unless this is a response to a HEAD request, http-parser cannot determine the length of the body. So, it should read until EOF. (It does not match .1-4 of sec 4.4, then .5 is applied.)

This was making the parser to close the connection for every message without a body

You should respond with 204 No Content, not 200.

@bnoordhuis
Copy link
Member

Thanks, but like @koichik said, it's not possible to determine the response body's size so there is no alternative but to read until EOF.

The 'single-line response to a HEAD request' case doesn't set the keep-alive flag right now because the parser doesn't know that it's a response to a HEAD request. I suppose that could be fixed (probably not easily) and it should be sufficiently uncommon that it's a minor optimization at best.

@bnoordhuis bnoordhuis closed this Jan 24, 2012
@koichik
Copy link
Contributor

koichik commented Jan 24, 2012

@bnoordhuis - If on_headers_complete() returns 1, then F_SKIPBODY flag is set. It indicates that it is a response to a HEAD request.

@gwik
Copy link
Author

gwik commented Jan 24, 2012

I'm not talking about HEAD requests.
A response to a GET, POST,... can be without a body. If there is no content-length nor transfer-encoding and no connection: close headers then response has no body and the parser should consider should be done (message_complete) after the headers and should not try to read a body until EOF.

http://tools.ietf.org/html/rfc2616#section-4.4 5.

If you think about it's pretty consistent because the client shouldn't close the connection (and the server don't expect the client to) if there is no connection: close header.

@bnoordhuis
Copy link
Member

@koichik: You're right. So much the better.

@gwik: See #72. The 'read until EOF if no Content-Length' behaviour was added in response to real-world scenarios.

@gwik
Copy link
Author

gwik commented Jan 24, 2012

What I suggested was in complement to that, but reading other parts of the RFC, I realize it doesn't matter. The scenario I was describing shouldn't happen if the server follows the RFC. I just need to change my test cases.

Sorry to have bothered you about it and thank you for this great parser.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants