Skip to content

Commit fd4d1e2

Browse files
lpincatargos
authored andcommitted
http2: remove square brackets from parsed hostname
Make `http2.connect()` work when using URLs with literal IPv6 addresses. Fixes: #28216 PR-URL: #28406 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Jiawen Geng <[email protected]>
1 parent 0111c61 commit fd4d1e2

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

lib/internal/http2/core.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -2782,7 +2782,16 @@ function connect(authority, options, listener) {
27822782
const protocol = authority.protocol || options.protocol || 'https:';
27832783
const port = '' + (authority.port !== '' ?
27842784
authority.port : (authority.protocol === 'http:' ? 80 : 443));
2785-
const host = authority.hostname || authority.host || 'localhost';
2785+
let host = 'localhost';
2786+
2787+
if (authority.hostname) {
2788+
host = authority.hostname;
2789+
2790+
if (host[0] === '[')
2791+
host = host.slice(1, -1);
2792+
} else if (authority.host) {
2793+
host = authority.host;
2794+
}
27862795

27872796
let socket;
27882797
if (typeof options.createConnection === 'function') {

test/parallel/test-http2-connect.js

+29-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
'use strict';
22

3-
const { mustCall, hasCrypto, skip, expectsError } = require('../common');
3+
const {
4+
mustCall,
5+
hasCrypto,
6+
hasIPv6,
7+
skip,
8+
expectsError
9+
} = require('../common');
410
if (!hasCrypto)
511
skip('missing crypto');
612
const { createServer, connect } = require('http2');
@@ -73,3 +79,25 @@ const { connect: netConnect } = require('net');
7379
type: Error
7480
});
7581
}
82+
83+
// Check for literal IPv6 addresses in URL's
84+
if (hasIPv6) {
85+
const server = createServer();
86+
server.listen(0, '::1', mustCall(() => {
87+
const { port } = server.address();
88+
const clients = new Set();
89+
90+
clients.add(connect(`http://[::1]:${port}`));
91+
clients.add(connect(new URL(`http://[::1]:${port}`)));
92+
93+
for (const client of clients) {
94+
client.once('connect', mustCall(() => {
95+
client.close();
96+
clients.delete(client);
97+
if (clients.size === 0) {
98+
server.close();
99+
}
100+
}));
101+
}
102+
}));
103+
}

0 commit comments

Comments
 (0)