1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 19:47:46 +00:00

AK: Serialize URL hosts with 'concept-host-serializer'

In order to follow spec text to achieve this, we need to change the
underlying representation of a host in AK::URL to deserialized format.
Before this, we were parsing the host and then immediately serializing
it again.

Making that change resulted in a whole bunch of fallout.

After this change, callers can access the serialized data through
this concept-host-serializer. The functional end result of this
change is that IPv6 hosts are now correctly serialized to be
surrounded with '[' and ']'.
This commit is contained in:
Shannon Booth 2023-07-27 21:40:41 +12:00 committed by Andreas Kling
parent 768f070b86
commit 8751be09f9
36 changed files with 175 additions and 143 deletions

View file

@ -44,7 +44,7 @@ static bool url_matches_about_blank(AK::URL const& url)
&& url.serialize_path() == "blank"sv
&& url.username().is_empty()
&& url.password().is_empty()
&& url.host().is_null();
&& url.host().has<Empty>();
}
// FIXME: This is an outdated older version of "determining the origin" and should be removed.

View file

@ -167,15 +167,15 @@ DeprecatedString HTMLHyperlinkElementUtils::host() const
auto& url = m_url;
// 3. If url or url's host is null, return the empty string.
if (!url.has_value() || url->host().is_null())
if (!url.has_value() || url->host().has<Empty>())
return DeprecatedString::empty();
// 4. If url's port is null, return url's host, serialized.
if (!url->port().has_value())
return url->host();
return url->serialized_host().release_value_but_fixme_should_propagate_errors().to_deprecated_string();
// 5. Return url's host, serialized, followed by ":" and url's port, serialized.
return DeprecatedString::formatted("{}:{}", url->host(), url->port().value());
return DeprecatedString::formatted("{}:{}", url->serialized_host().release_value_but_fixme_should_propagate_errors(), url->port().value());
}
// https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-host
@ -205,11 +205,14 @@ DeprecatedString HTMLHyperlinkElementUtils::hostname() const
// 1. Reinitialize url.
//
// 2. Let url be this element's url.
//
AK::URL url(href());
// 3. If url or url's host is null, return the empty string.
//
if (url.host().has<Empty>())
return DeprecatedString::empty();
// 4. Return url's host, serialized.
return AK::URL(href()).host();
return url.serialized_host().release_value_but_fixme_should_propagate_errors().to_deprecated_string();
}
void HTMLHyperlinkElementUtils::set_hostname(DeprecatedString hostname)

View file

@ -153,15 +153,15 @@ WebIDL::ExceptionOr<String> Location::host() const
auto url = this->url();
// 3. If url's host is null, return the empty string.
if (url.host().is_null())
if (url.host().has<Empty>())
return String {};
// 4. If url's port is null, return url's host, serialized.
if (!url.port().has_value())
return TRY_OR_THROW_OOM(vm, String::from_deprecated_string(url.host()));
return TRY_OR_THROW_OOM(vm, url.serialized_host());
// 5. Return url's host, serialized, followed by ":" and url's port, serialized.
return TRY_OR_THROW_OOM(vm, String::formatted("{}:{}", url.host(), *url.port()));
return TRY_OR_THROW_OOM(vm, String::formatted("{}:{}", TRY_OR_THROW_OOM(vm, url.serialized_host()), *url.port()));
}
WebIDL::ExceptionOr<void> Location::set_host(String const&)
@ -183,11 +183,11 @@ WebIDL::ExceptionOr<String> Location::hostname() const
auto url = this->url();
// 2. If this's url's host is null, return the empty string.
if (url.host().is_null())
if (url.host().has<Empty>())
return String {};
// 3. Return this's url's host, serialized.
return TRY_OR_THROW_OOM(vm, String::from_deprecated_string(url.host()));
return TRY_OR_THROW_OOM(vm, url.serialized_host());
}
WebIDL::ExceptionOr<void> Location::set_hostname(String const&)

View file

@ -217,7 +217,7 @@ static bool url_matches_about_blank(AK::URL const& url)
&& url.serialize_path() == "blank"sv
&& url.username().is_empty()
&& url.password().is_empty()
&& url.host().is_null();
&& url.host().has<Empty>();
}
// https://html.spec.whatwg.org/multipage/iframe-embed-object.html#shared-attribute-processing-steps-for-iframe-and-frame-elements

View file

@ -8,13 +8,15 @@
#pragma once
#include <AK/DeprecatedString.h>
#include <AK/URL.h>
#include <AK/URLParser.h>
namespace Web::HTML {
class Origin {
public:
Origin() = default;
Origin(DeprecatedString const& scheme, DeprecatedString const& host, u16 port)
Origin(DeprecatedString const& scheme, AK::URL::Host const& host, u16 port)
: m_scheme(scheme)
, m_host(host)
, m_port(port)
@ -22,10 +24,10 @@ public:
}
// https://html.spec.whatwg.org/multipage/origin.html#concept-origin-opaque
bool is_opaque() const { return m_scheme.is_null() && m_host.is_null() && m_port == 0; }
bool is_opaque() const { return m_scheme.is_null() && m_host.has<Empty>() && m_port == 0; }
DeprecatedString const& scheme() const { return m_scheme; }
DeprecatedString const& host() const { return m_host; }
AK::URL::Host const& host() const { return m_host; }
u16 port() const { return m_port; }
// https://html.spec.whatwg.org/multipage/origin.html#same-origin
@ -81,7 +83,7 @@ public:
result.append("://"sv);
// 4. Append origin's host, serialized, to result.
result.append(host());
result.append(URLParser::serialize_host(host()).release_value_but_fixme_should_propagate_errors().to_deprecated_string());
// 5. If origin's port is non-null, append a U+003A COLON character (:), and origin's port, serialized, to result.
if (port() != 0) {
@ -93,11 +95,11 @@ public:
}
// https://html.spec.whatwg.org/multipage/origin.html#concept-origin-effective-domain
Optional<DeprecatedString> effective_domain() const
Optional<AK::URL::Host> effective_domain() const
{
// 1. If origin is an opaque origin, then return null.
if (is_opaque())
return Optional<DeprecatedString> {};
return {};
// FIXME: 2. If origin's domain is non-null, then return origin's domain.
@ -109,7 +111,7 @@ public:
private:
DeprecatedString m_scheme;
DeprecatedString m_host;
AK::URL::Host m_host;
u16 m_port { 0 };
};
@ -120,7 +122,10 @@ template<>
struct Traits<Web::HTML::Origin> : public GenericTraits<Web::HTML::Origin> {
static unsigned hash(Web::HTML::Origin const& origin)
{
return pair_int_hash(origin.scheme().hash(), pair_int_hash(int_hash(origin.port()), origin.host().hash()));
auto hash_without_host = pair_int_hash(origin.scheme().hash(), origin.port());
if (origin.host().has<Empty>())
return hash_without_host;
return pair_int_hash(hash_without_host, URLParser::serialize_host(origin.host()).release_value_but_fixme_should_propagate_errors().hash());
}
};
} // namespace AK

View file

@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/URLParser.h>
#include <LibWeb/HTML/WorkerGlobalScope.h>
#include <LibWeb/HTML/WorkerLocation.h>
@ -43,15 +44,15 @@ WebIDL::ExceptionOr<String> WorkerLocation::host() const
auto const& url = m_global_scope->url();
// 2. If url's host is null, return the empty string.
if (url.host().is_empty())
if (url.host().has<Empty>())
return String {};
// 3. If url's port is null, return url's host, serialized.
if (!url.port().has_value())
return TRY_OR_THROW_OOM(vm, String::from_deprecated_string(url.host()));
return TRY_OR_THROW_OOM(vm, url.serialized_host());
// 4. Return url's host, serialized, followed by ":" and url's port, serialized.
return TRY_OR_THROW_OOM(vm, String::formatted("{}:{}", url.host().view(), url.port().value()));
return TRY_OR_THROW_OOM(vm, String::formatted("{}:{}", TRY_OR_THROW_OOM(vm, url.serialized_host()), url.port().value()));
}
// https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-hostname
@ -64,11 +65,11 @@ WebIDL::ExceptionOr<String> WorkerLocation::hostname() const
auto const& host = m_global_scope->url().host();
// 2. If host is null, return the empty string.
if (host.is_empty())
if (host.has<Empty>())
return String {};
// 3. Return host, serialized.
return TRY_OR_THROW_OOM(vm, String::from_deprecated_string(host));
return TRY_OR_THROW_OOM(vm, URLParser::serialize_host(host));
}
// https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-port