diff options
author | Andreas Kling <kling@serenityos.org> | 2022-09-20 15:38:53 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-09-20 15:38:53 +0200 |
commit | 287a9b552a017903d496cd3011142dad858749e9 (patch) | |
tree | 1dbf6a8ee811ce1ec5849db670b0c904405e594d | |
parent | ac76df3d47bb2de85aef2440ea1bb203bf7bd018 (diff) | |
download | serenity-287a9b552a017903d496cd3011142dad858749e9.zip |
AK: Fix bad parsing of some file:/// URLs with base URL
We were dropping the base URL path components in the resulting URL due
to mistakenly determining the input URL to start with a Windows drive
letter. Fix this, add a spec link, and a test.
-rw-r--r-- | AK/URLParser.cpp | 3 | ||||
-rw-r--r-- | Tests/AK/TestURL.cpp | 14 |
2 files changed, 16 insertions, 1 deletions
diff --git a/AK/URLParser.cpp b/AK/URLParser.cpp index 98f218a2ea..e39e0667fc 100644 --- a/AK/URLParser.cpp +++ b/AK/URLParser.cpp @@ -84,11 +84,12 @@ static Optional<String> parse_host(StringView input, bool is_not_special = false return ipv4_host; } +// https://url.spec.whatwg.org/#start-with-a-windows-drive-letter constexpr bool starts_with_windows_drive_letter(StringView input) { if (input.length() < 2) return false; - if (!is_ascii_alpha(input[0]) && !(input[1] == ':' || input[1] == '|')) + if (!is_ascii_alpha(input[0]) || !(input[1] == ':' || input[1] == '|')) return false; if (input.length() == 2) return true; diff --git a/Tests/AK/TestURL.cpp b/Tests/AK/TestURL.cpp index a9fbf426a3..66569e9a7e 100644 --- a/Tests/AK/TestURL.cpp +++ b/Tests/AK/TestURL.cpp @@ -392,3 +392,17 @@ TEST_CASE(unicode) EXPECT(url.query().is_null()); EXPECT(url.fragment().is_null()); } + +TEST_CASE(complete_file_url_with_base) +{ + URL url { "file:///home/index.html" }; + EXPECT(url.is_valid()); + EXPECT_EQ(url.path(), "/home/index.html"); + EXPECT_EQ(url.paths().size(), 2u); + EXPECT_EQ(url.paths()[0], "home"); + EXPECT_EQ(url.paths()[1], "index.html"); + + auto sub_url = url.complete_url("js/app.js"); + EXPECT(sub_url.is_valid()); + EXPECT_EQ(sub_url.path(), "/home/js/app.js"); +} |