Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http2: remove square brackets from parsed hostname #28406

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion lib/internal/http2/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like host can be default to localhost and remove the last else ?


if (authority.hostname) {
host = authority.hostname;

if (host.startsWith('['))
gengjiawen marked this conversation as resolved.
Show resolved Hide resolved
host = host.slice(1, -1);
} else if (authority.host) {
host = authority.host;
} else {
host = 'localhost';
}

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();
}
}));
}
}));
}