1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 14:28:12 +00:00

LibWeb: Add the URL::host, URL::hostname & URL:port attributes

This commit is contained in:
Idan Horowitz 2021-09-14 00:21:29 +03:00 committed by Andreas Kling
parent e89320887e
commit 7f9818bcbc
3 changed files with 82 additions and 3 deletions

View file

@ -5,6 +5,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/URLParser.h>
#include <LibWeb/URL/URL.h>
namespace Web::URL {
@ -111,6 +112,75 @@ void URL::set_password(String const& password)
m_url.set_password(AK::URL::percent_encode(password, AK::URL::PercentEncodeSet::Userinfo));
}
String URL::host() const
{
// 1. Let url be thiss URL.
auto& url = m_url;
// 2. If urls host is null, then return the empty string.
if (url.host().is_null())
return String::empty();
// 3. If urls port is null, return urls host, serialized.
if (!url.port().has_value())
return url.host();
// 4. Return urls host, serialized, followed by U+003A (:) and urls port, serialized.
return String::formatted("{}:{}", url.host(), *url.port());
}
void URL::set_host(const String& host)
{
// 1. If thiss URLs cannot-be-a-base-URL is true, then return.
if (m_url.cannot_be_a_base_url())
return;
// 2. Basic URL parse the given value with thiss URL as url and host state as state override.
auto result_url = URLParser::parse(host, nullptr, m_url, URLParser::State::Host);
if (result_url.is_valid())
m_url = move(result_url);
}
String URL::hostname() const
{
// 1. If thiss URLs host is null, then return the empty string.
if (m_url.host().is_null())
return String::empty();
// 2. Return thiss URLs host, serialized.
return m_url.host();
}
void URL::set_hostname(String const& hostname)
{
// 1. If thiss URLs cannot-be-a-base-URL is true, then return.
if (m_url.cannot_be_a_base_url())
return;
// 2. Basic URL parse the given value with thiss URL as url and hostname state as state override.
auto result_url = URLParser::parse(hostname, nullptr, m_url, URLParser::State::Hostname);
if (result_url.is_valid())
m_url = move(result_url);
}
String URL::port() const
{
// 1. If thiss URLs port is null, then return the empty string.
if (!m_url.port().has_value())
return {};
// 2. Return thiss URLs port, serialized.
return String::formatted("{}", *m_url.port());
}
void URL::set_port(String const& port)
{
// 1. If thiss URL cannot have a username/password/port, then return.
if (m_url.cannot_have_a_username_or_password_or_port())
return;
// 2. If the given value is the empty string, then set thiss URLs port to null.
if (port.is_empty())
m_url.set_port({});
// 3. Otherwise, basic URL parse the given value with thiss URL as url and port state as state override.
auto result_url = URLParser::parse(port, nullptr, m_url, URLParser::State::Port);
if (result_url.is_valid())
m_url = move(result_url);
}
URLSearchParams const* URL::search_params() const
{
return m_query;