http2: remove square brackets from parsed hostname
Make `http2.connect()` work when using URLs with literal IPv6 addresses. Fixes: https://github.com/nodejs/node/issues/28216 PR-URL: https://github.com/nodejs/node/pull/28406 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Jiawen Geng <technicalcute@gmail.com>
This commit is contained in:
parent
9b77be4814
commit
26b048effb
@ -2778,7 +2778,16 @@ function connect(authority, options, listener) {
|
|||||||
const protocol = authority.protocol || options.protocol || 'https:';
|
const protocol = authority.protocol || options.protocol || 'https:';
|
||||||
const port = '' + (authority.port !== '' ?
|
const port = '' + (authority.port !== '' ?
|
||||||
authority.port : (authority.protocol === 'http:' ? 80 : 443));
|
authority.port : (authority.protocol === 'http:' ? 80 : 443));
|
||||||
const host = authority.hostname || authority.host || 'localhost';
|
let host = 'localhost';
|
||||||
|
|
||||||
|
if (authority.hostname) {
|
||||||
|
host = authority.hostname;
|
||||||
|
|
||||||
|
if (host[0] === '[')
|
||||||
|
host = host.slice(1, -1);
|
||||||
|
} else if (authority.host) {
|
||||||
|
host = authority.host;
|
||||||
|
}
|
||||||
|
|
||||||
let socket;
|
let socket;
|
||||||
if (typeof options.createConnection === 'function') {
|
if (typeof options.createConnection === 'function') {
|
||||||
|
@ -1,6 +1,12 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const { mustCall, hasCrypto, skip, expectsError } = require('../common');
|
const {
|
||||||
|
mustCall,
|
||||||
|
hasCrypto,
|
||||||
|
hasIPv6,
|
||||||
|
skip,
|
||||||
|
expectsError
|
||||||
|
} = require('../common');
|
||||||
if (!hasCrypto)
|
if (!hasCrypto)
|
||||||
skip('missing crypto');
|
skip('missing crypto');
|
||||||
const { createServer, connect } = require('http2');
|
const { createServer, connect } = require('http2');
|
||||||
@ -73,3 +79,25 @@ const { connect: netConnect } = require('net');
|
|||||||
type: Error
|
type: Error
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check for literal IPv6 addresses in URL's
|
||||||
|
if (hasIPv6) {
|
||||||
|
const server = createServer();
|
||||||
|
server.listen(0, '::1', mustCall(() => {
|
||||||
|
const { port } = server.address();
|
||||||
|
const clients = new Set();
|
||||||
|
|
||||||
|
clients.add(connect(`http://[::1]:${port}`));
|
||||||
|
clients.add(connect(new URL(`http://[::1]:${port}`)));
|
||||||
|
|
||||||
|
for (const client of clients) {
|
||||||
|
client.once('connect', mustCall(() => {
|
||||||
|
client.close();
|
||||||
|
clients.delete(client);
|
||||||
|
if (clients.size === 0) {
|
||||||
|
server.close();
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user