LibURL: Implement create_with_file_scheme using URL Parser

Creating a URL should almost always go through the URLParser to
handle all of the small edge cases involved. This reduces the
need for URL valid state.
This commit is contained in:
Shannon Booth 2025-04-19 16:41:19 +12:00 committed by Tim Flynn
commit 2072eee83d
Notes: github-actions[bot] 2025-04-19 11:20:23 +00:00
4 changed files with 17 additions and 13 deletions

View file

@ -45,11 +45,11 @@ Optional<URL::URL> sanitize_url(StringView location, Optional<SearchEngine> cons
}
static constexpr Array SUPPORTED_SCHEMES { "about"sv, "data"sv, "file"sv, "http"sv, "https"sv, "resource"sv };
if (!any_of(SUPPORTED_SCHEMES, [&](StringView const& scheme) { return scheme == url.scheme(); }))
if (!any_of(SUPPORTED_SCHEMES, [&](StringView const& scheme) { return scheme == url->scheme(); }))
return search_url_or_error();
// FIXME: Add support for other schemes, e.g. "mailto:". Firefox and Chrome open mailto: locations.
auto const& host = url.host();
auto const& host = url->host();
if (host.has_value() && host->is_domain()) {
auto const& domain = host->get<String>();
@ -64,7 +64,7 @@ Optional<URL::URL> sanitize_url(StringView location, Optional<SearchEngine> cons
auto public_suffix = URL::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)));
url->set_host(MUST(String::formatted("{}.com", domain)));
else if (https_scheme_was_guessed && domain != "localhost"sv)
return search_url_or_error();
}