diff --git a/Meta/gn/secondary/Userland/Libraries/LibWeb/Fetch/Infrastructure/BUILD.gn b/Meta/gn/secondary/Userland/Libraries/LibWeb/Fetch/Infrastructure/BUILD.gn index 3543f9fa430..4a9e2e733f1 100644 --- a/Meta/gn/secondary/Userland/Libraries/LibWeb/Fetch/Infrastructure/BUILD.gn +++ b/Meta/gn/secondary/Userland/Libraries/LibWeb/Fetch/Infrastructure/BUILD.gn @@ -13,6 +13,7 @@ source_set("Infrastructure") { "HTTP.cpp", "IncrementalReadLoopReadRequest.cpp", "MimeTypeBlocking.cpp", + "NetworkPartitionKey.cpp", "NoSniffBlocking.cpp", "PortBlocking.cpp", "Task.cpp", diff --git a/Userland/Libraries/LibWeb/CMakeLists.txt b/Userland/Libraries/LibWeb/CMakeLists.txt index fccc8702358..b2cae72a4d0 100644 --- a/Userland/Libraries/LibWeb/CMakeLists.txt +++ b/Userland/Libraries/LibWeb/CMakeLists.txt @@ -225,6 +225,7 @@ set(SOURCES Fetch/Infrastructure/HTTP/Statuses.cpp Fetch/Infrastructure/IncrementalReadLoopReadRequest.cpp Fetch/Infrastructure/MimeTypeBlocking.cpp + Fetch/Infrastructure/NetworkPartitionKey.cpp Fetch/Infrastructure/NoSniffBlocking.cpp Fetch/Infrastructure/PortBlocking.cpp Fetch/Infrastructure/Task.cpp diff --git a/Userland/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp b/Userland/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp index 10a968df4c1..25a99c00894 100644 --- a/Userland/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp +++ b/Userland/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -1250,30 +1251,6 @@ WebIDL::ExceptionOr> http_redirect_fetch(JS::Realm& r return main_fetch(realm, fetch_params, recursive); } -// https://fetch.spec.whatwg.org/#network-partition-key -struct NetworkPartitionKey { - HTML::Origin top_level_origin; - // FIXME: See https://github.com/whatwg/fetch/issues/1035 - // This is the document origin in other browsers - void* second_key = nullptr; - - bool operator==(NetworkPartitionKey const&) const = default; -}; - -} - -// FIXME: Take this with us to the eventual header these structs end up in to avoid closing and re-opening the namespace. -template<> -class AK::Traits : public DefaultTraits { -public: - static unsigned hash(Web::Fetch::Fetching::NetworkPartitionKey const& partition_key) - { - return ::AK::Traits::hash(partition_key.top_level_origin); - } -}; - -namespace Web::Fetch::Fetching { - struct CachedResponse { Vector headers; ByteBuffer body; @@ -1305,7 +1282,7 @@ private: class HTTPCache { public: - CachePartition& get(NetworkPartitionKey const& key) + CachePartition& get(Infrastructure::NetworkPartitionKey const& key) { return *m_cache.ensure(key, [] { return make(); @@ -1319,48 +1296,14 @@ public: } private: - HashMap> m_cache; + HashMap> m_cache; }; -// https://fetch.spec.whatwg.org/#determine-the-network-partition-key -static NetworkPartitionKey determine_the_network_partition_key(HTML::Environment const& environment) -{ - // 1. Let topLevelOrigin be environment’s top-level origin. - auto top_level_origin = environment.top_level_origin; - - // FIXME: 2. If topLevelOrigin is null, then set topLevelOrigin to environment’s top-level creation URL’s origin - // This field is supposed to be nullable - - // 3. Assert: topLevelOrigin is an origin. - - // FIXME: 4. Let topLevelSite be the result of obtaining a site, given topLevelOrigin. - - // 5. Let secondKey be null or an implementation-defined value. - void* second_key = nullptr; - - // 6. Return (topLevelSite, secondKey). - return { top_level_origin, second_key }; -} - -// https://fetch.spec.whatwg.org/#request-determine-the-network-partition-key -static Optional determine_the_network_partition_key(Infrastructure::Request const& request) -{ - // 1. If request’s reserved client is non-null, then return the result of determining the network partition key given request’s reserved client. - if (auto reserved_client = request.reserved_client()) - return determine_the_network_partition_key(*reserved_client); - - // 2. If request’s client is non-null, then return the result of determining the network partition key given request’s client. - if (auto client = request.client()) - return determine_the_network_partition_key(*client); - - return {}; -} - // https://fetch.spec.whatwg.org/#determine-the-http-cache-partition static Optional determine_the_http_cache_partition(Infrastructure::Request const& request) { // 1. Let key be the result of determining the network partition key given request. - auto key = determine_the_network_partition_key(request); + auto key = Infrastructure::determine_the_network_partition_key(request); // 2. If key is null, then return null. if (!key.has_value()) diff --git a/Userland/Libraries/LibWeb/Fetch/Infrastructure/NetworkPartitionKey.cpp b/Userland/Libraries/LibWeb/Fetch/Infrastructure/NetworkPartitionKey.cpp new file mode 100644 index 00000000000..89900917b59 --- /dev/null +++ b/Userland/Libraries/LibWeb/Fetch/Infrastructure/NetworkPartitionKey.cpp @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2024, Andrew Kaster + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include + +namespace Web::Fetch::Infrastructure { + +// https://fetch.spec.whatwg.org/#determine-the-network-partition-key +NetworkPartitionKey determine_the_network_partition_key(HTML::Environment const& environment) +{ + // 1. Let topLevelOrigin be environment’s top-level origin. + auto top_level_origin = environment.top_level_origin; + + // FIXME: 2. If topLevelOrigin is null, then set topLevelOrigin to environment’s top-level creation URL’s origin + // This field is supposed to be nullable + + // 3. Assert: topLevelOrigin is an origin. + + // FIXME: 4. Let topLevelSite be the result of obtaining a site, given topLevelOrigin. + + // 5. Let secondKey be null or an implementation-defined value. + void* second_key = nullptr; + + // 6. Return (topLevelSite, secondKey). + return { top_level_origin, second_key }; +} + +// https://fetch.spec.whatwg.org/#request-determine-the-network-partition-key +Optional determine_the_network_partition_key(Infrastructure::Request const& request) +{ + // 1. If request’s reserved client is non-null, then return the result of determining the network partition key given request’s reserved client. + if (auto reserved_client = request.reserved_client()) + return determine_the_network_partition_key(*reserved_client); + + // 2. If request’s client is non-null, then return the result of determining the network partition key given request’s client. + if (auto client = request.client()) + return determine_the_network_partition_key(*client); + + return {}; +} + +} diff --git a/Userland/Libraries/LibWeb/Fetch/Infrastructure/NetworkPartitionKey.h b/Userland/Libraries/LibWeb/Fetch/Infrastructure/NetworkPartitionKey.h new file mode 100644 index 00000000000..bbd053b63dc --- /dev/null +++ b/Userland/Libraries/LibWeb/Fetch/Infrastructure/NetworkPartitionKey.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2024, Andrew Kaster + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include + +namespace Web::Fetch::Infrastructure { + +// https://fetch.spec.whatwg.org/#network-partition-key +struct NetworkPartitionKey { + HTML::Origin top_level_origin; + // FIXME: See https://github.com/whatwg/fetch/issues/1035 + // This is the document origin in other browsers + void* second_key = nullptr; + + bool operator==(NetworkPartitionKey const&) const = default; +}; + +NetworkPartitionKey determine_the_network_partition_key(HTML::Environment const& environment); + +Optional determine_the_network_partition_key(Infrastructure::Request const& request); + +} + +template<> +class AK::Traits : public DefaultTraits { +public: + static unsigned hash(Web::Fetch::Infrastructure::NetworkPartitionKey const& partition_key) + { + return ::AK::Traits::hash(partition_key.top_level_origin); + } +};