From 886a29d659a979659d30ea195228fd51ffd67297 Mon Sep 17 00:00:00 2001 From: cpq Date: Sat, 29 May 2021 10:07:05 +0100 Subject: [PATCH] Fix #1292 - allow requests with missing protocol version, responses with no reason fields --- mongoose.c | 4 ++-- src/http.c | 4 ++-- test/unit_test.c | 8 +++++++- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/mongoose.c b/mongoose.c index d5d08d47..0bd177f7 100644 --- a/mongoose.c +++ b/mongoose.c @@ -620,8 +620,8 @@ int mg_http_parse(const char *s, size_t len, struct mg_http_message *hm) { s = skip(s, end, " ", &hm->uri); s = skip(s, end, "\r\n", &hm->proto); - // Sanity check - if (hm->method.len == 0 || hm->uri.len == 0 || hm->proto.len == 0) return -1; + // Sanity check. Allow protocol/reason to be empty + if (hm->method.len == 0 || hm->uri.len == 0) return -1; // If URI contains '?' character, setup query string if ((qs = (const char *) memchr(hm->uri.ptr, '?', hm->uri.len)) != NULL) { diff --git a/src/http.c b/src/http.c index 2e29530c..a26937b3 100644 --- a/src/http.c +++ b/src/http.c @@ -209,8 +209,8 @@ int mg_http_parse(const char *s, size_t len, struct mg_http_message *hm) { s = skip(s, end, " ", &hm->uri); s = skip(s, end, "\r\n", &hm->proto); - // Sanity check - if (hm->method.len == 0 || hm->uri.len == 0 || hm->proto.len == 0) return -1; + // Sanity check. Allow protocol/reason to be empty + if (hm->method.len == 0 || hm->uri.len == 0) return -1; // If URI contains '?' character, setup query string if ((qs = (const char *) memchr(hm->uri.ptr, '?', hm->uri.len)) != NULL) { diff --git a/test/unit_test.c b/test/unit_test.c index cfd459af..eac583d0 100644 --- a/test/unit_test.c +++ b/test/unit_test.c @@ -344,6 +344,8 @@ static void eh1(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { mg_http_reply(c, 200, "", "%.*s", (int) hm->body.len, hm->body.ptr); } else if (mg_http_match_uri(hm, "/bar")) { mg_http_reply(c, 404, "", "not found"); + } else if (mg_http_match_uri(hm, "/no_reason")) { + mg_printf(c, "%s", "HTTP/1.0 200\r\nContent-Length: 2\r\n\r\nok"); } else if (mg_http_match_uri(hm, "/badroot")) { struct mg_http_serve_opts sopts = {"/BAAADDD!", NULL, NULL}; mg_http_serve_dir(c, hm, &sopts); @@ -473,6 +475,10 @@ static void test_http_server(void) { ASSERT(fetch(&mgr, buf, url, "GET /%%61.txt HTTP/1.0\n\n") == 200); ASSERT(cmpbody(buf, "hello\n") == 0); + // Responses with missing reason phrase must also work + ASSERT(fetch(&mgr, buf, url, "GET /no_reason HTTP/1.0\n\n") == 200); + ASSERT(cmpbody(buf, "ok") == 0); + { extern char *mg_http_etag(char *, size_t, mg_stat_t *); char etag[100]; @@ -862,7 +868,7 @@ static void test_http_parse(void) { const char *s = "a b c\n\n"; ASSERT(mg_http_parse(s, strlen(s), &hm) == (int) strlen(s)); s = "a b\nc\n\n"; - ASSERT(mg_http_parse(s, strlen(s), &hm) < 0); + ASSERT(mg_http_parse(s, strlen(s), &hm) == (int) strlen(s)); s = "a\nb\nc\n\n"; ASSERT(mg_http_parse(s, strlen(s), &hm) < 0); }