Repository navigation
Add a standalone HTTP/1.1 request-head parser #3112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
HarshPopat23
wants to merge
5
commits into
sourcemeta:main
Choose a base branch
from
HarshPopat23:http-request-head-parser
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4d9cd79
Add a standalone HTTP/1.1 request-head parser
HarshPopat23 23d1605
Document HTTP request parser result members
HarshPopat23 c2db749
Use uppercase for the request parser constexpr
HarshPopat23 e4b92e9
Use uppercase constexpr names in HTTP parser tests
HarshPopat23 596e0ee
Align HTTP request parser terminology with RFC 9112
HarshPopat23 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| #ifndef SOURCEMETA_CORE_HTTP_REQUEST_H_ | ||
| #define SOURCEMETA_CORE_HTTP_REQUEST_H_ | ||
|
|
||
| #ifndef SOURCEMETA_CORE_HTTP_EXPORT | ||
| #include <sourcemeta/core/http_export.h> | ||
| #endif | ||
|
|
||
| #include <cstddef> // std::size_t | ||
| #include <cstdint> // std::uint8_t | ||
| #include <string_view> // std::string_view | ||
| #include <utility> // std::pair | ||
| #include <vector> // 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<std::pair<std::string_view, std::string_view>> 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| #include <sourcemeta/core/http_request.h> | ||
| #include <sourcemeta/core/http_syntax.h> | ||
|
|
||
| #include <algorithm> // std::ranges::all_of | ||
| #include <cstddef> // std::size_t | ||
| #include <string_view> // 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
157 changes: 157 additions & 0 deletions
157
test/spec/http/http_parse_request_line_and_headers_test.cc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| #include <sourcemeta/core/http_request.h> | ||
| #include <sourcemeta/core/test.h> | ||
|
|
||
| #include <string_view> // 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()); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any official suite somewhere we could use too? There must be some re-usable HTTP parsing test suites somewhere right?