Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
26 changes: 15 additions & 11 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,18 +375,22 @@ function ClientRequest(input, options, cb) {
if (host && !this.getHeader('host') && setHost) {
let hostHeader = host;

// For the Host header, ensure that IPv6 addresses are enclosed
// in square brackets, as defined by URI formatting
// https://tools.ietf.org/html/rfc3986#section-3.2.2
const posColon = hostHeader.indexOf(':');
if (posColon !== -1 &&
hostHeader.includes(':', posColon + 1) &&
hostHeader.charCodeAt(0) !== 91/* '[' */) {
hostHeader = `[${hostHeader}]`;
}
if (method === 'CONNECT' && options.path) {
hostHeader = String(this.path);
} else {
// For the Host header, ensure that IPv6 addresses are enclosed
// in square brackets, as defined by URI formatting
// https://tools.ietf.org/html/rfc3986#section-3.2.2
const posColon = hostHeader.indexOf(':');
if (posColon !== -1 &&
hostHeader.includes(':', posColon + 1) &&
hostHeader.charCodeAt(0) !== 91/* '[' */) {
hostHeader = `[${hostHeader}]`;
}

if (port && +port !== defaultPort) {
hostHeader += ':' + port;
if (port && +port !== defaultPort) {
hostHeader += ':' + port;
}
}
this.setHeader('Host', hostHeader);
}
Expand Down
37 changes: 37 additions & 0 deletions test/parallel/test-http-connect-default-host-header.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const http = require('http');
const net = require('net');

const target = 'target.example.com:443';

const server = net.createServer(common.mustCall((socket) => {
socket.once('data', common.mustCall((data) => {
const rawRequest = data.toString();
const requestLines = rawRequest.split('\r\n');

assert.strictEqual(requestLines[0], `CONNECT ${target} HTTP/1.1`);
assert(requestLines.includes(`Host: ${target}`));

socket.end('HTTP/1.1 200 Connection established\r\n\r\n');
}));
}));

server.listen(0, common.localhostIPv4, common.mustCall(() => {
const req = http.request({
host: common.localhostIPv4,
port: server.address().port,
method: 'CONNECT',
path: target,
}, common.mustNotCall());

req.on('connect', common.mustCall((res, socket) => {
assert.strictEqual(res.statusCode, 200);
socket.destroy();
server.close();
}));

req.end();
}));
Loading