From 23e82114b414aebeb3aaeff7631743b44f6acc91 Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Sun, 13 Aug 2023 11:00:56 +1200 Subject: [PATCH] AK: Do not consider port of 0 as a null port This fixes an issue where if a port number of 0 was given for a non special scheme the port number was being dropped. --- AK/URL.cpp | 4 ++-- AK/URL.h | 4 ++-- Tests/LibWeb/Text/expected/URL/url.txt | 9 +++++++++ Tests/LibWeb/Text/input/URL/url.html | 1 + 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/AK/URL.cpp b/AK/URL.cpp index 8a5ee62241..6bc43c4af3 100644 --- a/AK/URL.cpp +++ b/AK/URL.cpp @@ -166,7 +166,7 @@ bool URL::compute_validity() const } // https://url.spec.whatwg.org/#default-port -u16 URL::default_port_for_scheme(StringView scheme) +Optional URL::default_port_for_scheme(StringView scheme) { // Spec defined mappings with port: if (scheme == "ftp") @@ -188,7 +188,7 @@ u16 URL::default_port_for_scheme(StringView scheme) if (scheme == "ircs") return 6697; - return 0; + return {}; } URL URL::create_with_file_scheme(DeprecatedString const& path, DeprecatedString const& fragment, DeprecatedString const& hostname) diff --git a/AK/URL.h b/AK/URL.h index 55ec4e1762..01221b3004 100644 --- a/AK/URL.h +++ b/AK/URL.h @@ -84,7 +84,7 @@ public: DeprecatedString path_segment_at_index(size_t index) const; size_t path_segment_count() const { return m_paths.size(); } - u16 port_or_default() const { return m_port.value_or(default_port_for_scheme(m_scheme)); } + u16 port_or_default() const { return m_port.value_or(default_port_for_scheme(m_scheme).value_or(0)); } bool cannot_be_a_base_url() const { return m_cannot_be_a_base_url; } bool cannot_have_a_username_or_password_or_port() const; @@ -131,7 +131,7 @@ public: static URL create_with_help_scheme(DeprecatedString const& path, DeprecatedString const& fragment = {}, DeprecatedString const& hostname = {}); static URL create_with_data(StringView mime_type, StringView payload, bool is_base64 = false); - static u16 default_port_for_scheme(StringView); + static Optional default_port_for_scheme(StringView); static bool is_special_scheme(StringView); enum class SpaceAsPlus { diff --git a/Tests/LibWeb/Text/expected/URL/url.txt b/Tests/LibWeb/Text/expected/URL/url.txt index cad194eaac..dcb44a8f2e 100644 --- a/Tests/LibWeb/Text/expected/URL/url.txt +++ b/Tests/LibWeb/Text/expected/URL/url.txt @@ -34,3 +34,12 @@ hostname => '[1:1:0:0:1::]' port => '' pathname => '/' search => '' +unknown://serenityos.org:0 +protocol => 'unknown:' +username => '' +password => '' +host => 'serenityos.org:0' +hostname => 'serenityos.org' +port => '0' +pathname => '' +search => '' diff --git a/Tests/LibWeb/Text/input/URL/url.html b/Tests/LibWeb/Text/input/URL/url.html index 041f775b93..1f17dc7738 100644 --- a/Tests/LibWeb/Text/input/URL/url.html +++ b/Tests/LibWeb/Text/input/URL/url.html @@ -19,6 +19,7 @@ 'http://[0:1:0:1:0:1:0:1]', 'http://[1:0:1:0:1:0:1:0]', 'http://[1:1:0:0:1:0:0:0]/', + 'unknown://serenityos.org:0', ]) { printURL(url); }