1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 18:57:45 +00:00

AK::URL: Check if URL requires a port set to be considered a valid URL

`AK::URL` will now check if the URL requires a port to be set using
`AK::URL.protocol_requires_port(protocol)`.

If the URL does not specify a port, and no default port for the URL
protocol is found with `AK::URL.default_port_for_protocol(protocol)`,
the URL is considered to be invalid.
This commit is contained in:
Brendan Coles 2020-11-04 06:20:20 +00:00 committed by Andreas Kling
parent fb89c324c5
commit 3e0e84dcd1
3 changed files with 91 additions and 18 deletions

View file

@ -93,12 +93,14 @@ bool URL::parse(const StringView& string)
if (m_protocol == "data") {
buffer.clear();
m_host = "";
state = State::InDataMimeType;
continue;
}
if (m_protocol == "about") {
buffer.clear();
m_host = "";
state = State::InPath;
continue;
}
@ -109,12 +111,6 @@ bool URL::parse(const StringView& string)
return false;
if (buffer.is_empty())
return false;
if (m_protocol == "http")
m_port = 80;
else if (m_protocol == "https")
m_port = 443;
else if (m_protocol == "gemini")
m_port = 1965;
state = State::InHostname;
buffer.clear();
continue;
@ -244,7 +240,11 @@ bool URL::parse(const StringView& string)
m_query = "";
if (m_fragment.is_null())
m_fragment = "";
return true;
if (!m_port && protocol_requires_port(m_protocol))
set_port(default_port_for_protocol(m_protocol));
return compute_validity();
}
URL::URL(const StringView& string)
@ -275,12 +275,11 @@ String URL::to_string() const
builder.append("://");
builder.append(m_host);
if (protocol() != "file") {
if (!(protocol() == "http" && port() == 80) && !(protocol() == "https" && port() == 443) && !(protocol() == "gemini" && port() == 1965)) {
builder.append(':');
builder.append(String::number(m_port));
}
if (default_port_for_protocol(protocol()) != port()) {
builder.append(':');
builder.append(String::number(m_port));
}
builder.append(m_path);
if (!m_query.is_empty()) {
builder.append('?');
@ -364,6 +363,12 @@ void URL::set_host(const String& host)
m_valid = compute_validity();
}
void URL::set_port(u16 port)
{
m_port = port;
m_valid = compute_validity();
}
void URL::set_path(const String& path)
{
m_path = path;
@ -385,16 +390,54 @@ bool URL::compute_validity() const
// FIXME: This is by no means complete.
if (m_protocol.is_empty())
return false;
if (m_protocol == "about") {
if (m_path.is_empty())
return false;
return true;
}
if (m_protocol == "file") {
if (m_path.is_empty())
return false;
} else {
if (m_host.is_empty())
return false;
return true;
}
if (m_protocol == "data") {
if (m_data_mime_type.is_empty())
return false;
return true;
}
if (m_host.is_empty())
return false;
if (!m_port && protocol_requires_port(m_protocol))
return false;
return true;
}
bool URL::protocol_requires_port(const String& protocol)
{
return (default_port_for_protocol(protocol) != 0);
}
u16 URL::default_port_for_protocol(const String& protocol)
{
if (protocol == "http")
return 80;
if (protocol == "https")
return 443;
if (protocol == "gemini")
return 1965;
if (protocol == "irc")
return 6667;
if (protocol == "ircs")
return 6697;
return 0;
}
URL URL::create_with_file_protocol(const String& path)
{
URL url;
@ -416,8 +459,8 @@ URL URL::create_with_url_or_path(const String& url_or_path)
URL URL::create_with_data(const StringView& mime_type, const StringView& payload, bool is_base64)
{
URL url;
url.m_valid = true;
url.set_protocol("data");
url.m_valid = true;
url.m_data_payload = payload;
url.m_data_mime_type = mime_type;
url.m_data_payload_is_base64 = is_base64;