AK: Implement 'concept-host-serializer' in URL spec

This implementation will allow us to fix serialization of IPv6
addresses not being surrounded by '[' and ']'.

Nothing is calling this function yet - this will come in the next
(larger) commit where the underlying host representation inside of
AK::URL is changed from DeprecatedString to URL::Host.
This commit is contained in:
Shannon Booth 2023-07-26 21:05:35 +12:00 committed by Andreas Kling
parent 803ca8cc80
commit 768f070b86
2 changed files with 23 additions and 0 deletions

View file

@ -609,6 +609,26 @@ static Optional<DeprecatedString> parse_host(StringView input, bool is_not_speci
return ascii_domain;
}
// https://url.spec.whatwg.org/#concept-host-serializer
ErrorOr<String> URLParser::serialize_host(URL::Host const& host)
{
// 1. If host is an IPv4 address, return the result of running the IPv4 serializer on host.
if (host.has<URL::IPv4Address>())
return serialize_ipv4_address(host.get<URL::IPv4Address>());
// 2. Otherwise, if host is an IPv6 address, return U+005B ([), followed by the result of running the IPv6 serializer on host, followed by U+005D (]).
if (host.has<URL::IPv6Address>()) {
StringBuilder output;
TRY(output.try_append('['));
serialize_ipv6_address(host.get<URL::IPv6Address>(), output);
TRY(output.try_append(']'));
return output.to_string();
}
// 3. Otherwise, host is a domain, opaque host, or empty host, return host.
return host.get<String>();
}
// https://url.spec.whatwg.org/#start-with-a-windows-drive-letter
constexpr bool starts_with_windows_drive_letter(StringView input)
{

View file

@ -61,6 +61,9 @@ public:
// https://url.spec.whatwg.org/#string-percent-encode-after-encoding
static DeprecatedString percent_encode_after_encoding(StringView input, URL::PercentEncodeSet percent_encode_set, bool space_as_plus = false);
// https://url.spec.whatwg.org/#concept-host-serializer
static ErrorOr<String> serialize_host(URL::Host const&);
private:
static Optional<URL> parse_data_url(StringView raw_input);
};