Skip to content

Commit 7afe3af

Browse files
committed
url: fix file url reparse
Fixes: #35571 Refs: whatwg/url#550 Refs: web-platform-tests/wpt#25989 PR-URL: #35671 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
1 parent c55f661 commit 7afe3af

File tree

5 files changed

+36
-17
lines changed

5 files changed

+36
-17
lines changed

lib/internal/url.js

-6
Original file line numberDiff line numberDiff line change
@@ -419,8 +419,6 @@ ObjectDefineProperties(URL.prototype, {
419419
domainToUnicode(this.hostname) : this.hostname;
420420
if (ctx.port !== null)
421421
ret += `:${ctx.port}`;
422-
} else if (ctx.scheme === 'file:') {
423-
ret += '//';
424422
}
425423
if (this[cannotBeBase]) {
426424
ret += ctx.path[0];
@@ -504,10 +502,6 @@ ObjectDefineProperties(URL.prototype, {
504502
if (scheme.length === 0)
505503
return;
506504
const ctx = this[context];
507-
if (ctx.scheme === 'file:' &&
508-
(ctx.host === '' || ctx.host === null)) {
509-
return;
510-
}
511505
parse(scheme, kSchemeStart, null, ctx,
512506
onParseProtocolComplete.bind(this));
513507
}

src/node_url.cc

+4-8
Original file line numberDiff line numberDiff line change
@@ -1461,13 +1461,11 @@ void URL::Parse(const char* input,
14611461
((buffer == "file:") &&
14621462
((url->flags & URL_FLAGS_HAS_USERNAME) ||
14631463
(url->flags & URL_FLAGS_HAS_PASSWORD) ||
1464-
(url->port != -1)))) {
1464+
(url->port != -1))) ||
1465+
(url->scheme == "file:" && url->host.empty())) {
14651466
url->flags |= URL_FLAGS_TERMINATED;
14661467
return;
14671468
}
1468-
1469-
// File scheme && (host == empty or null) check left to JS-land
1470-
// as it can be done before even entering C++ binding.
14711469
}
14721470

14731471
url->scheme = std::move(buffer);
@@ -1855,13 +1853,14 @@ void URL::Parse(const char* input,
18551853
break;
18561854
case kFile:
18571855
url->scheme = "file:";
1856+
url->host.clear();
1857+
url->flags |= URL_FLAGS_HAS_HOST;
18581858
if (ch == '/' || ch == '\\') {
18591859
state = kFileSlash;
18601860
} else if (has_base && base->scheme == "file:") {
18611861
switch (ch) {
18621862
case kEOL:
18631863
if (base->flags & URL_FLAGS_HAS_HOST) {
1864-
url->flags |= URL_FLAGS_HAS_HOST;
18651864
url->host = base->host;
18661865
}
18671866
if (base->flags & URL_FLAGS_HAS_PATH) {
@@ -1875,7 +1874,6 @@ void URL::Parse(const char* input,
18751874
break;
18761875
case '?':
18771876
if (base->flags & URL_FLAGS_HAS_HOST) {
1878-
url->flags |= URL_FLAGS_HAS_HOST;
18791877
url->host = base->host;
18801878
}
18811879
if (base->flags & URL_FLAGS_HAS_PATH) {
@@ -1888,7 +1886,6 @@ void URL::Parse(const char* input,
18881886
break;
18891887
case '#':
18901888
if (base->flags & URL_FLAGS_HAS_HOST) {
1891-
url->flags |= URL_FLAGS_HAS_HOST;
18921889
url->host = base->host;
18931890
}
18941891
if (base->flags & URL_FLAGS_HAS_PATH) {
@@ -1906,7 +1903,6 @@ void URL::Parse(const char* input,
19061903
default:
19071904
url->query.clear();
19081905
if (base->flags & URL_FLAGS_HAS_HOST) {
1909-
url->flags |= URL_FLAGS_HAS_HOST;
19101906
url->host = base->host;
19111907
}
19121908
if (base->flags & URL_FLAGS_HAS_PATH) {

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/4e15edcc3c/url
15+
- url: https://github.com/web-platform-tests/wpt/tree/33e4ac0902/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/urltestdata.json

+30-1
Original file line numberDiff line numberDiff line change
@@ -6091,7 +6091,7 @@
60916091
"base": "about:blank",
60926092
"failure": true
60936093
},
6094-
"# Additional file URL tetsts for (https://github.com/whatwg/url/issues/405)",
6094+
"# Additional file URL tests for (https://github.com/whatwg/url/issues/405)",
60956095
{
60966096
"input": "file://localhost//a//../..//foo",
60976097
"base": "about:blank",
@@ -6218,6 +6218,35 @@
62186218
"search": "",
62196219
"hash": ""
62206220
},
6221+
"File URL tests for https://github.com/whatwg/url/issues/549",
6222+
{
6223+
"input": "file:.//p",
6224+
"base": "about:blank",
6225+
"href": "file:////p",
6226+
"protocol": "file:",
6227+
"username": "",
6228+
"password": "",
6229+
"host": "",
6230+
"hostname": "",
6231+
"port": "",
6232+
"pathname": "//p",
6233+
"search": "",
6234+
"hash": ""
6235+
},
6236+
{
6237+
"input": "file:/.//p",
6238+
"base": "about:blank",
6239+
"href": "file:////p",
6240+
"protocol": "file:",
6241+
"username": "",
6242+
"password": "",
6243+
"host": "",
6244+
"hostname": "",
6245+
"port": "",
6246+
"pathname": "//p",
6247+
"search": "",
6248+
"hash": ""
6249+
},
62216250
"# IPv6 tests",
62226251
{
62236252
"input": "http://[1:0::]",

test/fixtures/wpt/versions.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"path": "encoding"
99
},
1010
"url": {
11-
"commit": "4e15edcc3ca51c72c3846133c7b175ecd94b3a9b",
11+
"commit": "33e4ac09029c463ea6ee57d6f33477a9043e98e8",
1212
"path": "url"
1313
},
1414
"resources": {

0 commit comments

Comments
 (0)