From a2b523eeb8ea0927b35c5503d1295ca7443a3dc6 Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Sat, 28 Jun 2025 21:05:42 +1200 Subject: [PATCH] LibURL: Replace use of URL::get_public_suffix It is confusing to have both URL::Host::public_suffix and URL:get_public_suffix, both with slightly different semantics. Instead, use PublicSuffixData for cases that just want a direct match against the list, and URL::Host::public_suffix in LibWeb land as the URL spec defined AO. --- Libraries/LibURL/Host.cpp | 5 +++-- Libraries/LibURL/URL.cpp | 7 +------ Libraries/LibURL/URL.h | 1 - Libraries/LibWeb/DOM/Document.cpp | 4 ++-- Libraries/LibWebView/URL.cpp | 5 +++-- 5 files changed, 9 insertions(+), 13 deletions(-) diff --git a/Libraries/LibURL/Host.cpp b/Libraries/LibURL/Host.cpp index 589004ac855..d0b8954b8d1 100644 --- a/Libraries/LibURL/Host.cpp +++ b/Libraries/LibURL/Host.cpp @@ -1,12 +1,13 @@ /* * Copyright (c) 2021, Max Wipfli - * Copyright (c) 2023-2024, Shannon Booth + * Copyright (c) 2023-2025, Shannon Booth * Copyright (c) 2024, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ #include +#include #include namespace URL { @@ -193,7 +194,7 @@ Optional Host::public_suffix() const // 3. Let publicSuffix be the public suffix determined by running the Public Suffix List algorithm with host as domain. [PSL] // NOTE: The spec algorithm for the public suffix returns "*" by default, but get_public_suffix() returns an empty Optional. // Remove the `value_or()` if and when we update it. - auto public_suffix = get_public_suffix(host_string.bytes_as_string_view()).value_or("*"_string); + auto public_suffix = PublicSuffixData::the()->get_public_suffix(host_string).value_or("*"_string); // 4. Assert: publicSuffix is an ASCII string that does not end with ".". VERIFY(public_suffix.is_ascii()); diff --git a/Libraries/LibURL/URL.cpp b/Libraries/LibURL/URL.cpp index 9eb85537081..e43295ad86d 100644 --- a/Libraries/LibURL/URL.cpp +++ b/Libraries/LibURL/URL.cpp @@ -496,16 +496,11 @@ bool is_public_suffix(StringView host) return PublicSuffixData::the()->is_public_suffix(host); } -Optional get_public_suffix(StringView host) -{ - return PublicSuffixData::the()->get_public_suffix(host); -} - // https://github.com/publicsuffix/list/wiki/Format#algorithm Optional get_registrable_domain(StringView host) { // The registered or registrable domain is the public suffix plus one additional label. - auto public_suffix = get_public_suffix(host); + auto public_suffix = PublicSuffixData::the()->get_public_suffix(host); if (!public_suffix.has_value() || !host.ends_with(*public_suffix)) return {}; diff --git a/Libraries/LibURL/URL.h b/Libraries/LibURL/URL.h index db2ccc04411..39626a883c6 100644 --- a/Libraries/LibURL/URL.h +++ b/Libraries/LibURL/URL.h @@ -202,7 +202,6 @@ Optional create_with_file_scheme(ByteString const& path, ByteString const& URL create_with_data(StringView mime_type, StringView payload, bool is_base64 = false); bool is_public_suffix(StringView host); -Optional get_public_suffix(StringView host); Optional get_registrable_domain(StringView host); inline URL about_blank() { return URL::about("blank"_string); } diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp index d82d86dc443..4da39f76604 100644 --- a/Libraries/LibWeb/DOM/Document.cpp +++ b/Libraries/LibWeb/DOM/Document.cpp @@ -3845,10 +3845,10 @@ static bool is_a_registrable_domain_suffix_of_or_is_equal_to(StringView host_suf // * hostSuffix equals hostSuffix's public suffix; or // * hostSuffix, prefixed by U+002E (.), matches the end of originalHost's public suffix, // then return false. [URL] - if (host_suffix_string == URL::get_public_suffix(host_suffix_string)) + if (host_suffix_string == host_suffix->public_suffix()) return false; - auto original_host_public_suffix = URL::get_public_suffix(original_host_string); + auto original_host_public_suffix = original_host.public_suffix(); VERIFY(original_host_public_suffix.has_value()); if (original_host_public_suffix->ends_with_bytes(prefixed_host_suffix)) diff --git a/Libraries/LibWebView/URL.cpp b/Libraries/LibWebView/URL.cpp index 1d203d87556..e176d01730a 100644 --- a/Libraries/LibWebView/URL.cpp +++ b/Libraries/LibWebView/URL.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include namespace WebView { @@ -61,7 +62,7 @@ Optional sanitize_url(StringView location, Optional cons if (any_of(RESERVED_TLDS, [&](StringView const& tld) { return domain.byte_count() > tld.length() && domain.ends_with_bytes(tld); })) return url; - auto public_suffix = URL::get_public_suffix(domain); + auto public_suffix = URL::PublicSuffixData::the()->get_public_suffix(domain); if (!public_suffix.has_value() || *public_suffix == domain) { if (append_tld == AppendTLD::Yes) url->set_host(MUST(String::formatted("{}.com", domain))); @@ -120,7 +121,7 @@ static URLParts break_web_url_into_parts(URL::URL const& url, StringView url_str domain = url_without_scheme; } - auto public_suffix = URL::get_public_suffix(domain); + auto public_suffix = URL::PublicSuffixData::the()->get_public_suffix(domain); if (!public_suffix.has_value() || !domain.ends_with(*public_suffix)) return { scheme, domain, remainder };