Skip to content

Commit

Permalink
url: fix WHATWG host formatting error
Browse files Browse the repository at this point in the history
The current url.format implementation will return an invalid URL string
without the host if there is a port and unicode: true.

This unexpected behavior is caused by domainToUnicode, which expects
a hostname instead of a host string according to node_url.cc.

Adds both a fix and a test for the issue.

PR-URL: #20493
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Anatoli Papirovski <[email protected]>
Reviewed-By: Trivikram Kamat <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Tiancheng "Timothy" Gu <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Daijiro Wachi <[email protected]>
  • Loading branch information
peakji authored and MylesBorins committed May 9, 2018
1 parent 4c4be85 commit ff619d3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,9 @@ Object.defineProperties(URL.prototype, {
ret += '@';
}
ret += options.unicode ?
domainToUnicode(this.host) : this.host;
domainToUnicode(this.hostname) : this.hostname;
if (ctx.port !== null)
ret += `:${ctx.port}`;
} else if (ctx.scheme === 'file:') {
ret += '//';
}
Expand Down
5 changes: 5 additions & 0 deletions test/parallel/test-url-format-whatwg.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,8 @@ assert.strictEqual(
url.format(myURL, { unicode: 0 }),
'http://xn--lck1c3crb1723bpq4a.com/a?a=b#c'
);

assert.strictEqual(
url.format(new URL('http://xn--0zwm56d.com:8080/path'), { unicode: true }),
'http://测试.com:8080/path'
);

0 comments on commit ff619d3

Please sign in to comment.