summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/Fetch
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2022-07-10 19:28:47 +0200
committerLinus Groh <mail@linusgroh.de>2022-07-14 00:42:26 +0100
commit9244f1697d8f0c7ef6cfa9ffa77f7be84a6cfc5e (patch)
treee1dba1368a1cc7a2663f62bad7f8854453741653 /Userland/Libraries/LibWeb/Fetch
parent9d60010b443ec450001afc7eb3d8ca223d09085a (diff)
downloadserenity-9244f1697d8f0c7ef6cfa9ffa77f7be84a6cfc5e.zip
LibWeb: Add definitions from '2.2.1. Methods' in the Fetch spec
Diffstat (limited to 'Userland/Libraries/LibWeb/Fetch')
-rw-r--r--Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Methods.cpp48
-rw-r--r--Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Methods.h18
2 files changed, 66 insertions, 0 deletions
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 <linusg@serenityos.org>
+ * Copyright (c) 2022, Kenneth Myhra <kennethmyhra@serenityos.org>
+ *
+ * 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 {
+
+// 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<ECMA262Parser> 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<ByteBuffer> 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 <linusg@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/Forward.h>
+
+namespace Web::Fetch {
+
+[[nodiscard]] bool is_method(ReadonlyBytes);
+[[nodiscard]] bool is_cors_safelisted_method(ReadonlyBytes);
+[[nodiscard]] bool is_forbidden_method(ReadonlyBytes);
+[[nodiscard]] ErrorOr<ByteBuffer> normalize_method(ReadonlyBytes);
+
+}