From 25de6ed8e40c3d201a496a00aa01330a6a2e941b Mon Sep 17 00:00:00 2001 From: Matt Klein Date: Wed, 18 Jul 2018 17:57:04 -0700 Subject: [PATCH] Fix multiple begin message cbs when response starts with CR/LF PR-URL: https://github.com/nodejs/http-parser/pull/432 Reviewed-By: Ben Noordhuis Reviewed-By: Jon Moss --- http_parser.c | 19 +++++++------------ test.c | 2 ++ 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/http_parser.c b/http_parser.c index db0830a..5403725 100644 --- a/http_parser.c +++ b/http_parser.c @@ -758,21 +758,16 @@ reexecute: case s_start_res: { + if (ch == CR || ch == LF) + break; parser->flags = 0; parser->content_length = ULLONG_MAX; - switch (ch) { - case 'H': - UPDATE_STATE(s_res_H); - break; - - case CR: - case LF: - break; - - default: - SET_ERRNO(HPE_INVALID_CONSTANT); - goto error; + if (ch == 'H') { + UPDATE_STATE(s_res_H); + } else { + SET_ERRNO(HPE_INVALID_CONSTANT); + goto error; } CALLBACK_NOTIFY(message_begin); diff --git a/test.c b/test.c index 5a1735b..f5be96d 100644 --- a/test.c +++ b/test.c @@ -2094,6 +2094,7 @@ int message_begin_cb (http_parser *p) { assert(p == &parser); + assert(!messages[num_messages].message_begin_cb_called); messages[num_messages].message_begin_cb_called = TRUE; return 0; } @@ -4189,6 +4190,7 @@ main (void) test_simple_type("HTTP/11.1 200 OK\r\n\r\n", HPE_INVALID_VERSION, HTTP_RESPONSE); test_simple_type("HTTP/1.01 200 OK\r\n\r\n", HPE_INVALID_VERSION, HTTP_RESPONSE); test_simple_type("HTTP/1.1\t200 OK\r\n\r\n", HPE_INVALID_VERSION, HTTP_RESPONSE); + test_simple_type("\rHTTP/1.1\t200 OK\r\n\r\n", HPE_INVALID_VERSION, HTTP_RESPONSE); for (i = 0; i < ARRAY_SIZE(responses); i++) { test_message(&responses[i]);