From 9244f1697d8f0c7ef6cfa9ffa77f7be84a6cfc5e Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Sun, 10 Jul 2022 19:28:47 +0200 Subject: [PATCH] LibWeb: Add definitions from '2.2.1. Methods' in the Fetch spec --- Meta/Lagom/CMakeLists.txt | 3 +- Userland/Libraries/LibWeb/CMakeLists.txt | 1 + .../Fetch/Infrastructure/HTTP/Methods.cpp | 48 +++++++++++++++++++ .../Fetch/Infrastructure/HTTP/Methods.h | 18 +++++++ 4 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Methods.cpp create mode 100644 Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Methods.h diff --git a/Meta/Lagom/CMakeLists.txt b/Meta/Lagom/CMakeLists.txt index 228138acb3..c74cbf7ba2 100644 --- a/Meta/Lagom/CMakeLists.txt +++ b/Meta/Lagom/CMakeLists.txt @@ -517,6 +517,7 @@ if (BUILD_LAGOM) file(GLOB LIBWEB_SOURCES CONFIGURE_DEPENDS "../../Userland/Libraries/LibWeb/*.cpp") file(GLOB LIBWEB_SUBDIR_SOURCES CONFIGURE_DEPENDS "../../Userland/Libraries/LibWeb/*/*.cpp") file(GLOB LIBWEB_SUBSUBDIR_SOURCES CONFIGURE_DEPENDS "../../Userland/Libraries/LibWeb/*/*/*.cpp") + file(GLOB LIBWEB_SUBSUBSUBDIR_SOURCES CONFIGURE_DEPENDS "../../Userland/Libraries/LibWeb/*/*/*/*.cpp") list(REMOVE_ITEM LIBWEB_SUBSUBDIR_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/../../Userland/Libraries/LibWeb/CSS/SyntaxHighlighter/SyntaxHighlighter.cpp") list(REMOVE_ITEM LIBWEB_SUBSUBDIR_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/../../Userland/Libraries/LibWeb/HTML/SyntaxHighlighter/SyntaxHighlighter.cpp") @@ -533,7 +534,7 @@ if (BUILD_LAGOM) ) lagom_lib(Web web - SOURCES ${LIBWEB_SOURCES} ${LIBWEB_SUBDIR_SOURCES} ${LIBWEB_SUBSUBDIR_SOURCES} ${LIBWEB_GENERATED_SOURCES} + SOURCES ${LIBWEB_SOURCES} ${LIBWEB_SUBDIR_SOURCES} ${LIBWEB_SUBSUBDIR_SOURCES} ${LIBWEB_SUBSUBSUBDIR_SOURCES} ${LIBWEB_GENERATED_SOURCES} LIBS LibMarkdown LibGemini LibGfx LibGL LibJS LibTextCodec LibWasm LibXML ) generate_js_wrappers(LibWeb) diff --git a/Userland/Libraries/LibWeb/CMakeLists.txt b/Userland/Libraries/LibWeb/CMakeLists.txt index ce4fa8a113..94196d9e5e 100644 --- a/Userland/Libraries/LibWeb/CMakeLists.txt +++ b/Userland/Libraries/LibWeb/CMakeLists.txt @@ -119,6 +119,7 @@ set(SOURCES Encoding/TextEncoder.cpp Fetch/Infrastructure/HTTP.cpp Fetch/Infrastructure/URL.cpp + Fetch/Infrastructure/HTTP/Methods.cpp FontCache.cpp Geometry/DOMRectList.cpp HTML/AttributeNames.cpp diff --git a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Methods.cpp b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Methods.cpp new file mode 100644 index 0000000000..4f62e896f1 --- /dev/null +++ b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Methods.cpp @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2022, Linus Groh + * Copyright (c) 2022, Kenneth Myhra + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include +#include +#include + +namespace Web::Fetch { + +// https://fetch.spec.whatwg.org/#concept-method +bool is_method(ReadonlyBytes method) +{ + // A method is a byte sequence that matches the method token production. + Regex regex { R"~~~(^[A-Za-z0-9!#$%&'*+\-.^_`|~]+$)~~~" }; + return regex.has_match(StringView { method }); +} + +// https://fetch.spec.whatwg.org/#cors-safelisted-method +bool is_cors_safelisted_method(ReadonlyBytes 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); +} + +// https://fetch.spec.whatwg.org/#forbidden-method +bool is_forbidden_method(ReadonlyBytes 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_case("CONNECT"sv, "TRACE"sv, "TRACK"sv); +} + +// https://fetch.spec.whatwg.org/#concept-method-normalize +ErrorOr normalize_method(ReadonlyBytes 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 = TRY(ByteBuffer::copy(method)); + if (StringView { method }.is_one_of_ignoring_case("DELETE"sv, "GET"sv, "HEAD"sv, "OPTIONS"sv, "POST"sv, "PUT"sv)) + Infra::byte_uppercase(bytes); + return bytes; +} + +} diff --git a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Methods.h b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Methods.h new file mode 100644 index 0000000000..4b77d3d5f7 --- /dev/null +++ b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Methods.h @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2022, Linus Groh + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include + +namespace Web::Fetch { + +[[nodiscard]] bool is_method(ReadonlyBytes); +[[nodiscard]] bool is_cors_safelisted_method(ReadonlyBytes); +[[nodiscard]] bool is_forbidden_method(ReadonlyBytes); +[[nodiscard]] ErrorOr normalize_method(ReadonlyBytes); + +}