From 32ddeb82d6ad9ced787a11a7b1fc8b5852baa7bb Mon Sep 17 00:00:00 2001 From: stasoid Date: Fri, 14 Feb 2025 15:31:43 +0500 Subject: [PATCH] LibURL+LibWeb: Remove leading slash when converting url to path ...on Windows --- Libraries/LibURL/URL.cpp | 12 ++++++++++++ Libraries/LibURL/URL.h | 1 + Libraries/LibWeb/Loader/GeneratedPagesLoader.cpp | 2 +- Libraries/LibWeb/Loader/Resource.cpp | 2 +- Libraries/LibWeb/Loader/ResourceLoader.cpp | 6 +++--- 5 files changed, 18 insertions(+), 5 deletions(-) diff --git a/Libraries/LibURL/URL.cpp b/Libraries/LibURL/URL.cpp index ec7932199e6..8a82f790854 100644 --- a/Libraries/LibURL/URL.cpp +++ b/Libraries/LibURL/URL.cpp @@ -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 { diff --git a/Libraries/LibURL/URL.h b/Libraries/LibURL/URL.h index ad5e60f8bd7..88ec9f965a8 100644 --- a/Libraries/LibURL/URL.h +++ b/Libraries/LibURL/URL.h @@ -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(); } diff --git a/Libraries/LibWeb/Loader/GeneratedPagesLoader.cpp b/Libraries/LibWeb/Loader/GeneratedPagesLoader.cpp index 5f2fce950c3..d256b698037 100644 --- a/Libraries/LibWeb/Loader/GeneratedPagesLoader.cpp +++ b/Libraries/LibWeb/Loader/GeneratedPagesLoader.cpp @@ -45,7 +45,7 @@ ErrorOr load_error_page(URL::URL const& url, StringView error_message) ErrorOr 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 names; while (dt.has_next()) diff --git a/Libraries/LibWeb/Loader/Resource.cpp b/Libraries/LibWeb/Loader/Resource.cpp index 075cd7e378f..3b2bf088e6f 100644 --- a/Libraries/LibWeb/Loader/Resource.cpp +++ b/Libraries/LibWeb/Loader/Resource.cpp @@ -102,7 +102,7 @@ void Resource::did_load(Badge, 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()); } } diff --git a/Libraries/LibWeb/Loader/ResourceLoader.cpp b/Libraries/LibWeb/Loader/ResourceLoader.cpp index 08fd211fb67..6b417ea5262 100644 --- a/Libraries/LibWeb/Loader/ResourceLoader.cpp +++ b/Libraries/LibWeb/Loader/ResourceLoader.cpp @@ -321,7 +321,7 @@ void ResourceLoader::load(LoadRequest& request, GC::Root 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 succes return; } - FileRequest file_request(URL::percent_decode(url.serialize_path()), [this, success_callback, error_callback, request, respond_directory_page](ErrorOr file_or_error) { + FileRequest file_request(url.file_path(), [this, success_callback, error_callback, request, respond_directory_page](ErrorOr 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 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 {};