AK: Recompute URL validity after changing protocol/host/path

This allows you to build URLs by calling setters on an empty URL and
actually get a valid URL at the end.
This commit is contained in:
Andreas Kling 2020-04-11 23:07:23 +02:00
parent 827e375297
commit b1555381ee
2 changed files with 43 additions and 4 deletions

View file

@ -210,4 +210,42 @@ URL URL::complete_url(const String& string) const
return url;
}
void URL::set_protocol(const String& protocol)
{
m_protocol = protocol;
m_valid = compute_validity();
}
void URL::set_host(const String& host)
{
m_host = host;
m_valid = compute_validity();
}
void URL::set_path(const String& path)
{
m_path = path;
m_valid = compute_validity();
}
void URL::set_query(const String& query)
{
m_query = query;
}
bool URL::compute_validity() const
{
// FIXME: This is by no means complete.
if (m_protocol.is_empty())
return false;
if (m_protocol == "http" || m_protocol == "https") {
if (m_host.is_empty())
return false;
} else if (m_protocol == "file") {
if (m_path.is_empty())
return false;
}
return true;
}
}

View file

@ -53,10 +53,10 @@ public:
String query() const { return m_query; }
u16 port() const { return m_port; }
void set_protocol(const String& protocol) { m_protocol = protocol; }
void set_host(const String& host) { m_host = host; }
void set_path(const String& path) { m_path = path; }
void set_query(const String& query) { m_query = query; }
void set_protocol(const String& protocol);
void set_host(const String& host);
void set_path(const String& path);
void set_query(const String& query);
void set_port(u16 port) { m_port = port; }
String to_string() const;
@ -65,6 +65,7 @@ public:
private:
bool parse(const StringView&);
bool compute_validity() const;
bool m_valid { false };
u16 m_port { 80 };