Skip to content

Commit f4dc343

Browse files
anonriglemire
andauthored
test: reproduce and fix node.js bug (#511)
* test: reproduce node.js bug * fix: add missing special scheme check for setter * Fix. * formatting. --------- Co-authored-by: Daniel Lemire <[email protected]>
1 parent b92b825 commit f4dc343

File tree

4 files changed

+15
-5
lines changed

4 files changed

+15
-5
lines changed

include/ada/url_aggregator-inl.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,9 @@ inline bool url_aggregator::has_hostname() const noexcept {
807807

808808
inline bool url_aggregator::has_port() const noexcept {
809809
ada_log("url_aggregator::has_port");
810-
return components.pathname_start != components.host_end;
810+
// A URL cannot have a username/password/port if its host is null or the empty
811+
// string, or its scheme is "file".
812+
return has_hostname() && components.pathname_start != components.host_end;
811813
}
812814

813815
inline bool url_aggregator::has_dash_dot() const noexcept {

src/url-setters.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ bool url::set_host_or_hostname(const std::string_view input) {
5555
}
5656

5757
// Let host be the result of host parsing host_view with url is not special.
58-
if (host_view.empty()) {
58+
if (host_view.empty() && !is_special()) {
5959
host = "";
6060
return true;
6161
}

src/url_aggregator.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -552,13 +552,12 @@ bool url_aggregator::set_host_or_hostname(const std::string_view input) {
552552
// empty string, and either url includes credentials or url's port is
553553
// non-null, return.
554554
else if (host_view.empty() &&
555-
(is_special() || has_credentials() ||
556-
components.port != url_components::omitted)) {
555+
(is_special() || has_credentials() || has_port())) {
557556
return false;
558557
}
559558

560559
// Let host be the result of host parsing host_view with url is not special.
561-
if (host_view.empty()) {
560+
if (host_view.empty() && !is_special()) {
562561
if (has_hostname()) {
563562
clear_hostname(); // easy!
564563
} else if (has_dash_dot()) {

tests/basic_tests.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,3 +378,12 @@ TYPED_TEST(basic_tests, url_host_type) {
378378
ada::url_host_type::IPV6);
379379
SUCCEED();
380380
}
381+
382+
// https://github.com/nodejs/node/issues/49650
383+
TYPED_TEST(basic_tests, nodejs_49650) {
384+
auto out = ada::parse<TypeParam>("http://foo");
385+
ASSERT_TRUE(out);
386+
ASSERT_FALSE(out->set_host("::"));
387+
ASSERT_EQ(out->get_href(), "http://foo/");
388+
SUCCEED();
389+
}

0 commit comments

Comments
 (0)