From dd392dfa03961d95a4a1bea14a6ea8903f4e9df6 Mon Sep 17 00:00:00 2001 From: Max Wipfli Date: Tue, 25 May 2021 21:32:20 +0200 Subject: AK: Add member variables to the URL class This adds the m_username, m_password, m_paths and m_cannot_be_a_base_url member variables to the URL class. These are necessary for the upcoming new URL parser. The deprecated m_path variable shadows the m_paths variable if it is non-null. This behavior will be removed once the old URL parser has been removed. --- AK/URL.cpp | 52 +++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 45 insertions(+), 7 deletions(-) (limited to 'AK/URL.cpp') diff --git a/AK/URL.cpp b/AK/URL.cpp index 7c0293255a..0a56793073 100644 --- a/AK/URL.cpp +++ b/AK/URL.cpp @@ -259,6 +259,20 @@ URL::URL(const StringView& string) m_valid = parse(string); } +String URL::path() const +{ + if (cannot_be_a_base_url()) + return paths()[0]; + if (!m_path.is_null()) + return m_path; + StringBuilder builder; + for (auto& path : m_paths) { + builder.append('/'); + builder.append(path); + } + return builder.to_string(); +} + String URL::to_string() const { StringBuilder builder; @@ -287,7 +301,7 @@ String URL::to_string() const builder.append(String::number(m_port)); } - builder.append(m_path); + builder.append(path()); if (!m_query.is_empty()) { builder.append('?'); builder.append(m_query); @@ -364,6 +378,18 @@ void URL::set_scheme(const String& scheme) m_valid = compute_validity(); } +void URL::set_username(const String& username) +{ + m_username = username; + m_valid = compute_validity(); +} + +void URL::set_password(const String& password) +{ + m_password = password; + m_valid = compute_validity(); +} + void URL::set_host(const String& host) { m_host = host; @@ -372,6 +398,10 @@ void URL::set_host(const String& host) void URL::set_port(const u16 port) { + if (port == default_port_for_scheme(m_scheme)) { + m_port = 0; + return; + } m_port = port; m_valid = compute_validity(); } @@ -382,6 +412,12 @@ void URL::set_path(const String& path) m_valid = compute_validity(); } +void URL::set_paths(const Vector& paths) +{ + m_paths = paths; + m_valid = compute_validity(); +} + void URL::set_query(const String& query) { m_query = query; @@ -399,13 +435,13 @@ bool URL::compute_validity() const return false; if (m_scheme == "about") { - if (m_path.is_empty()) + if (path().is_empty()) return false; return true; } if (m_scheme == "file") { - if (m_path.is_empty()) + if (path().is_empty()) return false; return true; } @@ -419,9 +455,6 @@ bool URL::compute_validity() const if (m_host.is_empty()) return false; - if (!m_port && scheme_requires_port(m_scheme)) - return false; - return true; } @@ -484,7 +517,12 @@ String URL::basename() const { if (!m_valid) return {}; - return LexicalPath(m_path).basename(); + // FIXME: Temporary m_path hack + if (!m_path.is_null()) + return LexicalPath(m_path).basename(); + if (m_paths.is_empty()) + return {}; + return m_paths.last(); } void URL::append_percent_encoded(StringBuilder& builder, u32 code_point) -- cgit v1.2.3