Skip to content

Update opaque_path, ensure opaque URL paths always roundtrip #925

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 25, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -619,10 +619,18 @@ result_type parse_url_impl(std::string_view user_input,
input_position = input_size + 1;
}
url.has_opaque_path = true;

// This is a really unlikely scenario in real world. We should not seek
// to optimize it.
url.update_base_pathname(unicode::percent_encode(
view, character_sets::C0_CONTROL_PERCENT_ENCODE));
if (!view.empty() && view.back() == ' ') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!view.empty() && view.back() == ' ') {
if (view.ends_with(' ')) {

std::string modified_view =
std::string(view.begin(), view.end() - 1) + "%20";
url.update_base_pathname(unicode::percent_encode(
modified_view, character_sets::C0_CONTROL_PERCENT_ENCODE));
} else {
url.update_base_pathname(unicode::percent_encode(
view, character_sets::C0_CONTROL_PERCENT_ENCODE));
}
break;
}
case state::PORT: {
Expand Down
8 changes: 4 additions & 4 deletions tests/basic_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ TYPED_TEST(basic_tests, nodejs2) {
ASSERT_EQ(url->get_search(), "?test");
url->set_search("");
ASSERT_EQ(url->get_search(), "");
ASSERT_EQ(url->get_pathname(), "space");
ASSERT_EQ(url->get_href(), "data:space");
ASSERT_EQ(url->get_pathname(), "space %20");
ASSERT_EQ(url->get_href(), "data:space %20");
SUCCEED();
}

Expand All @@ -206,8 +206,8 @@ TYPED_TEST(basic_tests, nodejs3) {
ASSERT_EQ(url->get_search(), "?test");
url->set_search("");
ASSERT_EQ(url->get_search(), "");
ASSERT_EQ(url->get_pathname(), "space ");
ASSERT_EQ(url->get_href(), "data:space #test");
ASSERT_EQ(url->get_pathname(), "space %20");
ASSERT_EQ(url->get_href(), "data:space %20#test");
SUCCEED();
}

Expand Down
2 changes: 1 addition & 1 deletion tests/wpt/ada_extra_setters_tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@
"new_value": "",
"expected": {
"search": "",
"pathname": "space"
"pathname": "space %20"
}
}
]
Expand Down
4 changes: 2 additions & 2 deletions tests/wpt/ada_extra_urltestdata.json
Original file line number Diff line number Diff line change
Expand Up @@ -224,15 +224,15 @@
{
"input": "data:space ?test#test",
"base": "about:blank",
"href": "data:space ?test#test",
"href": "data:space %20?test#test",
"origin": "null",
"protocol": "data:",
"username": "",
"password": "",
"host": "",
"hostname": "",
"port": "",
"pathname": "space ",
"pathname": "space %20",
"search": "?test",
"hash": "#test"
},
Expand Down
68 changes: 52 additions & 16 deletions tests/wpt/setters_tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -1177,6 +1177,24 @@
"host": "test.invalid",
"hostname": "test.invalid"
}
},
{
"href": "https://test.invalid/",
"new_value": "test/@aaa",
"expected": {
"href": "https://test/",
"host": "test",
"hostname": "test"
}
},
{
"href": "https://test.invalid/",
"new_value": "test/:aaa",
"expected": {
"href": "https://test/",
"host": "test",
"hostname": "test"
}
}
],
"hostname": [
Expand Down Expand Up @@ -1624,6 +1642,24 @@
"host": "test.invalid",
"hostname": "test.invalid"
}
},
{
"href": "https://test.invalid/",
"new_value": "test/@aaa",
"expected": {
"href": "https://test/",
"host": "test",
"hostname": "test"
}
},
{
"href": "https://test.invalid/",
"new_value": "test/:aaa",
"expected": {
"href": "https://test/",
"host": "test",
"hostname": "test"
}
}
],
"port": [
Expand Down Expand Up @@ -2241,38 +2277,38 @@
}
},
{
"comment": "Drop trailing spaces from trailing opaque paths",
"comment": "Trailing spaces and opaque paths",
"href": "data:space ?query",
"new_value": "",
"expected": {
"href": "data:space",
"pathname": "space",
"href": "data:space%20",
"pathname": "space%20",
"search": ""
}
},
{
"href": "sc:space ?query",
"new_value": "",
"expected": {
"href": "sc:space",
"pathname": "space",
"href": "sc:space%20",
"pathname": "space%20",
"search": ""
}
},
{
"comment": "Do not drop trailing spaces from non-trailing opaque paths",
"comment": "Trailing spaces and opaque paths",
"href": "data:space ?query#fragment",
"new_value": "",
"expected": {
"href": "data:space #fragment",
"href": "data:space %20#fragment",
"search": ""
}
},
{
"href": "sc:space ?query#fragment",
"new_value": "",
"expected": {
"href": "sc:space #fragment",
"href": "sc:space %20#fragment",
"search": ""
}
},
Expand Down Expand Up @@ -2429,38 +2465,38 @@
}
},
{
"comment": "Drop trailing spaces from trailing opaque paths",
"comment": "Trailing spaces and opaque paths",
"href": "data:space #fragment",
"new_value": "",
"expected": {
"href": "data:space",
"pathname": "space",
"href": "data:space %20",
"pathname": "space %20",
"hash": ""
}
},
{
"href": "sc:space #fragment",
"new_value": "",
"expected": {
"href": "sc:space",
"pathname": "space",
"href": "sc:space %20",
"pathname": "space %20",
"hash": ""
}
},
{
"comment": "Do not drop trailing spaces from non-trailing opaque paths",
"comment": "Trailing spaces and opaque paths",
"href": "data:space ?query#fragment",
"new_value": "",
"expected": {
"href": "data:space ?query",
"href": "data:space %20?query",
"hash": ""
}
},
{
"href": "sc:space ?query#fragment",
"new_value": "",
"expected": {
"href": "sc:space ?query",
"href": "sc:space %20?query",
"hash": ""
}
},
Expand Down
12 changes: 9 additions & 3 deletions tests/wpt/toascii.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
[
"This contains assorted IDNA tests that IdnaTestV2 might not cover.",
"Feel free to deduplicate with a clear commit message.",
"",
"If the test only applies to the URL Standard's 'domain to ASCII', ",
"and not to TR46's ToASCII, then tag it with `urlStandardOnly`",
{
"comment": "Label with hyphens in 3rd and 4th position",
"input": "aa--",
Expand Down Expand Up @@ -239,19 +242,22 @@
},
{
"input": "www.lookout.net\u2A7480",
"output": null
"output": null,
"urlStandardOnly": true
},
{
"input": "www\u00A0.lookout.net",
"output": null
"output": null,
"urlStandardOnly": true
},
{
"input": "\u1680lookout.net",
"output": null
},
{
"input": "\u001flookout.net",
"output": null
"output": null,
"urlStandardOnly": true
},
{
"input": "look\u06DDout.net",
Expand Down
120 changes: 120 additions & 0 deletions tests/wpt/urltestdata.json
Original file line number Diff line number Diff line change
Expand Up @@ -3778,6 +3778,126 @@
"search": "",
"hash": ""
},
{
"input": "non-special:opaque ",
"base": null,
"href": "non-special:opaque",
"origin": "null",
"protocol": "non-special:",
"username": "",
"password": "",
"host": "",
"hostname": "",
"port": "",
"pathname": "opaque",
"search": "",
"hash": ""
},
{
"input": "non-special:opaque ?hi",
"base": null,
"href": "non-special:opaque %20?hi",
"origin": "null",
"protocol": "non-special:",
"username": "",
"password": "",
"host": "",
"hostname": "",
"port": "",
"pathname": "opaque %20",
"search": "?hi",
"hash": ""
},
{
"input": "non-special:opaque #hi",
"base": null,
"href": "non-special:opaque %20#hi",
"origin": "null",
"protocol": "non-special:",
"username": "",
"password": "",
"host": "",
"hostname": "",
"port": "",
"pathname": "opaque %20",
"search": "",
"hash": "#hi"
},
{
"input": "non-special:opaque x?hi",
"base": null,
"href": "non-special:opaque x?hi",
"origin": "null",
"protocol": "non-special:",
"username": "",
"password": "",
"host": "",
"hostname": "",
"port": "",
"pathname": "opaque x",
"search": "?hi",
"hash": ""
},
{
"input": "non-special:opaque x#hi",
"base": null,
"href": "non-special:opaque x#hi",
"origin": "null",
"protocol": "non-special:",
"username": "",
"password": "",
"host": "",
"hostname": "",
"port": "",
"pathname": "opaque x",
"search": "",
"hash": "#hi"
},
{
"input": "non-special:opaque \t\t \t#hi",
"base": null,
"href": "non-special:opaque %20#hi",
"origin": "null",
"protocol": "non-special:",
"username": "",
"password": "",
"host": "",
"hostname": "",
"port": "",
"pathname": "opaque %20",
"search": "",
"hash": "#hi"
},
{
"input": "non-special:opaque \t\t #hi",
"base": null,
"href": "non-special:opaque %20#hi",
"origin": "null",
"protocol": "non-special:",
"username": "",
"password": "",
"host": "",
"hostname": "",
"port": "",
"pathname": "opaque %20",
"search": "",
"hash": "#hi"
},
{
"input": "non-special:opaque\t\t \r #hi",
"base": null,
"href": "non-special:opaque %20#hi",
"origin": "null",
"protocol": "non-special:",
"username": "",
"password": "",
"host": "",
"hostname": "",
"port": "",
"pathname": "opaque %20",
"search": "",
"hash": "#hi"
},
"Ideographic full stop (full-width period for Chinese, etc.) should be treated as a dot. U+3002 is mapped to U+002E (dot)",
{
"input": "http://www.foo。bar.com",
Expand Down
Loading