1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 12:37:44 +00:00

AK: Make URL::m_port an Optional<u16>, Expose raw port getter

Our current way of signalling a missing port with m_port == 0 was
lacking, as 0 is a valid port number in URLs.
This commit is contained in:
Idan Horowitz 2021-09-13 23:12:16 +03:00 committed by Andreas Kling
parent 1c9c43785d
commit d6cfa34667
15 changed files with 42 additions and 41 deletions

View file

@ -71,13 +71,13 @@ void URL::set_host(String host)
m_valid = compute_validity();
}
void URL::set_port(u16 port)
void URL::set_port(Optional<u16> port)
{
if (port == default_port_for_scheme(m_scheme)) {
m_port = 0;
m_port = {};
return;
}
m_port = port;
m_port = move(port);
m_valid = compute_validity();
}
@ -234,8 +234,8 @@ String URL::serialize(ExcludeFragment exclude_fragment) const
}
builder.append(m_host);
if (m_port != 0)
builder.appendff(":{}", m_port);
if (m_port.has_value())
builder.appendff(":{}", *m_port);
}
if (cannot_be_a_base_url()) {
@ -278,8 +278,8 @@ String URL::serialize_for_display() const
if (!m_host.is_null()) {
builder.append("//");
builder.append(m_host);
if (m_port != 0)
builder.appendff(":{}", m_port);
if (m_port.has_value())
builder.appendff(":{}", *m_port);
}
if (cannot_be_a_base_url()) {
@ -329,8 +329,8 @@ String URL::serialize_origin() const
builder.append(m_scheme);
builder.append("://"sv);
builder.append(m_host);
if (m_port != 0)
builder.append(":{}", m_port);
if (m_port.has_value())
builder.append(":{}", *m_port);
return builder.build();
}

View file

@ -56,7 +56,8 @@ public:
Vector<String> const& paths() const { return m_paths; }
String const& query() const { return m_query; }
String const& fragment() const { return m_fragment; }
u16 port() const { return m_port ? m_port : default_port_for_scheme(m_scheme); }
Optional<u16> port() const { return m_port; }
u16 port_or_default() const { return m_port.value_or(default_port_for_scheme(m_scheme)); }
bool cannot_be_a_base_url() const { return m_cannot_be_a_base_url; }
bool cannot_have_a_username_or_password_or_port() const { return m_host.is_null() || m_host.is_empty() || m_cannot_be_a_base_url || m_scheme == "file"sv; }
@ -68,7 +69,7 @@ public:
void set_username(String);
void set_password(String);
void set_host(String);
void set_port(u16);
void set_port(Optional<u16>);
void set_paths(Vector<String>);
void set_query(String);
void set_fragment(String);
@ -129,8 +130,8 @@ private:
String m_username;
String m_password;
String m_host;
// NOTE: If the port is the default port for the scheme, m_port should be 0.
u16 m_port { 0 };
// NOTE: If the port is the default port for the scheme, m_port should be empty.
Optional<u16> m_port;
String m_path;
Vector<String> m_paths;
String m_query;

View file

@ -472,7 +472,7 @@ URL URLParser::parse(StringView const& raw_input, URL const* base_url, Optional<
return {};
}
if (port.value() == URL::default_port_for_scheme(url->scheme()))
url->m_port = 0;
url->m_port = {};
else
url->m_port = port.value();
buffer.clear();