From 095a6ac07556bf514fd37e47dac0a67b5fd292c2 Mon Sep 17 00:00:00 2001 From: Olga Batyshkina Date: Thu, 20 Dec 2018 13:07:46 +0100 Subject: [PATCH 1/2] Allow duplicated Content-Length with the same value --- http_parser.c | 20 ++++++++++++++------ http_parser.h | 1 + 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/http_parser.c b/http_parser.c index ba1374e9..34e41dae 100644 --- a/http_parser.c +++ b/http_parser.c @@ -1424,12 +1424,6 @@ 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'; parser->header_state = h_content_length_num; break; @@ -1695,6 +1689,20 @@ size_t http_parser_execute (http_parser *parser, case h_connection_upgrade: parser->flags |= F_CONNECTION_UPGRADE; break; + case h_content_length_num: + case h_content_length_ws: + if (parser->flags & F_CONTENTLENGTH) { + // content length sent multiple times, check if value is the same + if (parser->initial_content_length != parser->content_length) { + SET_ERRNO(HPE_UNEXPECTED_CONTENT_LENGTH); + goto error; + } + } else { + // set content length flag + parser->flags |= F_CONTENTLENGTH; + parser->initial_content_length = parser->content_length; + } + break; default: break; } diff --git a/http_parser.h b/http_parser.h index e894d7ce..684befad 100644 --- a/http_parser.h +++ b/http_parser.h @@ -301,6 +301,7 @@ struct http_parser { uint32_t nread; /* # bytes read in various scenarios */ uint64_t content_length; /* # bytes in body (0 if no Content-Length header) */ + uint64_t initial_content_length; /* # used to verify same value if Content-Length header occurs multiple times */ /** READ-ONLY **/ unsigned short http_major; From 93e2ef5d6b41cc0714f72983d603417b3cd0bf1d Mon Sep 17 00:00:00 2001 From: Olga Batyshkina Date: Sun, 23 Dec 2018 19:12:46 +0100 Subject: [PATCH 2/2] Update comments to C89-style --- http_parser.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/http_parser.c b/http_parser.c index 34e41dae..0b9c83e0 100644 --- a/http_parser.c +++ b/http_parser.c @@ -1692,13 +1692,14 @@ size_t http_parser_execute (http_parser *parser, case h_content_length_num: case h_content_length_ws: if (parser->flags & F_CONTENTLENGTH) { - // content length sent multiple times, check if value is the same + /* content length sent multiple times, + * check if value is the same */ if (parser->initial_content_length != parser->content_length) { SET_ERRNO(HPE_UNEXPECTED_CONTENT_LENGTH); goto error; } } else { - // set content length flag + /* set content length flag */ parser->flags |= F_CONTENTLENGTH; parser->initial_content_length = parser->content_length; }