diff --git a/AK/URL.cpp b/AK/URL.cpp index 8a5ee622416..6bc43c4af3f 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 55ec4e1762c..01221b30048 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 cad194eaac8..dcb44a8f2e9 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 041f775b939..1f17dc77381 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); }