LibURL+LibWeb: Remove leading slash when converting url to path

...on Windows
This commit is contained in:
stasoid 2025-02-14 15:31:43 +05:00 committed by Andrew Kaster
commit 32ddeb82d6
Notes: github-actions[bot] 2025-04-11 01:05:22 +00:00
5 changed files with 18 additions and 5 deletions

View file

@ -246,6 +246,18 @@ String URL::serialize_path() const
return output.to_string_without_validation();
}
// This function is used whenever a path is needed to access the actual file on disk.
// On Windows serialize_path can produce a path like /C:/path/to/tst.htm, so the leading slash needs to be removed to obtain a valid path.
ByteString URL::file_path() const
{
ByteString path = percent_decode(serialize_path());
#ifdef AK_OS_WINDOWS
if (path.starts_with('/'))
path = path.substring(1);
#endif
return path;
}
// https://url.spec.whatwg.org/#concept-url-serializer
String URL::serialize(ExcludeFragment exclude_fragment) const
{

View file

@ -122,6 +122,7 @@ public:
}
String serialize_path() const;
ByteString file_path() const;
String serialize(ExcludeFragment = ExcludeFragment::No) const;
ByteString serialize_for_display() const;
ByteString to_byte_string() const { return serialize().to_byte_string(); }

View file

@ -45,7 +45,7 @@ ErrorOr<String> load_error_page(URL::URL const& url, StringView error_message)
ErrorOr<String> load_file_directory_page(URL::URL const& url)
{
// Generate HTML contents entries table
auto lexical_path = LexicalPath(URL::percent_decode(url.serialize_path()));
auto lexical_path = LexicalPath(url.file_path());
Core::DirIterator dt(lexical_path.string(), Core::DirIterator::Flags::SkipParentAndBaseDir);
Vector<ByteString> names;
while (dt.has_next())

View file

@ -102,7 +102,7 @@ void Resource::did_load(Badge<ResourceLoader>, ReadonlyBytes data, HTTP::HeaderM
if (content_type_options.value_or("").equals_ignoring_ascii_case("nosniff"sv)) {
m_mime_type = "text/plain";
} else {
m_mime_type = Core::guess_mime_type_based_on_filename(URL::percent_decode(url().serialize_path()));
m_mime_type = Core::guess_mime_type_based_on_filename(url().file_path());
}
}

View file

@ -321,7 +321,7 @@ void ResourceLoader::load(LoadRequest& request, GC::Root<SuccessCallback> succes
}
auto data = resource.value()->data();
auto response_headers = response_headers_for_file(URL::percent_decode(url.serialize_path()), resource.value()->modified_time());
auto response_headers = response_headers_for_file(url.file_path(), resource.value()->modified_time());
// FIXME: Implement timing info for resource requests.
Requests::RequestTimingInfo fixme_implement_timing_info {};
@ -339,7 +339,7 @@ void ResourceLoader::load(LoadRequest& request, GC::Root<SuccessCallback> succes
return;
}
FileRequest file_request(URL::percent_decode(url.serialize_path()), [this, success_callback, error_callback, request, respond_directory_page](ErrorOr<i32> file_or_error) {
FileRequest file_request(url.file_path(), [this, success_callback, error_callback, request, respond_directory_page](ErrorOr<i32> file_or_error) {
--m_pending_loads;
if (on_load_counter_change)
on_load_counter_change();
@ -387,7 +387,7 @@ void ResourceLoader::load(LoadRequest& request, GC::Root<SuccessCallback> succes
}
auto data = maybe_data.release_value();
auto response_headers = response_headers_for_file(URL::percent_decode(request.url().serialize_path()), st_or_error.value().st_mtime);
auto response_headers = response_headers_for_file(request.url().file_path(), st_or_error.value().st_mtime);
// FIXME: Implement timing info for file requests.
Requests::RequestTimingInfo fixme_implement_timing_info {};