Skip to content

Commit

Permalink
test: reproduce and fix node.js bug (#511)
Browse files Browse the repository at this point in the history
* test: reproduce node.js bug

* fix: add missing special scheme check for setter

* Fix.

* formatting.

---------

Co-authored-by: Daniel Lemire <[email protected]>
  • Loading branch information
anonrig and lemire authored Sep 19, 2023
1 parent b92b825 commit f4dc343
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
4 changes: 3 additions & 1 deletion include/ada/url_aggregator-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -807,7 +807,9 @@ inline bool url_aggregator::has_hostname() const noexcept {

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

inline bool url_aggregator::has_dash_dot() const noexcept {
Expand Down
2 changes: 1 addition & 1 deletion src/url-setters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ bool url::set_host_or_hostname(const std::string_view input) {
}

// Let host be the result of host parsing host_view with url is not special.
if (host_view.empty()) {
if (host_view.empty() && !is_special()) {
host = "";
return true;
}
Expand Down
5 changes: 2 additions & 3 deletions src/url_aggregator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -552,13 +552,12 @@ bool url_aggregator::set_host_or_hostname(const std::string_view input) {
// empty string, and either url includes credentials or url's port is
// non-null, return.
else if (host_view.empty() &&
(is_special() || has_credentials() ||
components.port != url_components::omitted)) {
(is_special() || has_credentials() || has_port())) {
return false;
}

// Let host be the result of host parsing host_view with url is not special.
if (host_view.empty()) {
if (host_view.empty() && !is_special()) {
if (has_hostname()) {
clear_hostname(); // easy!
} else if (has_dash_dot()) {
Expand Down
9 changes: 9 additions & 0 deletions tests/basic_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -378,3 +378,12 @@ TYPED_TEST(basic_tests, url_host_type) {
ada::url_host_type::IPV6);
SUCCEED();
}

// https://github.com/nodejs/node/issues/49650
TYPED_TEST(basic_tests, nodejs_49650) {
auto out = ada::parse<TypeParam>("http://foo");
ASSERT_TRUE(out);
ASSERT_FALSE(out->set_host("::"));
ASSERT_EQ(out->get_href(), "http://foo/");
SUCCEED();
}

0 comments on commit f4dc343

Please sign in to comment.