LibWebView: Add an API to query if a host is on the Public Suffix List

This commit is contained in:
Timothy Flynn 2023-10-20 10:42:34 -04:00 committed by Andreas Kling
parent d4615c47a4
commit 9f9e5c0f55
3 changed files with 20 additions and 8 deletions

View file

@ -66,6 +66,7 @@ public:
return s_the;
}
bool is_public_suffix(StringView host);
ErrorOr<Optional<String>> get_public_suffix(StringView string);
private:
@ -127,17 +128,18 @@ PublicSuffixData::PublicSuffixData()
}
}
bool PublicSuffixData::is_public_suffix(StringView host)
{
auto it = host.begin();
auto& node = m_dictionary.traverse_until_last_accessible_node(it, host.end());
return it.is_end() && node.metadata().has_value();
}
ErrorOr<Optional<String>> PublicSuffixData::get_public_suffix(StringView string)
{
auto input = string.split_view("."sv);
input.reverse();
auto can_find = [&](StringView input) -> bool {
auto it = input.begin();
auto& node = m_dictionary.traverse_until_last_accessible_node(it, input.end());
return it.is_end() && node.metadata().has_value();
};
StringBuilder overall_search_string;
StringBuilder search_string;
for (auto part : input) {
@ -145,7 +147,7 @@ ErrorOr<Optional<String>> PublicSuffixData::get_public_suffix(StringView string)
TRY(search_string.try_append(TRY(overall_search_string.to_string())));
TRY(search_string.try_append(part));
if (can_find(search_string.string_view())) {
if (is_public_suffix(search_string.string_view())) {
overall_search_string.append(TRY(String::from_utf8(part)));
overall_search_string.append("."sv);
continue;
@ -155,7 +157,7 @@ ErrorOr<Optional<String>> PublicSuffixData::get_public_suffix(StringView string)
TRY(search_string.try_append(TRY(overall_search_string.to_string())));
TRY(search_string.try_append("*"sv));
if (can_find(search_string.string_view())) {
if (is_public_suffix(search_string.string_view())) {
overall_search_string.append(TRY(String::from_utf8(part)));
overall_search_string.append("."sv);
continue;

View file

@ -45,6 +45,15 @@ static Optional<URL> query_public_suffix_list(StringView url_string)
return {};
}
bool is_public_suffix([[maybe_unused]] StringView host)
{
#if defined(ENABLE_PUBLIC_SUFFIX)
return PublicSuffixData::the()->is_public_suffix(host);
#else
return false;
#endif
}
Optional<String> get_public_suffix([[maybe_unused]] StringView host)
{
#if defined(ENABLE_PUBLIC_SUFFIX)

View file

@ -12,6 +12,7 @@
namespace WebView {
bool is_public_suffix(StringView host);
Optional<String> get_public_suffix(StringView host);
enum class AppendTLD {