From b1555381eecbc6e909716313a0a57fc10767a7cd Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 11 Apr 2020 23:07:23 +0200 Subject: [PATCH] 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. --- AK/URL.cpp | 38 ++++++++++++++++++++++++++++++++++++++ AK/URL.h | 9 +++++---- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/AK/URL.cpp b/AK/URL.cpp index e523de26862..e3460cb58df 100644 --- a/AK/URL.cpp +++ b/AK/URL.cpp @@ -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; +} + } diff --git a/AK/URL.h b/AK/URL.h index a3818160410..f057a3c9463 100644 --- a/AK/URL.h +++ b/AK/URL.h @@ -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 };