Skip to content

Commit c3d0936

Browse files
watildejoesepi
authored andcommitted
url: file URL path normalization
Refs: whatwg/url#544 Refs: web-platform-tests/wpt#25716 PR-URL: nodejs#35477 Fixes: nodejs#35429 Reviewed-By: Guy Bedford <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: David Carlier <[email protected]> Reviewed-By: Bradley Farias <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>
1 parent 441e364 commit c3d0936

File tree

8 files changed

+214
-101
lines changed

8 files changed

+214
-101
lines changed

lib/internal/bootstrap/pre_execution.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ function initializePolicy() {
362362
// no bare specifiers for now
363363
let manifestURL;
364364
if (require('path').isAbsolute(experimentalPolicy)) {
365-
manifestURL = new URL(`file:///${experimentalPolicy}`);
365+
manifestURL = new URL(experimentalPolicy, 'file://');
366366
} else {
367367
const cwdURL = pathToFileURL(process.cwd());
368368
cwdURL.pathname += '/';

src/inspector_agent.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ class NodeInspectorClient : public V8InspectorClient {
640640
node::url::URL url = node::url::URL::FromFilePath(resource_name);
641641
// TODO(ak239spb): replace this code with url.href().
642642
// Refs: https://github.com/nodejs/node/issues/22610
643-
return Utf8ToStringView(url.protocol() + "//" + url.path());
643+
return Utf8ToStringView(url.protocol() + "/" + url.path());
644644
}
645645

646646
node::Environment* env_;

src/node_url.cc

+19-33
Original file line numberDiff line numberDiff line change
@@ -1904,16 +1904,19 @@ void URL::Parse(const char* input,
19041904
state = kFragment;
19051905
break;
19061906
default:
1907+
url->query.clear();
1908+
if (base->flags & URL_FLAGS_HAS_HOST) {
1909+
url->flags |= URL_FLAGS_HAS_HOST;
1910+
url->host = base->host;
1911+
}
1912+
if (base->flags & URL_FLAGS_HAS_PATH) {
1913+
url->flags |= URL_FLAGS_HAS_PATH;
1914+
url->path = base->path;
1915+
}
19071916
if (!StartsWithWindowsDriveLetter(p, end)) {
1908-
if (base->flags & URL_FLAGS_HAS_HOST) {
1909-
url->flags |= URL_FLAGS_HAS_HOST;
1910-
url->host = base->host;
1911-
}
1912-
if (base->flags & URL_FLAGS_HAS_PATH) {
1913-
url->flags |= URL_FLAGS_HAS_PATH;
1914-
url->path = base->path;
1915-
}
19161917
ShortenUrlPath(url);
1918+
} else {
1919+
url->path.clear();
19171920
}
19181921
state = kPath;
19191922
continue;
@@ -1927,20 +1930,13 @@ void URL::Parse(const char* input,
19271930
if (ch == '/' || ch == '\\') {
19281931
state = kFileHost;
19291932
} else {
1930-
if (has_base &&
1931-
base->scheme == "file:" &&
1932-
!StartsWithWindowsDriveLetter(p, end)) {
1933-
if (IsNormalizedWindowsDriveLetter(base->path[0])) {
1933+
if (has_base && base->scheme == "file:") {
1934+
url->flags |= URL_FLAGS_HAS_HOST;
1935+
url->host = base->host;
1936+
if (!StartsWithWindowsDriveLetter(p, end) &&
1937+
IsNormalizedWindowsDriveLetter(base->path[0])) {
19341938
url->flags |= URL_FLAGS_HAS_PATH;
19351939
url->path.push_back(base->path[0]);
1936-
} else {
1937-
if (base->flags & URL_FLAGS_HAS_HOST) {
1938-
url->flags |= URL_FLAGS_HAS_HOST;
1939-
url->host = base->host;
1940-
} else {
1941-
url->flags &= ~URL_FLAGS_HAS_HOST;
1942-
url->host.clear();
1943-
}
19441940
}
19451941
}
19461942
state = kPath;
@@ -2024,29 +2020,19 @@ void URL::Parse(const char* input,
20242020
url->path.empty() &&
20252021
buffer.size() == 2 &&
20262022
IsWindowsDriveLetter(buffer)) {
2027-
if ((url->flags & URL_FLAGS_HAS_HOST) &&
2028-
!url->host.empty()) {
2029-
url->host.clear();
2030-
url->flags |= URL_FLAGS_HAS_HOST;
2031-
}
20322023
buffer[1] = ':';
20332024
}
20342025
url->flags |= URL_FLAGS_HAS_PATH;
20352026
url->path.emplace_back(std::move(buffer));
20362027
}
20372028
buffer.clear();
2038-
if (url->scheme == "file:" &&
2039-
(ch == kEOL ||
2040-
ch == '?' ||
2041-
ch == '#')) {
2042-
while (url->path.size() > 1 && url->path[0].empty()) {
2043-
url->path.erase(url->path.begin());
2044-
}
2045-
}
20462029
if (ch == '?') {
20472030
url->flags |= URL_FLAGS_HAS_QUERY;
2031+
url->query.clear();
20482032
state = kQuery;
20492033
} else if (ch == '#') {
2034+
url->flags |= URL_FLAGS_HAS_FRAGMENT;
2035+
url->fragment.clear();
20502036
state = kFragment;
20512037
}
20522038
} else {

test/cctest/test_url.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,14 @@ TEST_F(URLTest, FromFilePath) {
148148
#else
149149
file_url = URL::FromFilePath("/");
150150
EXPECT_EQ("file:", file_url.protocol());
151-
EXPECT_EQ("/", file_url.path());
151+
EXPECT_EQ("//", file_url.path());
152152

153153
file_url = URL::FromFilePath("/a/b/c");
154154
EXPECT_EQ("file:", file_url.protocol());
155-
EXPECT_EQ("/a/b/c", file_url.path());
155+
EXPECT_EQ("//a/b/c", file_url.path());
156156

157157
file_url = URL::FromFilePath("/a/%%.js");
158158
EXPECT_EQ("file:", file_url.protocol());
159-
EXPECT_EQ("/a/%25%25.js", file_url.path());
159+
EXPECT_EQ("//a/%25%25.js", file_url.path());
160160
#endif
161161
}

test/fixtures/wpt/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Last update:
1212

1313
- console: https://github.com/web-platform-tests/wpt/tree/3b1f72e99a/console
1414
- encoding: https://github.com/web-platform-tests/wpt/tree/d7f9e16c9a/encoding
15-
- url: https://github.com/web-platform-tests/wpt/tree/e2ddf48b78/url
15+
- url: https://github.com/web-platform-tests/wpt/tree/050308a616/url
1616
- resources: https://github.com/web-platform-tests/wpt/tree/1d14e821b9/resources
1717
- interfaces: https://github.com/web-platform-tests/wpt/tree/15e47f779c/interfaces
1818
- html/webappapis/microtask-queuing: https://github.com/web-platform-tests/wpt/tree/2c5c3c4c27/html/webappapis/microtask-queuing

test/fixtures/wpt/url/resources/setters_tests.json

+6-6
Original file line numberDiff line numberDiff line change
@@ -1672,26 +1672,26 @@
16721672
"href": "file://monkey/",
16731673
"new_value": "\\\\",
16741674
"expected": {
1675-
"href": "file://monkey/",
1676-
"pathname": "/"
1675+
"href": "file://monkey//",
1676+
"pathname": "//"
16771677
}
16781678
},
16791679
{
16801680
"comment": "File URLs and (back)slashes",
16811681
"href": "file:///unicorn",
16821682
"new_value": "//\\/",
16831683
"expected": {
1684-
"href": "file:///",
1685-
"pathname": "/"
1684+
"href": "file://////",
1685+
"pathname": "////"
16861686
}
16871687
},
16881688
{
16891689
"comment": "File URLs and (back)slashes",
16901690
"href": "file:///unicorn",
16911691
"new_value": "//monkey/..//",
16921692
"expected": {
1693-
"href": "file:///",
1694-
"pathname": "/"
1693+
"href": "file://///",
1694+
"pathname": "///"
16951695
}
16961696
},
16971697
{

0 commit comments

Comments
 (0)