summaryrefslogtreecommitdiff
path: root/AK/URL.cpp
diff options
context:
space:
mode:
authorMax Wipfli <mail@maxwipfli.ch>2021-05-25 21:32:20 +0200
committerAndreas Kling <kling@serenityos.org>2021-06-01 09:28:05 +0200
commitdd392dfa03961d95a4a1bea14a6ea8903f4e9df6 (patch)
tree86d44a7d6d798e8122bca9c68c5413692b601281 /AK/URL.cpp
parent0d41a7d39aebde99bed69a54022e2036228f8af8 (diff)
downloadserenity-dd392dfa03961d95a4a1bea14a6ea8903f4e9df6.zip
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.
Diffstat (limited to 'AK/URL.cpp')
-rw-r--r--AK/URL.cpp52
1 files changed, 45 insertions, 7 deletions
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<String>& 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)