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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions http_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -1817,6 +1817,14 @@ http_message_needs_eof (http_parser *parser)
return 0;
}

/* do not read until EOF if protocol is 1.1 and connection header
is not 'close' */
if (parser->http_major > 0 &&
parser->http_minor > 0 &&
!(parser->flags & F_CONNECTION_CLOSE)) {
return 0;
}

return 1;
}

Expand Down
45 changes: 35 additions & 10 deletions test.c
Original file line number Diff line number Diff line change
Expand Up @@ -874,8 +874,8 @@ const struct message responses[] =
, {.name= "404 no headers no body"
,.type= HTTP_RESPONSE
,.raw= "HTTP/1.1 404 Not Found\r\n\r\n"
,.should_keep_alive= FALSE
,.message_complete_on_eof= TRUE
,.should_keep_alive= TRUE
,.message_complete_on_eof= FALSE
,.http_major= 1
,.http_minor= 1
,.status_code= 404
Expand All @@ -889,8 +889,8 @@ const struct message responses[] =
, {.name= "301 no response phrase"
,.type= HTTP_RESPONSE
,.raw= "HTTP/1.1 301\r\n\r\n"
,.should_keep_alive = FALSE
,.message_complete_on_eof= TRUE
,.should_keep_alive = TRUE
,.message_complete_on_eof= FALSE
,.http_major= 1
,.http_minor= 1
,.status_code= 301
Expand Down Expand Up @@ -1121,23 +1121,27 @@ const struct message responses[] =

#define NO_CONTENT_LENGTH_NO_TRANSFER_ENCODING_RESPONSE 12
/* The client should wait for the server's EOF. That is, when neither
* content-length nor transfer-encoding is specified, the end of body
* content-length nor transfer-encoding is specified, and connection will close
* the end of body
* is specified by the EOF.
*/
, {.name= "neither content-length nor transfer-encoding response"
, {.name= "neither content-length nor transfer-encoding response "
"connection close header in HTTP 1.1"
,.type= HTTP_RESPONSE
,.raw= "HTTP/1.1 200 OK\r\n"
"Content-Type: text/plain\r\n"
"Connection: close\r\n"
"\r\n"
"hello world"
,.should_keep_alive= FALSE
,.message_complete_on_eof= TRUE
,.http_major= 1
,.http_minor= 1
,.status_code= 200
,.num_headers= 1
,.num_headers= 2
,.headers=
{ { "Content-Type", "text/plain" }
{ { "Content-Type", "text/plain" },
{ "Connection", "close" }
}
,.body= "hello world"
}
Expand Down Expand Up @@ -1184,14 +1188,17 @@ const struct message responses[] =
, {.name= "HTTP/1.1 with an EOF-terminated 200 status"
,.type= HTTP_RESPONSE
,.raw= "HTTP/1.1 200 OK\r\n"
"Connection: close\r\n"
"\r\n"
,.should_keep_alive= FALSE
,.message_complete_on_eof= TRUE
,.http_major= 1
,.http_minor= 1
,.status_code= 200
,.num_headers= 0
,.headers={}
,.num_headers= 1
,.headers=
{ { "Connection", "close"}
}
,.body_size= 0
,.body= ""
}
Expand Down Expand Up @@ -1286,6 +1293,24 @@ const struct message responses[] =
}
#endif /* !HTTP_PARSER_STRICT */

#define NO_CONTENT_LENGTH_NO_TRANSFER_ENCODING_RESPONSE_NO_CONNECTION_CLOSE 20
/* The client should not try to read a body until eof since the Connection:
close header is not set.
*/
, {.name= "neither content-length nor transfer-encoding response"
" and no connection close header with protocol 1.1"
,.type= HTTP_RESPONSE
,.raw= "HTTP/1.1 200 OK\r\n\r\n"
,.should_keep_alive= TRUE
,.message_complete_on_eof= FALSE
,.http_major= 1
,.http_minor= 1
,.status_code= 200
,.num_headers= 0
,.headers = {}
,.body= ""
}

, {.name= NULL } /* sentinel */
};

Expand Down