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

@ -169,15 +169,19 @@ URL create_with_file_scheme(ByteString const& path, ByteString const& fragment,
if (!lexical_path.is_absolute()) if (!lexical_path.is_absolute())
return {}; return {};
URL url; StringBuilder url_builder;
url.set_scheme("file"_string); url_builder.append("file://"sv);
url.set_host(hostname == "localhost" ? String {} : String::from_byte_string(hostname).release_value_but_fixme_should_propagate_errors()); url_builder.append(hostname);
url.set_paths(lexical_path.parts()); url_builder.append(lexical_path.string());
if (path.ends_with('/')) if (path.ends_with('/'))
url.append_slash(); url_builder.append('/');
if (!fragment.is_empty()) if (!fragment.is_empty()) {
url.set_fragment(String::from_byte_string(fragment).release_value_but_fixme_should_propagate_errors()); url_builder.append('#');
return url; url_builder.append(fragment);
}
auto url = Parser::basic_parse(url_builder.string_view());
return url.has_value() ? url.release_value() : URL {};
} }
URL create_with_url_or_path(ByteString const& url_or_path) URL create_with_url_or_path(ByteString const& url_or_path)

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 }; 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(); return search_url_or_error();
// FIXME: Add support for other schemes, e.g. "mailto:". Firefox and Chrome open mailto: locations. // 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()) { if (host.has_value() && host->is_domain()) {
auto const& domain = host->get<String>(); 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); auto public_suffix = URL::get_public_suffix(domain);
if (!public_suffix.has_value() || *public_suffix == domain) { if (!public_suffix.has_value() || *public_suffix == domain) {
if (append_tld == AppendTLD::Yes) 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) else if (https_scheme_was_guessed && domain != "localhost"sv)
return search_url_or_error(); return search_url_or_error();
} }

View file

@ -422,7 +422,7 @@ static void run_test(HeadlessWebView& view, Test& test, Application& app)
view.on_test_finish = {}; view.on_test_finish = {};
promise->when_resolved([&view, &test, &app](auto) { promise->when_resolved([&view, &test, &app](auto) {
auto url = URL::create_with_file_scheme(MUST(FileSystem::real_path(test.input_path))); auto url = URL::create_with_file_scheme(MUST(FileSystem::real_path(test.input_path))).release_value();
switch (test.mode) { switch (test.mode) {
case TestMode::Crash: case TestMode::Crash:

View file

@ -435,7 +435,7 @@ static void do_run_tests(XML::Document& document)
path_builder.append(suite.attributes.find("URI")->value); path_builder.append(suite.attributes.find("URI")->value);
auto url = URL::create_with_file_scheme(path_builder.string_view()); auto url = URL::create_with_file_scheme(path_builder.string_view());
auto file_path = URL::percent_decode(url.serialize_path()); auto file_path = URL::percent_decode(url->serialize_path());
auto file_result = Core::File::open(file_path, Core::File::OpenMode::Read); auto file_result = Core::File::open(file_path, Core::File::OpenMode::Read);
if (file_result.is_error()) { if (file_result.is_error()) {
warnln("Read error for {}: {}", file_path, file_result.error()); warnln("Read error for {}: {}", file_path, file_result.error());