From 768f070b863aadc949d1ac276b7b9a3a666b4497 Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Wed, 26 Jul 2023 21:05:35 +1200 Subject: [PATCH] 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. --- AK/URLParser.cpp | 20 ++++++++++++++++++++ AK/URLParser.h | 3 +++ 2 files changed, 23 insertions(+) diff --git a/AK/URLParser.cpp b/AK/URLParser.cpp index 8105a65a72b..1a634b0ae62 100644 --- a/AK/URLParser.cpp +++ b/AK/URLParser.cpp @@ -609,6 +609,26 @@ static Optional parse_host(StringView input, bool is_not_speci return ascii_domain; } +// https://url.spec.whatwg.org/#concept-host-serializer +ErrorOr 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()) + return serialize_ipv4_address(host.get()); + + // 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()) { + StringBuilder output; + TRY(output.try_append('[')); + serialize_ipv6_address(host.get(), 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(); +} + // https://url.spec.whatwg.org/#start-with-a-windows-drive-letter constexpr bool starts_with_windows_drive_letter(StringView input) { diff --git a/AK/URLParser.h b/AK/URLParser.h index 6e825c74eff..f506c535f4a 100644 --- a/AK/URLParser.h +++ b/AK/URLParser.h @@ -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 serialize_host(URL::Host const&); + private: static Optional parse_data_url(StringView raw_input); };