Skip to content
Snippets Groups Projects
Commit 9ee7e568 authored by Nikolay Shirokovskiy's avatar Nikolay Shirokovskiy Committed by Aleksandr Lyapunov
Browse files

http_parser: fix parsing HTTP protocol version

Handle status header response like 'HTTP/2 200' with version without
dot.

Closes #7319

NO_DOC=bugfix
parent 9f2b015b
No related branches found
No related tags found
No related merge requests found
## bugfix/lib
* Fixed http.client to properly parse HTTP status header like 'HTTP/2 200' where
HTTP version does not have minor part (gh-7319).
......@@ -125,6 +125,11 @@ http_parse_status_line(struct http_parser *parser, const char **bufp,
state = sw_first_minor_digit;
break;
}
if (ch == ' ') {
parser->http_minor = 0;
state = sw_status;
break;
}
if (ch < '0' || ch > '9') {
return HTTP_PARSE_INVALID;
}
......
......@@ -44,7 +44,7 @@ struct http_parser {
*/
const char *hdr_value_start;
/**
* Pointer to header field value end (exlusive).
* Pointer to header field value end (exclusive).
*/
const char *hdr_value_end;
......
......@@ -6,12 +6,12 @@
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
struct http_parser parser;
char *buf = (char *)data;
const char *buf = (char *)data;
http_parser_create(&parser);
parser.hdr_name = (char *)calloc(size, sizeof(char));
if (parser.hdr_name == NULL)
return 0;
char *end_buf = buf + size;
const char *end_buf = buf + size;
http_parse_header_line(&parser, &buf, end_buf, size);
free(parser.hdr_name);
......
......@@ -178,6 +178,9 @@ target_link_libraries(csv.test csv)
add_executable(json.test json.c)
target_link_libraries(json.test json unit ${ICU_LIBRARIES})
add_executable(http_parser.test http_parser.c)
target_link_libraries(http_parser.test unit http_parser)
add_executable(rmean.test rmean.cc core_test_utils.c)
target_link_libraries(rmean.test stat unit)
add_executable(histogram.test histogram.c core_test_utils.c)
......
#include <stdio.h>
#include <string.h>
#include "trivia/util.h"
#include "http_parser/http_parser.h"
#define UNIT_TAP_COMPATIBLE 1
#include "unit.h"
static void
test_protocol_version(void)
{
static const struct {
const char *status;
int major;
int minor;
} tests[] = {
{ "HTTP/1.1 200\r\n", 1, 1 },
{ "HTTP/2.0 301\r\n", 2, 0 },
{ "HTTP/2 200\r\n", 2, 0 },
};
char buf[10];
plan(6);
header();
for (size_t i = 0; i < lengthof(tests); i++) {
struct http_parser p;
const char *l;
http_parser_create(&p);
p.hdr_name = buf;
l = tests[i].status;
http_parse_header_line(&p, &l, l + strlen(l), lengthof(buf));
is(tests[i].major, p.http_major,
"expected major number is '%d', received '%d' for '%s'",
tests[i].major, p.http_major, tests[i].status);
is(tests[i].minor, p.http_minor,
"expected minor number is '%d', received '%d' for '%s'",
tests[i].minor, p.http_minor, tests[i].status);
}
footer();
check_plan();
}
int
main(void)
{
plan(1);
test_protocol_version();
return check_plan();
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment