Skip to content

Truly independent web browser

LibWeb+LibWebCommon+LibWebView: Share Fetch's URL scheme checks

Shannon Boothb72f078ebc36
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/BlockingAlgorithms.cpp b/Libraries/LibWeb/ContentSecurityPolicy/BlockingAlgorithms.cpp
index f8cb381703f..43e00f9b1bf 100644
--- a/Libraries/LibWeb/ContentSecurityPolicy/BlockingAlgorithms.cpp
+++ b/Libraries/LibWeb/ContentSecurityPolicy/BlockingAlgorithms.cpp
@@ -16,7 +16,6 @@
#include <LibWeb/DOM/Element.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h>
-#include <LibWeb/Fetch/Infrastructure/URL.h>
#include <LibWeb/HTML/PolicyContainers.h>
#include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/HTML/Window.h>
@@ -25,6 +24,7 @@
#include <LibWeb/TrustedTypes/TrustedScript.h>
#include <LibWeb/TrustedTypes/TrustedTypePolicy.h>
#include <LibWeb/WebAssembly/WebAssembly.h>
+#include <LibWebCommon/Fetch/Infrastructure/URL.h>
#include <LibWebCommon/Infra/Strings.h>
namespace Web::ContentSecurityPolicy {
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/DirectiveOperations.cpp b/Libraries/LibWeb/ContentSecurityPolicy/Directives/DirectiveOperations.cpp
index 4a85434f4ea..11c8c7e6823 100644
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/DirectiveOperations.cpp
+++ b/Libraries/LibWeb/ContentSecurityPolicy/Directives/DirectiveOperations.cpp
@@ -19,13 +19,13 @@
#include <LibWeb/DOMURL/DOMURL.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h>
-#include <LibWeb/Fetch/Infrastructure/URL.h>
#include <LibWeb/HTML/HTMLScriptElement.h>
#include <LibWeb/HTML/HTMLStyleElement.h>
#include <LibWeb/SRI/SRI.h>
#include <LibWeb/SVG/SVGElement.h>
#include <LibWeb/SVG/SVGScriptElement.h>
#include <LibWeb/SVG/SVGStyleElement.h>
+#include <LibWebCommon/Fetch/Infrastructure/URL.h>
#include <LibWebCommon/Infra/Strings.h>
namespace Web::ContentSecurityPolicy::Directives {
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Directives/FrameAncestorsDirective.cpp b/Libraries/LibWeb/ContentSecurityPolicy/Directives/FrameAncestorsDirective.cpp
index fd39cbebee2..9d44c09d9ac 100644
--- a/Libraries/LibWeb/ContentSecurityPolicy/Directives/FrameAncestorsDirective.cpp
+++ b/Libraries/LibWeb/ContentSecurityPolicy/Directives/FrameAncestorsDirective.cpp
@@ -13,8 +13,8 @@
#include <LibWeb/DOMURL/DOMURL.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h>
-#include <LibWeb/Fetch/Infrastructure/URL.h>
#include <LibWeb/HTML/LocalNavigable.h>
+#include <LibWebCommon/Fetch/Infrastructure/URL.h>
namespace Web::ContentSecurityPolicy::Directives {
diff --git a/Libraries/LibWeb/ContentSecurityPolicy/Violation.cpp b/Libraries/LibWeb/ContentSecurityPolicy/Violation.cpp
index 7014cc86788..834ed1e0689 100644
--- a/Libraries/LibWeb/ContentSecurityPolicy/Violation.cpp
+++ b/Libraries/LibWeb/ContentSecurityPolicy/Violation.cpp
@@ -16,13 +16,13 @@
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOMURL/DOMURL.h>
#include <LibWeb/Fetch/Fetching/Fetching.h>
-#include <LibWeb/Fetch/Infrastructure/URL.h>
#include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/HTML/Scripting/TemporaryExecutionContext.h>
#include <LibWeb/HTML/Window.h>
#include <LibWeb/HTML/WorkerGlobalScope.h>
#include <LibWeb/HighResolutionTime/TimeOrigin.h>
#include <LibWeb/Infra/JSON.h>
+#include <LibWebCommon/Fetch/Infrastructure/URL.h>
namespace Web::ContentSecurityPolicy {
diff --git a/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp b/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp
index 5ccbf731548..6bb3a1ef5cb 100644
--- a/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp
+++ b/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp
@@ -75,6 +75,7 @@
#include <LibWeb/WebIDL/DOMException.h>
#include <LibWeb/WebIDL/ExceptionOrUtils.h>
#include <LibWebCommon/Fetch/Infrastructure/HTTP/Statuses.h>
+#include <LibWebCommon/Fetch/Infrastructure/URL.h>
#include <LibWebCommon/Loader/UserAgent.h>
#include <LibWebCommon/SecureContexts/AbstractOperations.h>
diff --git a/Libraries/LibWeb/Fetch/Infrastructure/PortBlocking.cpp b/Libraries/LibWeb/Fetch/Infrastructure/PortBlocking.cpp
index eb27333fe14..1a917b70a82 100644
--- a/Libraries/LibWeb/Fetch/Infrastructure/PortBlocking.cpp
+++ b/Libraries/LibWeb/Fetch/Infrastructure/PortBlocking.cpp
@@ -7,7 +7,7 @@
#include <LibHTTP/Port.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
#include <LibWeb/Fetch/Infrastructure/PortBlocking.h>
-#include <LibWeb/Fetch/Infrastructure/URL.h>
+#include <LibWebCommon/Fetch/Infrastructure/URL.h>
namespace Web::Fetch::Infrastructure {
diff --git a/Libraries/LibWeb/Fetch/Infrastructure/URL.cpp b/Libraries/LibWeb/Fetch/Infrastructure/URL.cpp
index afb1ed40501..afcec9f9e1f 100644
--- a/Libraries/LibWeb/Fetch/Infrastructure/URL.cpp
+++ b/Libraries/LibWeb/Fetch/Infrastructure/URL.cpp
@@ -15,27 +15,6 @@
namespace Web::Fetch::Infrastructure {
-// https://fetch.spec.whatwg.org/#is-local
-bool is_local_url(URL::URL const& url)
-{
- // A URL is local if its scheme is a local scheme.
- return any_of(LOCAL_SCHEMES, [&](auto scheme) { return url.scheme() == scheme; });
-}
-
-// https://fetch.spec.whatwg.org/#fetch-scheme
-bool is_fetch_scheme(StringView scheme)
-{
- // A fetch scheme is "about", "blob", "data", "file", or an HTTP(S) scheme.
- return any_of(FETCH_SCHEMES, [&](auto fetch_scheme) { return scheme == fetch_scheme; });
-}
-
-// https://fetch.spec.whatwg.org/#http-scheme
-bool is_http_or_https_scheme(StringView scheme)
-{
- // An HTTP(S) scheme is "http" or "https".
- return any_of(HTTP_SCHEMES, [&](auto http_scheme) { return scheme == http_scheme; });
-}
-
// https://fetch.spec.whatwg.org/#data-url-processor
ErrorOr<DataURL> process_data_url(URL::URL const& data_url)
{
diff --git a/Libraries/LibWeb/Fetch/Infrastructure/URL.h b/Libraries/LibWeb/Fetch/Infrastructure/URL.h
index 4e855dc46aa..acecc30d138 100644
--- a/Libraries/LibWeb/Fetch/Infrastructure/URL.h
+++ b/Libraries/LibWeb/Fetch/Infrastructure/URL.h
@@ -7,47 +7,20 @@
#pragma once
-#include <AK/Array.h>
#include <AK/ByteBuffer.h>
#include <AK/Error.h>
-#include <AK/String.h>
-#include <AK/StringView.h>
#include <LibURL/Forward.h>
#include <LibWeb/Export.h>
#include <LibWebCommon/MimeSniff/MimeType.h>
namespace Web::Fetch::Infrastructure {
-// https://fetch.spec.whatwg.org/#local-scheme
-// A local scheme is "about", "blob", or "data".
-inline constexpr Array LOCAL_SCHEMES = {
- "about"sv, "blob"sv, "data"sv
-};
-
-// https://fetch.spec.whatwg.org/#http-scheme
-// An HTTP(S) scheme is "http" or "https".
-inline constexpr Array HTTP_SCHEMES = {
- "http"sv, "https"sv
-};
-
-// https://fetch.spec.whatwg.org/#fetch-scheme
-// A fetch scheme is "about", "blob", "data", "file", or an HTTP(S) scheme.
-inline constexpr Array FETCH_SCHEMES = {
- "about"sv, "blob"sv, "data"sv, "file"sv, "http"sv, "https"sv,
-
- // AD-HOC: Internal fetch schemes:
- "resource"sv
-};
-
// https://fetch.spec.whatwg.org/#data-url-struct
struct DataURL {
MimeSniff::MimeType mime_type;
ByteBuffer body;
};
-[[nodiscard]] WEB_API bool is_local_url(URL::URL const&);
-[[nodiscard]] bool is_fetch_scheme(StringView);
-[[nodiscard]] WEB_API bool is_http_or_https_scheme(StringView);
WEB_API ErrorOr<DataURL> process_data_url(URL::URL const&);
}
diff --git a/Libraries/LibWeb/HTML/LocalNavigable.cpp b/Libraries/LibWeb/HTML/LocalNavigable.cpp
index 4a79bedee81..634302831f8 100644
--- a/Libraries/LibWeb/HTML/LocalNavigable.cpp
+++ b/Libraries/LibWeb/HTML/LocalNavigable.cpp
@@ -45,7 +45,6 @@
#include <LibWeb/Fetch/Infrastructure/FetchAlgorithms.h>
#include <LibWeb/Fetch/Infrastructure/FetchController.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
-#include <LibWeb/Fetch/Infrastructure/URL.h>
#include <LibWeb/HTML/BrowsingContext.h>
#include <LibWeb/HTML/CheckUnloadingCanceledState.h>
#include <LibWeb/HTML/DocumentState.h>
@@ -99,6 +98,7 @@
#include <LibWeb/WebIDL/Promise.h>
#include <LibWeb/XHR/FormData.h>
#include <LibWebCommon/CSS/SystemColor.h>
+#include <LibWebCommon/Fetch/Infrastructure/URL.h>
#include <LibWebCommon/HTML/CrossOrigin/OpenerPolicyEnforcement.h>
#include <LibWebCommon/HTML/HistoryHandlingBehavior.h>
#include <LibWebCommon/HTML/NavigationPopulationRequest.h>
diff --git a/Libraries/LibWeb/HTML/PolicyContainers.cpp b/Libraries/LibWeb/HTML/PolicyContainers.cpp
index 9104cb69339..b1005534973 100644
--- a/Libraries/LibWeb/HTML/PolicyContainers.cpp
+++ b/Libraries/LibWeb/HTML/PolicyContainers.cpp
@@ -10,9 +10,9 @@
#include <LibWeb/ContentSecurityPolicy/Policy.h>
#include <LibWeb/ContentSecurityPolicy/PolicyList.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h>
-#include <LibWeb/Fetch/Infrastructure/URL.h>
#include <LibWeb/HTML/PolicyContainers.h>
#include <LibWeb/ReferrerPolicy/AbstractOperations.h>
+#include <LibWebCommon/Fetch/Infrastructure/URL.h>
#include <LibWebCommon/HTML/SerializedPolicyContainer.h>
namespace Web::HTML {
diff --git a/Libraries/LibWeb/HTML/Scripting/Fetching.cpp b/Libraries/LibWeb/HTML/Scripting/Fetching.cpp
index b83391d2b08..6345cd8bacf 100644
--- a/Libraries/LibWeb/HTML/Scripting/Fetching.cpp
+++ b/Libraries/LibWeb/HTML/Scripting/Fetching.cpp
@@ -34,7 +34,6 @@
#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h>
#include <LibWeb/Fetch/Infrastructure/NetworkPartitionKey.h>
-#include <LibWeb/Fetch/Infrastructure/URL.h>
#include <LibWeb/HTML/EventLoop/EventLoop.h>
#include <LibWeb/HTML/HTMLScriptElement.h>
#include <LibWeb/HTML/PotentialCORSRequest.h>
@@ -46,6 +45,7 @@
#include <LibWeb/HTML/Window.h>
#include <LibWeb/Infra/SerializedURL.h>
#include <LibWeb/Loader/ResourceLoader.h>
+#include <LibWebCommon/Fetch/Infrastructure/URL.h>
#include <LibWebCommon/Infra/Strings.h>
#include <LibWebCommon/MimeSniff/MimeType.h>
diff --git a/Libraries/LibWeb/HTML/WorkerGlobalScope.cpp b/Libraries/LibWeb/HTML/WorkerGlobalScope.cpp
index e89f1e8828c..d1261cd4510 100644
--- a/Libraries/LibWeb/HTML/WorkerGlobalScope.cpp
+++ b/Libraries/LibWeb/HTML/WorkerGlobalScope.cpp
@@ -12,7 +12,6 @@
#include <LibWeb/ContentSecurityPolicy/Directives/Directive.h>
#include <LibWeb/ContentSecurityPolicy/Policy.h>
#include <LibWeb/ContentSecurityPolicy/PolicyList.h>
-#include <LibWeb/Fetch/Infrastructure/URL.h>
#include <LibWeb/HTML/EventHandler.h>
#include <LibWeb/HTML/EventNames.h>
#include <LibWeb/HTML/MessageEvent.h>
@@ -26,6 +25,7 @@
#include <LibWeb/HTML/WorkerNavigator.h>
#include <LibWeb/Page/Page.h>
#include <LibWeb/ServiceWorker/ServiceWorkerGlobalScope.h>
+#include <LibWebCommon/Fetch/Infrastructure/URL.h>
namespace Web::HTML {
diff --git a/Libraries/LibWeb/ReferrerPolicy/AbstractOperations.cpp b/Libraries/LibWeb/ReferrerPolicy/AbstractOperations.cpp
index 9e33af401ae..0c51303d1ff 100644
--- a/Libraries/LibWeb/ReferrerPolicy/AbstractOperations.cpp
+++ b/Libraries/LibWeb/ReferrerPolicy/AbstractOperations.cpp
@@ -10,11 +10,11 @@
#include <LibWeb/DOMURL/DOMURL.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h>
-#include <LibWeb/Fetch/Infrastructure/URL.h>
#include <LibWeb/HTML/Scripting/EnvironmentSettingsSnapshot.h>
#include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/HTML/Window.h>
#include <LibWeb/ReferrerPolicy/AbstractOperations.h>
+#include <LibWebCommon/Fetch/Infrastructure/URL.h>
#include <LibWebCommon/ReferrerPolicy/ReferrerPolicy.h>
#include <LibWebCommon/SecureContexts/AbstractOperations.h>
diff --git a/Libraries/LibWebCommon/CMakeLists.txt b/Libraries/LibWebCommon/CMakeLists.txt
index 3e1d5757ee6..efaa4607c9f 100644
--- a/Libraries/LibWebCommon/CMakeLists.txt
+++ b/Libraries/LibWebCommon/CMakeLists.txt
@@ -6,6 +6,7 @@ set(SOURCES
CSS/StyleSheetIdentifier.cpp
CSS/SystemColor.cpp
Fetch/Infrastructure/HTTP/Statuses.cpp
+ Fetch/Infrastructure/URL.cpp
FileAPI/BlobURLStore.cpp
FileAPI/SerializedBlobURLEntry.cpp
Gamepad/GamepadSharedState.cpp
diff --git a/Libraries/LibWebCommon/Fetch/Infrastructure/URL.cpp b/Libraries/LibWebCommon/Fetch/Infrastructure/URL.cpp
new file mode 100644
index 00000000000..e18e88f1dff
--- /dev/null
+++ b/Libraries/LibWebCommon/Fetch/Infrastructure/URL.cpp
@@ -0,0 +1,35 @@
+/*
+ * Copyright (c) 2022, Linus Groh <[email protected]>
+ * Copyright (c) 2022, Andreas Kling <[email protected]>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <AK/AnyOf.h>
+#include <LibURL/URL.h>
+#include <LibWebCommon/Fetch/Infrastructure/URL.h>
+
+namespace Web::Fetch::Infrastructure {
+
+// https://fetch.spec.whatwg.org/#is-local
+bool is_local_url(URL::URL const& url)
+{
+ // A URL is local if its scheme is a local scheme.
+ return any_of(LOCAL_SCHEMES, [&](auto scheme) { return url.scheme() == scheme; });
+}
+
+// https://fetch.spec.whatwg.org/#fetch-scheme
+bool is_fetch_scheme(StringView scheme)
+{
+ // A fetch scheme is "about", "blob", "data", "file", or an HTTP(S) scheme.
+ return any_of(FETCH_SCHEMES, [&](auto fetch_scheme) { return scheme == fetch_scheme; });
+}
+
+// https://fetch.spec.whatwg.org/#http-scheme
+bool is_http_or_https_scheme(StringView scheme)
+{
+ // An HTTP(S) scheme is "http" or "https".
+ return any_of(HTTP_SCHEMES, [&](auto http_scheme) { return scheme == http_scheme; });
+}
+
+}
diff --git a/Libraries/LibWebCommon/Fetch/Infrastructure/URL.h b/Libraries/LibWebCommon/Fetch/Infrastructure/URL.h
new file mode 100644
index 00000000000..5652da52b75
--- /dev/null
+++ b/Libraries/LibWebCommon/Fetch/Infrastructure/URL.h
@@ -0,0 +1,42 @@
+/*
+ * Copyright (c) 2022, Linus Groh <[email protected]>
+ * Copyright (c) 2022, Andreas Kling <[email protected]>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/Array.h>
+#include <AK/StringView.h>
+#include <LibURL/Forward.h>
+#include <LibWebCommon/Export.h>
+
+namespace Web::Fetch::Infrastructure {
+
+// https://fetch.spec.whatwg.org/#local-scheme
+// A local scheme is "about", "blob", or "data".
+inline constexpr Array LOCAL_SCHEMES = {
+ "about"sv, "blob"sv, "data"sv
+};
+
+// https://fetch.spec.whatwg.org/#http-scheme
+// An HTTP(S) scheme is "http" or "https".
+inline constexpr Array HTTP_SCHEMES = {
+ "http"sv, "https"sv
+};
+
+// https://fetch.spec.whatwg.org/#fetch-scheme
+// A fetch scheme is "about", "blob", "data", "file", or an HTTP(S) scheme.
+inline constexpr Array FETCH_SCHEMES = {
+ "about"sv, "blob"sv, "data"sv, "file"sv, "http"sv, "https"sv,
+
+ // AD-HOC: Internal fetch schemes:
+ "resource"sv
+};
+
+[[nodiscard]] WEBCOMMON_API bool is_local_url(URL::URL const&);
+[[nodiscard]] WEBCOMMON_API bool is_fetch_scheme(StringView);
+[[nodiscard]] WEBCOMMON_API bool is_http_or_https_scheme(StringView);
+
+}
diff --git a/Libraries/LibWebView/URL.cpp b/Libraries/LibWebView/URL.cpp
index fbd5cb05b4a..13e77d36866 100644
--- a/Libraries/LibWebView/URL.cpp
+++ b/Libraries/LibWebView/URL.cpp
@@ -14,6 +14,7 @@
#include <LibFileSystem/FileSystem.h>
#include <LibURL/Parser.h>
#include <LibURL/PublicSuffixData.h>
+#include <LibWebCommon/Fetch/Infrastructure/URL.h>
#include <LibWebView/Application.h>
#include <LibWebView/Autocomplete.h>
#include <LibWebView/ExternalURLHandler.h>
@@ -23,7 +24,7 @@ namespace WebView {
bool is_url_handled_internally(URL::URL const& url)
{
- return url.scheme().is_one_of("about"sv, "blob"sv, "data"sv, "file"sv, "http"sv, "https"sv, "resource"sv);
+ return Web::Fetch::Infrastructure::is_fetch_scheme(url.scheme());
}
static bool has_valid_explicit_port(StringView input)