url: fix WHATWG host formatting error

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: https://github.com/nodejs/node/pull/20493
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com>
This commit is contained in:
Yichao 'Peak' Ji 2018-05-03 15:13:25 +08:00 committed by Anatoli Papirovski
parent 8f6ab9f799
commit 2a96ee284c
No known key found for this signature in database
GPG Key ID: 614E2E1ABEB4B2C0
2 changed files with 8 additions and 1 deletions

View File

@ -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 += '//';
}

View File

@ -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'
);