url: make urlToOptions() handle IPv6 literals

Strip the enclosing square brackets from the parsed hostname.

PR-URL: https://github.com/nodejs/node/pull/19720
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
Luigi Pinca 2018-04-01 08:52:10 +02:00
parent d1156da815
commit fa2d43bd3e
2 changed files with 6 additions and 1 deletions

View File

@ -1314,7 +1314,9 @@ function domainToUnicode(domain) {
function urlToOptions(url) {
var options = {
protocol: url.protocol,
hostname: url.hostname,
hostname: url.hostname.startsWith('[') ?
url.hostname.slice(1, -1) :
url.hostname,
hash: url.hash,
search: url.search,
pathname: url.pathname,

View File

@ -143,6 +143,9 @@ assert.strictEqual(url.searchParams, oldParams);
assert.strictEqual(opts.pathname, '/aaa/zzz');
assert.strictEqual(opts.search, '?l=24');
assert.strictEqual(opts.hash, '#test');
const { hostname } = urlToOptions(new URL('http://[::1]:21'));
assert.strictEqual(hostname, '::1');
}
// Test special origins