ladybird/Libraries/LibHTTP/Method.cpp
Timothy Flynn 9375660b64 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.)
2025-11-27 14:57:29 +01:00

50 lines
1.6 KiB
C++

/*
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2022, Kenneth Myhra <kennethmyhra@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibHTTP/Method.h>
#include <LibRegex/Regex.h>
namespace HTTP {
// https://fetch.spec.whatwg.org/#concept-method
bool is_method(StringView method)
{
// A method is a byte sequence that matches the method token production.
Regex<ECMA262Parser> regex { R"~~~(^[A-Za-z0-9!#$%&'*+\-.^_`|~]+$)~~~" };
return regex.has_match(method);
}
// https://fetch.spec.whatwg.org/#cors-safelisted-method
bool is_cors_safelisted_method(StringView method)
{
// A CORS-safelisted method is a method that is `GET`, `HEAD`, or `POST`.
return method.is_one_of("GET"sv, "HEAD"sv, "POST"sv);
}
// https://fetch.spec.whatwg.org/#forbidden-method
bool is_forbidden_method(StringView method)
{
// A forbidden method is a method that is a byte-case-insensitive match for `CONNECT`, `TRACE`, or `TRACK`.
return method.is_one_of_ignoring_ascii_case("CONNECT"sv, "TRACE"sv, "TRACK"sv);
}
// https://fetch.spec.whatwg.org/#concept-method-normalize
ByteString normalize_method(StringView method)
{
// To normalize a method, if it is a byte-case-insensitive match for `DELETE`, `GET`, `HEAD`, `OPTIONS`, `POST`,
// or `PUT`, byte-uppercase it.
static auto NORMALIZED_METHODS = to_array<ByteString>({ "DELETE"sv, "GET"sv, "HEAD"sv, "OPTIONS"sv, "POST"sv, "PUT"sv });
for (auto const& normalized_method : NORMALIZED_METHODS) {
if (normalized_method.equals_ignoring_ascii_case(method))
return normalized_method;
}
return method;
}
}