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.
This commit is contained in:
Shannon Booth 2025-06-28 21:05:42 +12:00 committed by Tim Ledbetter
commit a2b523eeb8
Notes: github-actions[bot] 2025-06-29 11:49:11 +00:00
5 changed files with 9 additions and 13 deletions

View file

@ -1,12 +1,13 @@
/*
* Copyright (c) 2021, Max Wipfli <mail@maxwipfli.ch>
* Copyright (c) 2023-2024, Shannon Booth <shannon@serenityos.org>
* Copyright (c) 2023-2025, Shannon Booth <shannon@serenityos.org>
* Copyright (c) 2024, Sam Atkins <sam@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibURL/Host.h>
#include <LibURL/PublicSuffixData.h>
#include <LibURL/URL.h>
namespace URL {
@ -193,7 +194,7 @@ Optional<String> 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());

View file

@ -496,16 +496,11 @@ bool is_public_suffix(StringView host)
return PublicSuffixData::the()->is_public_suffix(host);
}
Optional<String> get_public_suffix(StringView host)
{
return PublicSuffixData::the()->get_public_suffix(host);
}
// https://github.com/publicsuffix/list/wiki/Format#algorithm
Optional<String> 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 {};

View file

@ -202,7 +202,6 @@ Optional<URL> 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<String> get_public_suffix(StringView host);
Optional<String> get_registrable_domain(StringView host);
inline URL about_blank() { return URL::about("blank"_string); }

View file

@ -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))

View file

@ -9,6 +9,7 @@
#include <AK/String.h>
#include <LibFileSystem/FileSystem.h>
#include <LibURL/Parser.h>
#include <LibURL/PublicSuffixData.h>
#include <LibWebView/URL.h>
namespace WebView {
@ -61,7 +62,7 @@ Optional<URL::URL> sanitize_url(StringView location, Optional<SearchEngine> 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 };