2022-07-10 19:28:47 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
|
|
|
|
|
* Copyright (c) 2022, Kenneth Myhra <kennethmyhra@serenityos.org>
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
2025-11-26 14:13:23 -05:00
|
|
|
#include <LibHTTP/Method.h>
|
2022-07-10 19:28:47 +02:00
|
|
|
#include <LibRegex/Regex.h>
|
|
|
|
|
|
2025-11-26 14:13:23 -05:00
|
|
|
namespace HTTP {
|
2022-07-10 19:28:47 +02:00
|
|
|
|
|
|
|
|
// https://fetch.spec.whatwg.org/#concept-method
|
2025-11-24 18:35:55 -05:00
|
|
|
bool is_method(StringView method)
|
2022-07-10 19:28:47 +02:00
|
|
|
{
|
|
|
|
|
// A method is a byte sequence that matches the method token production.
|
|
|
|
|
Regex<ECMA262Parser> regex { R"~~~(^[A-Za-z0-9!#$%&'*+\-.^_`|~]+$)~~~" };
|
2025-11-24 18:35:55 -05:00
|
|
|
return regex.has_match(method);
|
2022-07-10 19:28:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://fetch.spec.whatwg.org/#cors-safelisted-method
|
2025-11-24 18:35:55 -05:00
|
|
|
bool is_cors_safelisted_method(StringView method)
|
2022-07-10 19:28:47 +02:00
|
|
|
{
|
|
|
|
|
// A CORS-safelisted method is a method that is `GET`, `HEAD`, or `POST`.
|
2025-11-24 18:35:55 -05:00
|
|
|
return method.is_one_of("GET"sv, "HEAD"sv, "POST"sv);
|
2022-07-10 19:28:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://fetch.spec.whatwg.org/#forbidden-method
|
2025-11-24 18:35:55 -05:00
|
|
|
bool is_forbidden_method(StringView method)
|
2022-07-10 19:28:47 +02:00
|
|
|
{
|
|
|
|
|
// A forbidden method is a method that is a byte-case-insensitive match for `CONNECT`, `TRACE`, or `TRACK`.
|
2025-11-24 18:35:55 -05:00
|
|
|
return method.is_one_of_ignoring_ascii_case("CONNECT"sv, "TRACE"sv, "TRACK"sv);
|
2022-07-10 19:28:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://fetch.spec.whatwg.org/#concept-method-normalize
|
2025-11-24 18:35:55 -05:00
|
|
|
ByteString normalize_method(StringView method)
|
2022-07-10 19:28:47 +02:00
|
|
|
{
|
2025-11-24 18:35:55 -05:00
|
|
|
// 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;
|
2022-07-10 19:28:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|