diff --git a/doc/api/http.md b/doc/api/http.md index 91309e79fdb..fc19be72d14 100644 --- a/doc/api/http.md +++ b/doc/api/http.md @@ -1858,6 +1858,8 @@ changes: details. Any [`Duplex`][] stream is a valid return value. * `timeout` {number}: A number specifying the socket timeout in milliseconds. This will set the timeout before the socket is connected. + * `setHost` {boolean}: Specifies whether or not to automatically add the + `Host` header. Defaults to `true`. * `callback` {Function} * Returns: {http.ClientRequest} diff --git a/lib/_http_client.js b/lib/_http_client.js index a7df01bbf4f..5be1632fe59 100644 --- a/lib/_http_client.js +++ b/lib/_http_client.js @@ -116,7 +116,7 @@ function ClientRequest(options, cb) { var host = options.host = validateHost(options.hostname, 'hostname') || validateHost(options.host, 'host') || 'localhost'; - var setHost = (options.setHost === undefined); + var setHost = (options.setHost === undefined || Boolean(options.setHost)); this.socketPath = options.socketPath; this.timeout = options.timeout; diff --git a/test/parallel/test-https-host-headers.js b/test/parallel/test-https-host-headers.js index a1330ad73a1..11fa8dd70f9 100644 --- a/test/parallel/test-https-host-headers.js +++ b/test/parallel/test-https-host-headers.js @@ -16,7 +16,7 @@ const httpsServer = https.createServer(options, reqHandler); function reqHandler(req, res) { console.log(`Got request: ${req.headers.host} ${req.url}`); - if (req.url === '/setHostFalse5') { + if (req.url.startsWith('/setHostFalse')) { assert.strictEqual(req.headers.host, undefined); } else { assert.strictEqual( @@ -97,6 +97,34 @@ function testHttps() { setHost: false, port: this.address().port, rejectUnauthorized: false + }, cb).on('error', thrower); + + https.request({ + method: 'GET', + path: `/${counter++}`, + host: 'localhost', + setHost: true, + //agent: false, + port: this.address().port, + rejectUnauthorized: false }, cb).on('error', thrower).end(); + + https.get({ + method: 'GET', + path: `/setHostFalse${counter++}`, + host: 'localhost', + setHost: 0, + port: this.address().port, + rejectUnauthorized: false + }, cb).on('error', thrower); + + https.get({ + method: 'GET', + path: `/setHostFalse${counter++}`, + host: 'localhost', + setHost: null, + port: this.address().port, + rejectUnauthorized: false + }, cb).on('error', thrower); }); }