diff --git a/mongoose.c b/mongoose.c index 3b0fd692..76c8bd16 100644 --- a/mongoose.c +++ b/mongoose.c @@ -1493,9 +1493,6 @@ int mg_http_parse(const char *s, size_t len, struct mg_http_message *hm) { while (s < end && s[0] == ' ') s++; // Skip spaces if ((s = skiptorn(s, end, &hm->proto)) == NULL) return false; - // 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) { hm->query.ptr = qs + 1; @@ -1503,6 +1500,10 @@ int mg_http_parse(const char *s, size_t len, struct mg_http_message *hm) { hm->uri.len = (size_t) (qs - hm->uri.ptr); } + // Sanity check. Allow protocol/reason to be empty + // Do this check after hm->method.len and hm->uri.len are finalised + if (hm->method.len == 0 || hm->uri.len == 0) return -1; + if (!mg_http_parse_headers(s, end, hm->headers, sizeof(hm->headers) / sizeof(hm->headers[0]))) return -1; // error when parsing diff --git a/src/http.c b/src/http.c index 1ee482b0..73346b8b 100644 --- a/src/http.c +++ b/src/http.c @@ -257,9 +257,6 @@ int mg_http_parse(const char *s, size_t len, struct mg_http_message *hm) { while (s < end && s[0] == ' ') s++; // Skip spaces if ((s = skiptorn(s, end, &hm->proto)) == NULL) return false; - // 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) { hm->query.ptr = qs + 1; @@ -267,6 +264,10 @@ int mg_http_parse(const char *s, size_t len, struct mg_http_message *hm) { hm->uri.len = (size_t) (qs - hm->uri.ptr); } + // Sanity check. Allow protocol/reason to be empty + // Do this check after hm->method.len and hm->uri.len are finalised + if (hm->method.len == 0 || hm->uri.len == 0) return -1; + if (!mg_http_parse_headers(s, end, hm->headers, sizeof(hm->headers) / sizeof(hm->headers[0]))) return -1; // error when parsing