Do uri len check after it is finalised

This commit is contained in:
cpq
2023-08-21 17:16:48 +01:00
parent e7459d91f9
commit 3256218fd2
2 changed files with 8 additions and 6 deletions
+4 -3
View File
@@ -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
+4 -3
View File
@@ -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