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.
This commit is contained in:
Shannon Booth 2023-08-13 11:00:56 +12:00 committed by Andrew Kaster
parent 66f4cdba85
commit 23e82114b4
Notes: sideshowbarker 2024-07-17 17:49:11 +09:00
4 changed files with 14 additions and 4 deletions

View file

@ -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<u16> 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)

View file

@ -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<u16> default_port_for_scheme(StringView);
static bool is_special_scheme(StringView);
enum class SpaceAsPlus {

View file

@ -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 => ''

View file

@ -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);
}