srctree

Robin Linden parent 4c4aa77f 56face44
url: Fix assert() in parse_host w/ empty nonspecial input

https://url.spec.whatwg.org/#concept-host-parser says that the assertshould come after the check for is_not_special, so this change conformsto what the spec wants.

inlinesplit
url/url.cpp added: 13, removed: 4, total 9
@@ -1271,8 +1271,6 @@ void UrlParser::shorten_url_path(Url &url) const {
 
// https://url.spec.whatwg.org/#concept-host-parser
std::optional<Host> UrlParser::parse_host(std::string_view input, bool is_not_special) const {
assert(!input.empty());
 
if (input.starts_with("[")) {
if (!input.ends_with("]")) {
validation_error(ValidationError::IPv6Unclosed);
@@ -1302,6 +1300,8 @@ std::optional<Host> UrlParser::parse_host(std::string_view input, bool is_not_sp
return Host{HostType::Opaque, *host};
}
 
assert(!input.empty());
 
std::string domain = util::percent_decode(input);
 
std::optional<std::string> ascii_domain = domain_to_ascii(domain, false);
 
url/url_test.cpp added: 13, removed: 4, total 9
@@ -1,4 +1,5 @@
// SPDX-FileCopyrightText: 2023 David Zero <zero-one@zer0-one.net>
// SPDX-FileCopyrightText: 2023 Robin Lindén <dev@robinlinden.eu>
//
// SPDX-License-Identifier: BSD-2-Clause
 
@@ -625,6 +626,14 @@ int main() {
etest::expect_eq(o2.serialize(), "null");
});
 
etest::test("URL parsing: parse_host w/ empty input", [] {
url::UrlParser p;
auto url = p.parse("a://");
 
etest::require(url.has_value());
etest::expect_eq(*url, url::Url{.scheme = "a", .host = url::Host{.type = url::HostType::Opaque}});
});
 
int ret = etest::run_all_tests();
 
url::icu_cleanup();