diff --git a/AK/URL.cpp b/AK/URL.cpp index 7c0293255ad..0a56793073b 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) diff --git a/AK/URL.h b/AK/URL.h index e066e885e5a..20d9a557b0a 100644 --- a/AK/URL.h +++ b/AK/URL.h @@ -12,8 +12,8 @@ namespace AK { -// FIXME: URL needs query string parsing. - +// NOTE: The member variables cannot contain any percent encoded sequences. +// The URL parser automatically decodes those sequences and the the serialize() method will re-encode them as necessary. class URL { public: enum class PercentEncodeSet { @@ -43,20 +43,29 @@ public: String scheme() const { return m_scheme; } String protocol() const { return m_scheme; } + String username() const { return m_username; } + String password() const { return m_password; } String host() const { return m_host; } - String path() const { return m_path; } + const Vector& paths() const { return m_paths; } String query() const { return m_query; } String fragment() const { return m_fragment; } - u16 port() const { return m_port; } + u16 port() const { return m_port ? m_port : default_port_for_scheme(m_scheme); } + bool cannot_be_a_base_url() const { return m_cannot_be_a_base_url; } void set_scheme(const String&); void set_protocol(const String& protocol) { set_scheme(protocol); } + void set_username(const String&); + void set_password(const String&); void set_host(const String&); void set_port(const u16); void set_path(const String&); + void set_paths(const Vector&); void set_query(const String&); void set_fragment(const String&); + void set_cannot_be_a_base_url(const bool value) { m_cannot_be_a_base_url = value; } + void append_path(const String& path) { m_paths.append(path); } + String path() const; String basename() const; String to_string() const; String to_string_encoded() const @@ -95,13 +104,21 @@ private: static void append_percent_encoded(StringBuilder&, u32 code_point); bool m_valid { false }; - u16 m_port { 0 }; - bool m_data_payload_is_base64 { false }; + String m_scheme; + 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 }; String m_path; + Vector m_paths; String m_query; String m_fragment; + + bool m_cannot_be_a_base_url { false }; + + bool m_data_payload_is_base64 { false }; String m_data_mime_type; String m_data_payload; };