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

Commit

Permalink
parser: fix Content-Length header parsing.
Browse files Browse the repository at this point in the history
Commit e2e467b ("Update http-parser to 2.6.1") enforces that messages
contain no more than one Content-Length header but it considers any
header that starts with "Content-Length" as a duplicate.

Fix: #324
PR-URL: #325
Reviewed-By: Fedor Indutny <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
bnoordhuis authored and indutny committed Jul 17, 2016
1 parent b2cc8e4 commit cacb07d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
11 changes: 6 additions & 5 deletions http_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -1366,12 +1366,7 @@ size_t http_parser_execute (http_parser *parser,
|| c != CONTENT_LENGTH[parser->index]) {
parser->header_state = h_general;
} else if (parser->index == sizeof(CONTENT_LENGTH)-2) {
if (parser->flags & F_CONTENTLENGTH) {
SET_ERRNO(HPE_UNEXPECTED_CONTENT_LENGTH);
goto error;
}
parser->header_state = h_content_length;
parser->flags |= F_CONTENTLENGTH;
}
break;

Expand Down Expand Up @@ -1474,6 +1469,12 @@ size_t http_parser_execute (http_parser *parser,
goto error;
}

if (parser->flags & F_CONTENTLENGTH) {
SET_ERRNO(HPE_UNEXPECTED_CONTENT_LENGTH);
goto error;
}

parser->flags |= F_CONTENTLENGTH;
parser->content_length = ch - '0';
break;

Expand Down
26 changes: 26 additions & 0 deletions test.c
Original file line number Diff line number Diff line change
Expand Up @@ -1745,6 +1745,32 @@ const struct message responses[] =
,.body= ""
}

#define CONTENT_LENGTH_X 21
, {.name= "Content-Length-X"
,.type= HTTP_RESPONSE
,.raw= "HTTP/1.1 200 OK\r\n"
"Content-Length-X: 0\r\n"
"Transfer-Encoding: chunked\r\n"
"\r\n"
"2\r\n"
"OK\r\n"
"0\r\n"
"\r\n"
,.should_keep_alive= TRUE
,.message_complete_on_eof= FALSE
,.http_major= 1
,.http_minor= 1
,.status_code= 200
,.response_status= "OK"
,.num_headers= 2
,.headers= { { "Content-Length-X", "0" }
, { "Transfer-Encoding", "chunked" }
}
,.body= "OK"
,.num_chunks_complete= 2
,.chunk_lengths= { 2 }
}

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

Expand Down

0 comments on commit cacb07d

Please sign in to comment.