Skip to content
Permalink
Browse files

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 <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>
  • Loading branch information...
lpinca authored and targos committed Jun 24, 2019
1 parent 0111c61 commit fd4d1e20f30d7314b4f6895ae41d227a9c0f90e3
Showing with 39 additions and 2 deletions.
  1. +10 −1 lib/internal/http2/core.js
  2. +29 −1 test/parallel/test-http2-connect.js
@@ -2782,7 +2782,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') {
@@ -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');
@@ -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 fd4d1e2

Please sign in to comment.
You can’t perform that action at this time.