diff --git a/Userland/Libraries/LibWebView/CookieJar.cpp b/Userland/Libraries/LibWebView/CookieJar.cpp index f9731979a1..22cf2149eb 100644 --- a/Userland/Libraries/LibWebView/CookieJar.cpp +++ b/Userland/Libraries/LibWebView/CookieJar.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2023, Tim Flynn + * Copyright (c) 2021-2024, Tim Flynn * Copyright (c) 2022, the SerenityOS developers. * Copyright (c) 2022, Tobias Christiansen * Copyright (c) 2023, Jelle Raaijmakers @@ -9,7 +9,6 @@ #include #include -#include #include #include #include @@ -163,7 +162,7 @@ void CookieJar::dump_cookies() builder.appendff("\t{}SameSite{} = {:s}\n", attribute_color, no_color, Web::Cookie::same_site_to_string(cookie.same_site)); }); - dbgln("{} cookies stored\n{}", total_cookies, builder.to_byte_string()); + dbgln("{} cookies stored\n{}", total_cookies, builder.string_view()); } Vector CookieJar::get_all_cookies() @@ -187,7 +186,7 @@ Vector CookieJar::get_all_cookies(URL const& url) return get_matching_cookies(url, domain.value(), Web::Cookie::Source::Http, MatchingCookiesSpecMode::WebDriver); } -Optional CookieJar::get_named_cookie(URL const& url, ByteString const& name) +Optional CookieJar::get_named_cookie(URL const& url, StringView name) { auto domain = canonicalize_domain(url); if (!domain.has_value()) @@ -196,7 +195,7 @@ Optional CookieJar::get_named_cookie(URL const& url, ByteSt auto cookie_list = get_matching_cookies(url, domain.value(), Web::Cookie::Source::Http, MatchingCookiesSpecMode::WebDriver); for (auto const& cookie : cookie_list) { - if (cookie.name == name.view()) + if (cookie.name == name) return cookie; } @@ -216,7 +215,7 @@ Optional CookieJar::canonicalize_domain(const URL& url) return url.serialized_host().release_value_but_fixme_should_propagate_errors().to_byte_string().to_lowercase(); } -bool CookieJar::domain_matches(ByteString const& string, ByteString const& domain_string) +bool CookieJar::domain_matches(StringView string, StringView domain_string) { // https://tools.ietf.org/html/rfc6265#section-5.1.3 @@ -240,7 +239,7 @@ bool CookieJar::domain_matches(ByteString const& string, ByteString const& domai return true; } -bool CookieJar::path_matches(ByteString const& request_path, ByteString const& cookie_path) +bool CookieJar::path_matches(StringView request_path, StringView cookie_path) { // https://tools.ietf.org/html/rfc6265#section-5.1.4 @@ -333,7 +332,7 @@ void CookieJar::store_cookie(Web::Cookie::ParsedCookie const& parsed_cookie, con // 6. If the domain-attribute is non-empty: if (!cookie.domain.is_empty()) { // If the canonicalized request-host does not domain-match the domain-attribute: Ignore the cookie entirely and abort these steps. - if (!domain_matches(canonicalized_domain, cookie.domain.to_byte_string())) + if (!domain_matches(canonicalized_domain, cookie.domain)) return; // Set the cookie's host-only-flag to false. Set the cookie's domain to the domain-attribute. @@ -393,7 +392,7 @@ void CookieJar::store_cookie(Web::Cookie::ParsedCookie const& parsed_cookie, con MUST(sync_promise->await()); } -Vector CookieJar::get_matching_cookies(const URL& url, ByteString const& canonicalized_domain, Web::Cookie::Source source, MatchingCookiesSpecMode mode) +Vector CookieJar::get_matching_cookies(const URL& url, StringView canonicalized_domain, Web::Cookie::Source source, MatchingCookiesSpecMode mode) { // https://tools.ietf.org/html/rfc6265#section-5.4 @@ -403,13 +402,13 @@ Vector CookieJar::get_matching_cookies(const URL& url, Byte select_all_cookies_from_database([&](auto cookie) { // Either: The cookie's host-only-flag is true and the canonicalized request-host is identical to the cookie's domain. // Or: The cookie's host-only-flag is false and the canonicalized request-host domain-matches the cookie's domain. - bool is_host_only_and_has_identical_domain = cookie.host_only && (canonicalized_domain.view() == cookie.domain); - bool is_not_host_only_and_domain_matches = !cookie.host_only && domain_matches(canonicalized_domain, cookie.domain.to_byte_string()); + bool is_host_only_and_has_identical_domain = cookie.host_only && (canonicalized_domain == cookie.domain); + bool is_not_host_only_and_domain_matches = !cookie.host_only && domain_matches(canonicalized_domain, cookie.domain); if (!is_host_only_and_has_identical_domain && !is_not_host_only_and_domain_matches) return; // The request-uri's path path-matches the cookie's path. - if (!path_matches(url.serialize_path(), cookie.path.to_byte_string())) + if (!path_matches(url.serialize_path(), cookie.path)) return; // If the cookie's secure-only-flag is true, then the request-uri's scheme must denote a "secure" protocol. diff --git a/Userland/Libraries/LibWebView/CookieJar.h b/Userland/Libraries/LibWebView/CookieJar.h index b553dffce2..4b51c23f8e 100644 --- a/Userland/Libraries/LibWebView/CookieJar.h +++ b/Userland/Libraries/LibWebView/CookieJar.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2023, Tim Flynn + * Copyright (c) 2021-2024, Tim Flynn * * SPDX-License-Identifier: BSD-2-Clause */ @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -54,15 +55,15 @@ public: void dump_cookies(); Vector get_all_cookies(); Vector get_all_cookies(URL const& url); - Optional get_named_cookie(URL const& url, ByteString const& name); + Optional get_named_cookie(URL const& url, StringView name); private: explicit CookieJar(PersistedStorage); explicit CookieJar(TransientStorage); static Optional canonicalize_domain(const URL& url); - static bool domain_matches(ByteString const& string, ByteString const& domain_string); - static bool path_matches(ByteString const& request_path, ByteString const& cookie_path); + static bool domain_matches(StringView string, StringView domain_string); + static bool path_matches(StringView request_path, StringView cookie_path); static ByteString default_path(const URL& url); enum class MatchingCookiesSpecMode { @@ -71,7 +72,7 @@ private: }; void store_cookie(Web::Cookie::ParsedCookie const& parsed_cookie, const URL& url, ByteString canonicalized_domain, Web::Cookie::Source source); - Vector get_matching_cookies(const URL& url, ByteString const& canonicalized_domain, Web::Cookie::Source source, MatchingCookiesSpecMode mode = MatchingCookiesSpecMode::RFC6265); + Vector get_matching_cookies(const URL& url, StringView canonicalized_domain, Web::Cookie::Source source, MatchingCookiesSpecMode mode = MatchingCookiesSpecMode::RFC6265); void insert_cookie_into_database(Web::Cookie::Cookie const& cookie); void update_cookie_in_database(Web::Cookie::Cookie const& cookie);