diff --git a/Userland/Libraries/LibHTTP/HttpRequest.cpp b/Userland/Libraries/LibHTTP/HttpRequest.cpp index 4b16366c75..cbf3319619 100644 --- a/Userland/Libraries/LibHTTP/HttpRequest.cpp +++ b/Userland/Libraries/LibHTTP/HttpRequest.cpp @@ -102,6 +102,7 @@ ErrorOr HttpRequest::from_raw_request(Read Vector buffer; + Optional content_length; DeprecatedString method; DeprecatedString resource; DeprecatedString protocol; @@ -168,6 +169,10 @@ ErrorOr HttpRequest::from_raw_request(Read } commit_and_advance_to(current_header.value, next_state); + + if (current_header.name.equals_ignoring_ascii_case("Content-Length"sv)) + content_length = current_header.value.to_uint(); + headers.append(move(current_header)); break; } @@ -189,6 +194,12 @@ ErrorOr HttpRequest::from_raw_request(Read } } + if (state != State::InBody) + return ParseError::RequestIncomplete; + + if (content_length.has_value() && content_length.value() != body.size()) + return ParseError::RequestIncomplete; + HttpRequest request; if (method == "GET") request.m_method = Method::GET; diff --git a/Userland/Libraries/LibHTTP/HttpRequest.h b/Userland/Libraries/LibHTTP/HttpRequest.h index 29b648cdc8..fa66ee4788 100644 --- a/Userland/Libraries/LibHTTP/HttpRequest.h +++ b/Userland/Libraries/LibHTTP/HttpRequest.h @@ -20,6 +20,7 @@ class HttpRequest { public: enum class ParseError { RequestTooLarge, + RequestIncomplete, OutOfMemory, UnsupportedMethod }; @@ -29,6 +30,8 @@ public: switch (error) { case ParseError::RequestTooLarge: return "Request too large"sv; + case ParseError::RequestIncomplete: + return "Request is incomplete"sv; case ParseError::OutOfMemory: return "Out of memory"sv; case ParseError::UnsupportedMethod: