diff --git a/src/spec/http/CMakeLists.txt b/src/spec/http/CMakeLists.txt index 8174e2da3..c329566da 100644 --- a/src/spec/http/CMakeLists.txt +++ b/src/spec/http/CMakeLists.txt @@ -10,7 +10,7 @@ endif() sourcemeta_library(NAMESPACE sourcemeta PROJECT core NAME http PRIVATE_HEADERS problem.h status.h method.h message.h error.h system.h - aws_sigv4.h syntax.h tracecontext.h baggage.h + aws_sigv4.h syntax.h tracecontext.h baggage.h request.h SOURCES helpers.h entropy.h problem.cc match_accept.cc match_accept_language.cc negotiate_encoding.cc from_date.cc format_link.cc format_vary.cc field_list.cc @@ -22,7 +22,7 @@ sourcemeta_library(NAMESPACE sourcemeta PROJECT core NAME http make_traceparent.cc continue_traceparent.cc is_baggage.cc is_baggage_value.cc serialize_cache_control.cc - scan_quoted_string.cc + scan_quoted_string.cc parse_request_line_and_headers.cc ${SOURCEMETA_CORE_HTTP_CLIENT_SOURCE}) if(SOURCEMETA_CORE_INSTALL) diff --git a/src/spec/http/include/sourcemeta/core/http.h b/src/spec/http/include/sourcemeta/core/http.h index 81f410059..e91585d0f 100644 --- a/src/spec/http/include/sourcemeta/core/http.h +++ b/src/spec/http/include/sourcemeta/core/http.h @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include diff --git a/src/spec/http/include/sourcemeta/core/http_request.h b/src/spec/http/include/sourcemeta/core/http_request.h new file mode 100644 index 000000000..553559e16 --- /dev/null +++ b/src/spec/http/include/sourcemeta/core/http_request.h @@ -0,0 +1,64 @@ +#ifndef SOURCEMETA_CORE_HTTP_REQUEST_H_ +#define SOURCEMETA_CORE_HTTP_REQUEST_H_ + +#ifndef SOURCEMETA_CORE_HTTP_EXPORT +#include +#endif + +#include // std::size_t +#include // std::uint8_t +#include // std::string_view +#include // std::pair +#include // std::vector + +namespace sourcemeta::core { + +/// @ingroup http +/// Outcome of parsing an HTTP/1.1 request line and header section. +enum class HTTPRequestLineAndHeadersStatus : std::uint8_t { + /// The request line and all header fields were parsed. + Complete, + /// More input is needed to finish a line or find the terminating blank line. + Incomplete, + /// The parser rejected a line or its ending. + Malformed +}; + +/// @ingroup http +/// The parsed HTTP request line and header section. String views borrow the +/// input. +struct HTTPRequestLineAndHeaders { + /// The outcome of the parse. + HTTPRequestLineAndHeadersStatus status{ + HTTPRequestLineAndHeadersStatus::Incomplete}; + /// Bytes through the terminating blank line, or zero unless complete. + std::size_t consumed{0}; + /// The method token as sent, or an empty view unless complete. + std::string_view method{}; + /// The request target as sent, or an empty view unless complete. + std::string_view target{}; + /// Header field names and values in wire order, including repeated names. + std::vector> headers{}; +}; + +/// @ingroup http +/// Parse the HTTP/1.1 request line and header section using CRLF line endings +/// and single spaces in the request line (RFC 9112 Sections 2, 3 and 5). The +/// target is returned verbatim after checking that it consists of visible ASCII +/// characters. Header field values have leading and trailing optional +/// whitespace removed. Request-target forms, field-specific semantics +/// (including Host) and message body framing need separate validation before +/// serving a request. +/// +/// Complete consumes through the blank line. Incomplete means another line +/// or line ending is needed. Malformed means a line or its ending was rejected. +/// Both failure results have zero bytes consumed and empty headers. +/// Callers can append input and retry from the beginning when incomplete. +/// Keep the input alive and unmodified while using the returned views. +SOURCEMETA_CORE_HTTP_EXPORT +auto http_parse_request_line_and_headers(const std::string_view input) + -> HTTPRequestLineAndHeaders; + +} // namespace sourcemeta::core + +#endif diff --git a/src/spec/http/parse_request_line_and_headers.cc b/src/spec/http/parse_request_line_and_headers.cc new file mode 100644 index 000000000..a68e65fde --- /dev/null +++ b/src/spec/http/parse_request_line_and_headers.cc @@ -0,0 +1,81 @@ +#include +#include + +#include // std::ranges::all_of +#include // std::size_t +#include // std::string_view + +namespace sourcemeta::core { + +auto http_parse_request_line_and_headers(const std::string_view input) + -> HTTPRequestLineAndHeaders { + constexpr HTTPRequestLineAndHeadersStatus MALFORMED{ + HTTPRequestLineAndHeadersStatus::Malformed}; + HTTPRequestLineAndHeaders result; + std::size_t offset{0}; + while (true) { + const auto end{input.find_first_of("\r\n", offset)}; + if (end == std::string_view::npos) { + return {}; + } + + if (input[end] != '\r') { + return {.status = MALFORMED}; + } + + if (end + 1 == input.size()) { + return {}; + } + + if (input[end + 1] != '\n') { + return {.status = MALFORMED}; + } + + const auto line{input.substr(offset, end - offset)}; + if (offset == 0) { + const auto method_end{line.find(' ')}; + if (method_end == std::string_view::npos) { + return {.status = MALFORMED}; + } + + const auto target_start{method_end + 1}; + const auto target_end{line.find(' ', target_start)}; + if (target_end == std::string_view::npos || + line.substr(target_end + 1) != "HTTP/1.1") { + return {.status = MALFORMED}; + } + + result.method = line.substr(0, method_end); + result.target = line.substr(target_start, target_end - target_start); + if (!http_is_token(result.method) || result.target.empty() || + !std::ranges::all_of(result.target, [](const char byte) { + return byte >= '!' && byte <= '~'; + })) { + return {.status = MALFORMED}; + } + } else if (line.empty()) { + result.status = HTTPRequestLineAndHeadersStatus::Complete; + result.consumed = end + 2; + return result; + } else { + const auto colon{line.find(':')}; + if (colon == std::string_view::npos || + !http_is_token(line.substr(0, colon))) { + return {.status = MALFORMED}; + } + + const auto value{line.substr(colon + 1)}; + if (!std::ranges::all_of(value, http_is_field_octet)) { + return {.status = MALFORMED}; + } + + result.headers.emplace_back( + line.substr(0, colon), + http_trim_trailing_ows(http_trim_leading_ows(value))); + } + + offset = end + 2; + } +} + +} // namespace sourcemeta::core diff --git a/test/spec/http/CMakeLists.txt b/test/spec/http/CMakeLists.txt index 5487d5597..5f830915a 100644 --- a/test/spec/http/CMakeLists.txt +++ b/test/spec/http/CMakeLists.txt @@ -10,7 +10,8 @@ sourcemeta_test(NAMESPACE sourcemeta PROJECT core NAME http http_method_test.cc http_status_from_code_test.cc http_is_status_code_test.cc http_is_status_line_test.cc http_accumulate_header_line_test.cc - http_parse_headers_test.cc http_serialize_headers_test.cc + http_parse_headers_test.cc http_parse_request_line_and_headers_test.cc + http_serialize_headers_test.cc http_header_find_test.cc http_error_test.cc http_is_traceparent_test.cc http_parse_traceparent_test.cc http_is_tracestate_test.cc http_parse_tracestate_test.cc diff --git a/test/spec/http/http_parse_request_line_and_headers_test.cc b/test/spec/http/http_parse_request_line_and_headers_test.cc new file mode 100644 index 000000000..be422f4c7 --- /dev/null +++ b/test/spec/http/http_parse_request_line_and_headers_test.cc @@ -0,0 +1,157 @@ +#include +#include + +#include // std::string_view + +TEST( + request_line_and_headers_complete_preserves_method_target_and_field_order) { + constexpr std::string_view INPUT{ + "custom! /path?key=value HTTP/1.1\r\nHost: example.com\r\n" + "X-Value:\t one \t\r\nx-value:two\r\nEmpty: \t\r\n\r\n"}; + const auto result{ + sourcemeta::core::http_parse_request_line_and_headers(INPUT)}; + EXPECT_TRUE(result.status == + sourcemeta::core::HTTPRequestLineAndHeadersStatus::Complete); + EXPECT_EQ(result.consumed, INPUT.size()); + EXPECT_EQ(result.method, "custom!"); + EXPECT_EQ(result.target, "/path?key=value"); + EXPECT_EQ(result.headers.size(), 4); + EXPECT_EQ(result.headers.at(0).first, "Host"); + EXPECT_EQ(result.headers.at(0).second, "example.com"); + EXPECT_EQ(result.headers.at(1).first, "X-Value"); + EXPECT_EQ(result.headers.at(1).second, "one"); + EXPECT_EQ(result.headers.at(2).first, "x-value"); + EXPECT_EQ(result.headers.at(2).second, "two"); + EXPECT_TRUE(result.headers.at(3).second.empty()); +} + +TEST(request_line_and_headers_stops_before_body) { + constexpr std::string_view INPUT{ + "POST / HTTP/1.1\r\nHost: example.com\r\nContent-Length: 4\r\n\r\ndata"}; + const auto result{ + sourcemeta::core::http_parse_request_line_and_headers(INPUT)}; + EXPECT_TRUE(result.status == + sourcemeta::core::HTTPRequestLineAndHeadersStatus::Complete); + EXPECT_EQ(INPUT.substr(result.consumed), "data"); +} + +TEST(request_line_and_headers_empty_input_is_incomplete) { + const auto result{sourcemeta::core::http_parse_request_line_and_headers("")}; + EXPECT_TRUE(result.status == + sourcemeta::core::HTTPRequestLineAndHeadersStatus::Incomplete); + EXPECT_EQ(result.consumed, 0); +} + +TEST(request_line_and_headers_split_terminator_is_incomplete) { + const auto result{sourcemeta::core::http_parse_request_line_and_headers( + "GET / HTTP/1.1\r\nHost: example.com\r\n\r")}; + EXPECT_TRUE(result.status == + sourcemeta::core::HTTPRequestLineAndHeadersStatus::Incomplete); + EXPECT_EQ(result.consumed, 0); + EXPECT_TRUE(result.headers.empty()); +} + +TEST(request_line_and_headers_rejects_other_versions) { + const auto result{sourcemeta::core::http_parse_request_line_and_headers( + "GET / HTTP/1.0\r\n\r\n")}; + EXPECT_TRUE(result.status == + sourcemeta::core::HTTPRequestLineAndHeadersStatus::Malformed); + EXPECT_EQ(result.consumed, 0); +} + +TEST(request_line_and_headers_rejects_missing_target) { + const auto result{sourcemeta::core::http_parse_request_line_and_headers( + "GET HTTP/1.1\r\n\r\n")}; + EXPECT_TRUE(result.status == + sourcemeta::core::HTTPRequestLineAndHeadersStatus::Malformed); +} + +TEST(request_line_and_headers_rejects_bare_lf) { + const auto result{sourcemeta::core::http_parse_request_line_and_headers( + "GET / HTTP/1.1\n\n")}; + EXPECT_TRUE(result.status == + sourcemeta::core::HTTPRequestLineAndHeadersStatus::Malformed); +} + +TEST(request_line_and_headers_rejects_whitespace_before_colon) { + const auto result{sourcemeta::core::http_parse_request_line_and_headers( + "GET / HTTP/1.1\r\nHost : example.com\r\n\r\n")}; + EXPECT_TRUE(result.status == + sourcemeta::core::HTTPRequestLineAndHeadersStatus::Malformed); + EXPECT_EQ(result.consumed, 0); + EXPECT_TRUE(result.headers.empty()); +} + +TEST(request_line_and_headers_rejects_field_value_control_byte) { + const auto result{sourcemeta::core::http_parse_request_line_and_headers( + "GET / HTTP/1.1\r\nHost: example.com\r\nX-Value: \x01\r\n\r\n")}; + EXPECT_TRUE(result.status == + sourcemeta::core::HTTPRequestLineAndHeadersStatus::Malformed); + EXPECT_TRUE(result.headers.empty()); +} + +TEST(request_line_and_headers_rejects_obsolete_line_folding) { + const auto result{sourcemeta::core::http_parse_request_line_and_headers( + "GET / HTTP/1.1\r\nHost: example.com\r\n continued\r\n\r\n")}; + EXPECT_TRUE(result.status == + sourcemeta::core::HTTPRequestLineAndHeadersStatus::Malformed); +} + +TEST(request_line_and_headers_partial_request_line_is_incomplete) { + const auto result{ + sourcemeta::core::http_parse_request_line_and_headers("GET / HTTP/1.")}; + EXPECT_TRUE(result.status == + sourcemeta::core::HTTPRequestLineAndHeadersStatus::Incomplete); + EXPECT_EQ(result.consumed, 0); +} + +TEST(request_line_and_headers_partial_field_is_incomplete) { + const auto result{sourcemeta::core::http_parse_request_line_and_headers( + "GET / HTTP/1.1\r\nHost: example.com")}; + EXPECT_TRUE(result.status == + sourcemeta::core::HTTPRequestLineAndHeadersStatus::Incomplete); + EXPECT_EQ(result.consumed, 0); + EXPECT_TRUE(result.method.empty()); + EXPECT_TRUE(result.target.empty()); + EXPECT_TRUE(result.headers.empty()); +} + +TEST(request_line_and_headers_rejects_carriage_return_without_line_feed) { + const auto result{sourcemeta::core::http_parse_request_line_and_headers( + "GET / HTTP/1.1\rHost: example.com\r\n\r\n")}; + EXPECT_TRUE(result.status == + sourcemeta::core::HTTPRequestLineAndHeadersStatus::Malformed); +} + +// Adapted from llhttp's "Simple request" fixture (MIT): +// https://github.com/nodejs/llhttp/blob/85c5d42cd563d5426a478d32248a45ee81f4b635/test/request/sample.md#simple-request +TEST(llhttp_simple_request) { + constexpr std::string_view INPUT{ + "OPTIONS /url HTTP/1.1\r\nHeader1: Value1\r\nHeader2:\t Value2\r\n\r\n"}; + const auto result{ + sourcemeta::core::http_parse_request_line_and_headers(INPUT)}; + EXPECT_TRUE(result.status == + sourcemeta::core::HTTPRequestLineAndHeadersStatus::Complete); + EXPECT_EQ(result.consumed, INPUT.size()); + EXPECT_EQ(result.method, "OPTIONS"); + EXPECT_EQ(result.target, "/url"); + EXPECT_EQ(result.headers.size(), 2); + EXPECT_EQ(result.headers.at(0).first, "Header1"); + EXPECT_EQ(result.headers.at(0).second, "Value1"); + EXPECT_EQ(result.headers.at(1).first, "Header2"); + EXPECT_EQ(result.headers.at(1).second, "Value2"); +} + +// Adapted from the same suite's "No headers and no body" fixture. +TEST(llhttp_no_headers_and_no_body) { + constexpr std::string_view INPUT{ + "GET /get_no_headers_no_body/world HTTP/1.1\r\n\r\n"}; + const auto result{ + sourcemeta::core::http_parse_request_line_and_headers(INPUT)}; + EXPECT_TRUE(result.status == + sourcemeta::core::HTTPRequestLineAndHeadersStatus::Complete); + EXPECT_EQ(result.consumed, INPUT.size()); + EXPECT_EQ(result.method, "GET"); + EXPECT_EQ(result.target, "/get_no_headers_no_body/world"); + EXPECT_TRUE(result.headers.empty()); +}