From e1f44a6366c40a5fdd4ad0a67805246e79553237 Mon Sep 17 00:00:00 2001 From: XadillaX Date: Wed, 21 Mar 2018 12:11:40 +0800 Subject: [PATCH] http: fix `request` when `setHost` is `true` Fixes: https://github.com/nodejs/node/issues/19457 PR-URL: https://github.com/nodejs/node/pull/19502 Reviewed-By: Luigi Pinca Reviewed-By: Benjamin Gruenbaum Reviewed-By: James M Snell Reviewed-By: Yuta Hiroto Reviewed-By: Trivikram Kamat --- doc/api/http.md | 2 ++ lib/_http_client.js | 2 +- test/parallel/test-https-host-headers.js | 30 +++++++++++++++++++++++- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/doc/api/http.md b/doc/api/http.md index 297f68e462e435..74ad1b2ba0ecd0 100644 --- a/doc/api/http.md +++ b/doc/api/http.md @@ -1809,6 +1809,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 7ba4d27e85b1b5..59f7c93acb72c7 100644 --- a/lib/_http_client.js +++ b/lib/_http_client.js @@ -139,7 +139,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 eb0e8b896a85ca..99b7baaaf6d345 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( @@ -103,6 +103,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); }); }