mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-12-08 06:09:58 +00:00
LibWeb: Store HTTP methods and headers as ByteString
The spec declares these as a byte sequence, which we then implemented as a ByteBuffer. This has become pretty awkward to deal with, as evidenced by the plethora of `MUST(ByteBuffer::copy(...))` and `.bytes()` calls everywhere inside Fetch. We would then treat the bytes as a string anyways by wrapping them in StringView everywhere. We now store these as a ByteString. This is more comfortable to deal with, and we no longer need to continually copy underlying storage (as ByteString is ref-counted). This work is largely preparatory for an upcoming HTTP header refactor.
This commit is contained in:
parent
ed27eea091
commit
f675cfe90f
Notes:
github-actions[bot]
2025-11-26 14:16:12 +00:00
Author: https://github.com/trflynn89
Commit: f675cfe90f
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/6933
28 changed files with 480 additions and 651 deletions
|
|
@ -5,44 +5,46 @@
|
|||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/ByteBuffer.h>
|
||||
#include <AK/StringView.h>
|
||||
#include <LibRegex/Regex.h>
|
||||
#include <LibWeb/Fetch/Infrastructure/HTTP/Methods.h>
|
||||
#include <LibWeb/Infra/ByteSequences.h>
|
||||
|
||||
namespace Web::Fetch::Infrastructure {
|
||||
|
||||
// https://fetch.spec.whatwg.org/#concept-method
|
||||
bool is_method(ReadonlyBytes 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(StringView { method });
|
||||
return regex.has_match(method);
|
||||
}
|
||||
|
||||
// https://fetch.spec.whatwg.org/#cors-safelisted-method
|
||||
bool is_cors_safelisted_method(ReadonlyBytes method)
|
||||
bool is_cors_safelisted_method(StringView method)
|
||||
{
|
||||
// A CORS-safelisted method is a method that is `GET`, `HEAD`, or `POST`.
|
||||
return StringView { method }.is_one_of("GET"sv, "HEAD"sv, "POST"sv);
|
||||
return method.is_one_of("GET"sv, "HEAD"sv, "POST"sv);
|
||||
}
|
||||
|
||||
// https://fetch.spec.whatwg.org/#forbidden-method
|
||||
bool is_forbidden_method(ReadonlyBytes 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 StringView { method }.is_one_of_ignoring_ascii_case("CONNECT"sv, "TRACE"sv, "TRACK"sv);
|
||||
return method.is_one_of_ignoring_ascii_case("CONNECT"sv, "TRACE"sv, "TRACK"sv);
|
||||
}
|
||||
|
||||
// https://fetch.spec.whatwg.org/#concept-method-normalize
|
||||
ByteBuffer normalize_method(ReadonlyBytes method)
|
||||
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.
|
||||
auto bytes = MUST(ByteBuffer::copy(method));
|
||||
if (StringView { method }.is_one_of_ignoring_ascii_case("DELETE"sv, "GET"sv, "HEAD"sv, "OPTIONS"sv, "POST"sv, "PUT"sv))
|
||||
Infra::byte_uppercase(bytes);
|
||||
return bytes;
|
||||
// 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue