From 44daa79f5afb12e9fec42b430da0a11e9e7d23d0 Mon Sep 17 00:00:00 2001 From: Luigi Pinca Date: Mon, 24 Jun 2019 18:15:58 +0200 Subject: [PATCH 1/3] 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 --- lib/internal/http2/core.js | 13 ++++++++++++- test/parallel/test-http2-connect.js | 30 ++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/lib/internal/http2/core.js b/lib/internal/http2/core.js index 8c05ff626792d7..1b1d6ad9cfc2fd 100644 --- a/lib/internal/http2/core.js +++ b/lib/internal/http2/core.js @@ -2774,7 +2774,18 @@ 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 = ''; + + if (authority.hostname) { + host = authority.hostname; + + if (host.startsWith('[')) + host = host.slice(1, -1); + } else if (authority.host) { + host = authority.host; + } else { + host = 'localhost'; + } let socket; if (typeof options.createConnection === 'function') { diff --git a/test/parallel/test-http2-connect.js b/test/parallel/test-http2-connect.js index a2f30f974e9329..2137ef28926726 100644 --- a/test/parallel/test-http2-connect.js +++ b/test/parallel/test-http2-connect.js @@ -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(); + } + })); + } + })); +} From 9111e8a0acc453f2d57834567cf5bda4c1b50624 Mon Sep 17 00:00:00 2001 From: Luigi Pinca Date: Tue, 25 Jun 2019 18:31:40 +0200 Subject: [PATCH 2/3] fixup! http2: remove square brackets from parsed hostname --- lib/internal/http2/core.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/http2/core.js b/lib/internal/http2/core.js index 1b1d6ad9cfc2fd..261ddb26b96a25 100644 --- a/lib/internal/http2/core.js +++ b/lib/internal/http2/core.js @@ -2779,7 +2779,7 @@ function connect(authority, options, listener) { if (authority.hostname) { host = authority.hostname; - if (host.startsWith('[')) + if (host[0] === '[') host = host.slice(1, -1); } else if (authority.host) { host = authority.host; From 7d6584df5bdd2cb61edd0fdd32b397a785df4635 Mon Sep 17 00:00:00 2001 From: Luigi Pinca Date: Wed, 26 Jun 2019 14:55:01 +0200 Subject: [PATCH 3/3] fixup! fixup! http2: remove square brackets from parsed hostname --- lib/internal/http2/core.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/internal/http2/core.js b/lib/internal/http2/core.js index 261ddb26b96a25..871f876599359f 100644 --- a/lib/internal/http2/core.js +++ b/lib/internal/http2/core.js @@ -2774,7 +2774,7 @@ function connect(authority, options, listener) { const protocol = authority.protocol || options.protocol || 'https:'; const port = '' + (authority.port !== '' ? authority.port : (authority.protocol === 'http:' ? 80 : 443)); - let host = ''; + let host = 'localhost'; if (authority.hostname) { host = authority.hostname; @@ -2783,8 +2783,6 @@ function connect(authority, options, listener) { host = host.slice(1, -1); } else if (authority.host) { host = authority.host; - } else { - host = 'localhost'; } let socket;