Skip to content

Commit

Permalink
http2: remove square brackets from parsed hostname
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
lpinca committed Jul 1, 2019
1 parent 9b77be4 commit 26b048e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
11 changes: 10 additions & 1 deletion lib/internal/http2/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -2778,7 +2778,16 @@ function connect(authority, options, listener) {
const protocol = authority.protocol || options.protocol || 'https:';
const port = '' + (authority.port !== '' ?
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;
if (typeof options.createConnection === 'function') {
Expand Down
30 changes: 29 additions & 1 deletion test/parallel/test-http2-connect.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
'use strict';

const { mustCall, hasCrypto, skip, expectsError } = require('../common');
const {
mustCall,
hasCrypto,
hasIPv6,
skip,
expectsError
} = require('../common');
if (!hasCrypto)
skip('missing crypto');
const { createServer, connect } = require('http2');
Expand Down Expand Up @@ -73,3 +79,25 @@ const { connect: netConnect } = require('net');
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();
}
}));
}
}));
}

0 comments on commit 26b048e

Please sign in to comment.