Skip to content

Commit

Permalink
url: fix file url reparse
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
watilde committed Oct 19, 2020
1 parent c55f661 commit 7afe3af
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 17 deletions.
6 changes: 0 additions & 6 deletions lib/internal/url.js
Expand Up @@ -419,8 +419,6 @@ ObjectDefineProperties(URL.prototype, {
domainToUnicode(this.hostname) : this.hostname;
if (ctx.port !== null)
ret += `:${ctx.port}`;
} else if (ctx.scheme === 'file:') {
ret += '//';
}
if (this[cannotBeBase]) {
ret += ctx.path[0];
Expand Down Expand Up @@ -504,10 +502,6 @@ ObjectDefineProperties(URL.prototype, {
if (scheme.length === 0)
return;
const ctx = this[context];
if (ctx.scheme === 'file:' &&
(ctx.host === '' || ctx.host === null)) {
return;
}
parse(scheme, kSchemeStart, null, ctx,
onParseProtocolComplete.bind(this));
}
Expand Down
12 changes: 4 additions & 8 deletions src/node_url.cc
Expand Up @@ -1461,13 +1461,11 @@ void URL::Parse(const char* input,
((buffer == "file:") &&
((url->flags & URL_FLAGS_HAS_USERNAME) ||
(url->flags & URL_FLAGS_HAS_PASSWORD) ||
(url->port != -1)))) {
(url->port != -1))) ||
(url->scheme == "file:" && url->host.empty())) {
url->flags |= URL_FLAGS_TERMINATED;
return;
}

// File scheme && (host == empty or null) check left to JS-land
// as it can be done before even entering C++ binding.
}

url->scheme = std::move(buffer);
Expand Down Expand Up @@ -1855,13 +1853,14 @@ void URL::Parse(const char* input,
break;
case kFile:
url->scheme = "file:";
url->host.clear();
url->flags |= URL_FLAGS_HAS_HOST;
if (ch == '/' || ch == '\\') {
state = kFileSlash;
} else if (has_base && base->scheme == "file:") {
switch (ch) {
case kEOL:
if (base->flags & URL_FLAGS_HAS_HOST) {
url->flags |= URL_FLAGS_HAS_HOST;
url->host = base->host;
}
if (base->flags & URL_FLAGS_HAS_PATH) {
Expand All @@ -1875,7 +1874,6 @@ void URL::Parse(const char* input,
break;
case '?':
if (base->flags & URL_FLAGS_HAS_HOST) {
url->flags |= URL_FLAGS_HAS_HOST;
url->host = base->host;
}
if (base->flags & URL_FLAGS_HAS_PATH) {
Expand All @@ -1888,7 +1886,6 @@ void URL::Parse(const char* input,
break;
case '#':
if (base->flags & URL_FLAGS_HAS_HOST) {
url->flags |= URL_FLAGS_HAS_HOST;
url->host = base->host;
}
if (base->flags & URL_FLAGS_HAS_PATH) {
Expand All @@ -1906,7 +1903,6 @@ void URL::Parse(const char* input,
default:
url->query.clear();
if (base->flags & URL_FLAGS_HAS_HOST) {
url->flags |= URL_FLAGS_HAS_HOST;
url->host = base->host;
}
if (base->flags & URL_FLAGS_HAS_PATH) {
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/wpt/README.md
Expand Up @@ -12,7 +12,7 @@ Last update:

- console: https://github.com/web-platform-tests/wpt/tree/3b1f72e99a/console
- encoding: https://github.com/web-platform-tests/wpt/tree/d7f9e16c9a/encoding
- url: https://github.com/web-platform-tests/wpt/tree/4e15edcc3c/url
- url: https://github.com/web-platform-tests/wpt/tree/33e4ac0902/url
- resources: https://github.com/web-platform-tests/wpt/tree/1d14e821b9/resources
- interfaces: https://github.com/web-platform-tests/wpt/tree/15e47f779c/interfaces
- html/webappapis/microtask-queuing: https://github.com/web-platform-tests/wpt/tree/2c5c3c4c27/html/webappapis/microtask-queuing
Expand Down
31 changes: 30 additions & 1 deletion test/fixtures/wpt/url/resources/urltestdata.json
Expand Up @@ -6091,7 +6091,7 @@
"base": "about:blank",
"failure": true
},
"# Additional file URL tetsts for (https://github.com/whatwg/url/issues/405)",
"# Additional file URL tests for (https://github.com/whatwg/url/issues/405)",
{
"input": "file://localhost//a//../..//foo",
"base": "about:blank",
Expand Down Expand Up @@ -6218,6 +6218,35 @@
"search": "",
"hash": ""
},
"File URL tests for https://github.com/whatwg/url/issues/549",
{
"input": "file:.//p",
"base": "about:blank",
"href": "file:////p",
"protocol": "file:",
"username": "",
"password": "",
"host": "",
"hostname": "",
"port": "",
"pathname": "//p",
"search": "",
"hash": ""
},
{
"input": "file:/.//p",
"base": "about:blank",
"href": "file:////p",
"protocol": "file:",
"username": "",
"password": "",
"host": "",
"hostname": "",
"port": "",
"pathname": "//p",
"search": "",
"hash": ""
},
"# IPv6 tests",
{
"input": "http://[1:0::]",
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/wpt/versions.json
Expand Up @@ -8,7 +8,7 @@
"path": "encoding"
},
"url": {
"commit": "4e15edcc3ca51c72c3846133c7b175ecd94b3a9b",
"commit": "33e4ac09029c463ea6ee57d6f33477a9043e98e8",
"path": "url"
},
"resources": {
Expand Down

0 comments on commit 7afe3af

Please sign in to comment.