LibHTTP+LibWeb+RequestServer: Move Fetch's HTTP header infra to LibHTTP

The end goal here is for LibHTTP to be the home of our RFC 9111 (HTTP
caching) implementation. We currently have one implementation in LibWeb
for our in-memory cache and another in RequestServer for our disk cache.

The implementations both largely revolve around interacting with HTTP
headers. But in LibWeb, we are using Fetch's header infra, and in RS we
are using are home-grown header infra from LibHTTP.

So to give these a common denominator, this patch replaces the LibHTTP
implementation with Fetch's infra. Our existing LibHTTP implementation
was not particularly compliant with any spec, so this at least gives us
a standards-based common implementation.

This migration also required moving a handful of other Fetch AOs over
to LibHTTP. (It turns out these AOs were all from the Fetch/Infra/HTTP
folder, so perhaps it makes sense for LibHTTP to be the implementation
of that entire set of facilities.)
This commit is contained in:
Timothy Flynn 2025-11-26 14:13:23 -05:00 committed by Jelle Raaijmakers
parent 3dce6766a3
commit 9375660b64
Notes: github-actions[bot] 2025-11-27 13:58:52 +00:00
78 changed files with 935 additions and 1017 deletions

View file

@ -11,6 +11,11 @@
namespace HTTP {
HttpRequest::HttpRequest(NonnullRefPtr<HeaderList> headers)
: m_headers(move(headers))
{
}
StringView to_string_view(HttpRequest::Method method)
{
switch (method) {
@ -60,8 +65,8 @@ ErrorOr<ByteBuffer> HttpRequest::to_raw_request() const
TRY(builder.try_appendff(":{}", *m_url.port()));
TRY(builder.try_append("\r\n"sv));
// Start headers.
bool has_content_length = m_headers.contains("Content-Length"sv);
for (auto const& [name, value] : m_headers.headers()) {
bool has_content_length = m_headers->contains("Content-Length"sv);
for (auto const& [name, value] : *m_headers) {
TRY(builder.try_append(name));
TRY(builder.try_append(": "sv));
TRY(builder.try_append(value));
@ -113,7 +118,7 @@ ErrorOr<HttpRequest, HttpRequest::ParseError> HttpRequest::from_raw_request(Read
ByteString method;
ByteString resource;
ByteString protocol;
HeaderMap headers;
auto headers = HeaderList::create();
Header current_header;
ByteBuffer body;
@ -180,7 +185,7 @@ ErrorOr<HttpRequest, HttpRequest::ParseError> HttpRequest::from_raw_request(Read
if (current_header.name.equals_ignoring_ascii_case("Content-Length"sv))
content_length = current_header.value.to_number<unsigned>();
headers.set(move(current_header.name), move(current_header.value));
headers->append({ move(current_header.name), move(current_header.value) });
break;
}
buffer.append(consume());
@ -207,7 +212,7 @@ ErrorOr<HttpRequest, HttpRequest::ParseError> HttpRequest::from_raw_request(Read
if (content_length.has_value() && content_length.value() != body.size())
return ParseError::RequestIncomplete;
HttpRequest request;
HttpRequest request { move(headers) };
if (method == "GET")
request.m_method = Method::GET;
else if (method == "HEAD")
@ -229,7 +234,6 @@ ErrorOr<HttpRequest, HttpRequest::ParseError> HttpRequest::from_raw_request(Read
else
return ParseError::UnsupportedMethod;
request.m_headers = move(headers);
auto url_parts = resource.split_limit('?', 2, SplitBehavior::KeepEmpty);
auto url_part_to_string = [](ByteString const& url_part) -> ErrorOr<String, ParseError> {
@ -258,11 +262,6 @@ ErrorOr<HttpRequest, HttpRequest::ParseError> HttpRequest::from_raw_request(Read
return request;
}
void HttpRequest::set_headers(HTTP::HeaderMap headers)
{
m_headers = move(headers);
}
Optional<Header> HttpRequest::get_http_basic_authentication_header(URL::URL const& url)
{
if (!url.includes_credentials())