From b115d110a3e82b8dceb6941534c3ed1c4378458f Mon Sep 17 00:00:00 2001 From: Peter Griess Date: Sat, 7 Jan 2012 17:37:05 -0600 Subject: [PATCH] Don't wait for EOF on 0-length KA messages. - Break EOF handling out of http_should_keep_alive() into http_message_needs_eof(), which we now use when determining what to do with a message of unknown length. This prevents us from falling into the s_body_identity_eof state in the cases where we actually *do* know the length of the message (e.g. because the response status was 204). --- http_parser.c | 47 +++++++++++++++++++++++++++++------------------ test.c | 2 +- 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/http_parser.c b/http_parser.c index 52992f9..9150c05 100644 --- a/http_parser.c +++ b/http_parser.c @@ -358,6 +358,8 @@ static struct { }; #undef HTTP_STRERROR_GEN +int http_message_needs_eof(http_parser *parser); + /* Our URL parser. * * This is designed to be shared by http_parser_execute() for URL validation, @@ -1541,7 +1543,8 @@ size_t http_parser_execute (http_parser *parser, /* Content-Length header given and non-zero */ state = s_body_identity; } else { - if (parser->type == HTTP_REQUEST || http_should_keep_alive(parser)) { + if (parser->type == HTTP_REQUEST || + !http_message_needs_eof(parser)) { /* Assume content-length 0 - read the next */ CALLBACK2(message_complete); state = NEW_MESSAGE(); @@ -1704,6 +1707,30 @@ error: } +/* Does the parser need to see an EOF to find the end of the message? */ +int +http_message_needs_eof (http_parser *parser) +{ + if (parser->type == HTTP_REQUEST) { + return 0; + } + + /* See RFC 2616 section 4.4 */ + if (parser->status_code / 100 == 1 || /* 1xx e.g. Continue */ + parser->status_code == 204 || /* No Content */ + parser->status_code == 304 || /* Not Modified */ + parser->flags & F_SKIPBODY) { /* response to a HEAD request */ + return 0; + } + + if ((parser->flags & F_CHUNKED) || parser->content_length >= 0) { + return 0; + } + + return 1; +} + + int http_should_keep_alive (http_parser *parser) { @@ -1719,23 +1746,7 @@ http_should_keep_alive (http_parser *parser) } } - if (parser->type == HTTP_REQUEST) { - return 1; - } - - /* See RFC 2616 section 4.4 */ - if (parser->status_code / 100 == 1 || /* 1xx e.g. Continue */ - parser->status_code == 204 || /* No Content */ - parser->status_code == 304 || /* Not Modified */ - parser->flags & F_SKIPBODY) { /* response to a HEAD request */ - return 1; - } - - if (parser->flags & F_CHUNKED || parser->content_length >= 0) { - return 1; - } - - return 0; + return !http_message_needs_eof(parser); } diff --git a/test.c b/test.c index 146d7c4..fd41d97 100644 --- a/test.c +++ b/test.c @@ -1218,7 +1218,7 @@ const struct message responses[] = "Connection: close\r\n" "\r\n" ,.should_keep_alive= FALSE - ,.message_complete_on_eof= TRUE + ,.message_complete_on_eof= FALSE ,.http_major= 1 ,.http_minor= 1 ,.status_code= 204