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

Fix: Content-Length with obsolete line folding and invalid input #458

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -1434,6 +1434,10 @@ size_t http_parser_execute (http_parser *parser,
parser->header_state = h_content_length_num;
break;

// when obsolete line folding is encountered for content length continue to the s_header_value state
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use C89-style comments and wrap at 80 columns? Thanks.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

case h_content_length_ws:
break;

case h_connection:
/* looking for 'Connection: keep-alive' */
if (c == 'k') {
Expand Down Expand Up @@ -1677,6 +1681,10 @@ size_t http_parser_execute (http_parser *parser,
case s_header_value_lws:
{
if (ch == ' ' || ch == '\t') {
if (parser->header_state == h_content_length_num) {
// treat obsolete line folding as space
parser->header_state = h_content_length_ws;
}
UPDATE_STATE(s_header_value_start);
REEXECUTE();
}
Expand Down
14 changes: 14 additions & 0 deletions test.c
Original file line number Diff line number Diff line change
Expand Up @@ -4203,6 +4203,20 @@ main (void)
HPE_INVALID_CONTENT_LENGTH,
HTTP_REQUEST);

test_simple_type(
"POST / HTTP/1.1\r\n"
"Content-Length: 42\r\n"
" Hello world!\r\n",
HPE_INVALID_CONTENT_LENGTH,
HTTP_REQUEST);

test_simple_type(
"POST / HTTP/1.1\r\n"
"Content-Length: 42\r\n"
" \r\n",
HPE_OK,
HTTP_REQUEST);

//// RESPONSES

test_simple_type("HTP/1.1 200 OK\r\n\r\n", HPE_INVALID_VERSION, HTTP_RESPONSE);
Expand Down