diff --git a/AK/URLParser.cpp b/AK/URLParser.cpp index 8105a65a72..1a634b0ae6 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 6e825c74ef..f506c535f4 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); };