From 867fee86cf65fbd31c3220643f902a9b8a9c2e14 Mon Sep 17 00:00:00 2001 From: XadillaX Date: Wed, 21 Mar 2018 12:11:40 +0800 Subject: [PATCH 1/5] http: fix `request` when `setHost` is `true` Fixes: https://github.com/nodejs/node/issues/19457 --- lib/_http_client.js | 2 +- test/parallel/test-https-host-headers.js | 30 +++++++++++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/lib/_http_client.js b/lib/_http_client.js index 8e3d61a75612ca..ab8b81d750c108 100644 --- a/lib/_http_client.js +++ b/lib/_http_client.js @@ -115,7 +115,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 || 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..085ee7de87df9e 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( @@ -104,5 +104,33 @@ function testHttps() { port: this.address().port, rejectUnauthorized: false }, cb).on('error', thrower).end(); + + 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).end(); + + https.get({ + method: 'GET', + path: `/setHostFalse${counter++}`, + host: 'localhost', + setHost: null, + port: this.address().port, + rejectUnauthorized: false + }, cb).on('error', thrower).end(); }); } From a308e56f88e3790516844224d004e33b89042208 Mon Sep 17 00:00:00 2001 From: XadillaX Date: Wed, 21 Mar 2018 22:27:25 +0800 Subject: [PATCH 2/5] f --- lib/_http_client.js | 2 +- test/parallel/test-https-host-headers.js | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/_http_client.js b/lib/_http_client.js index ab8b81d750c108..a24d27a3912424 100644 --- a/lib/_http_client.js +++ b/lib/_http_client.js @@ -115,7 +115,7 @@ function ClientRequest(options, cb) { var host = options.host = validateHost(options.hostname, 'hostname') || validateHost(options.host, 'host') || 'localhost'; - var setHost = (options.setHost === undefined || options.setHost); + 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 085ee7de87df9e..99b7baaaf6d345 100644 --- a/test/parallel/test-https-host-headers.js +++ b/test/parallel/test-https-host-headers.js @@ -103,7 +103,7 @@ function testHttps() { setHost: false, port: this.address().port, rejectUnauthorized: false - }, cb).on('error', thrower).end(); + }, cb).on('error', thrower); https.request({ method: 'GET', @@ -122,7 +122,7 @@ function testHttps() { setHost: 0, port: this.address().port, rejectUnauthorized: false - }, cb).on('error', thrower).end(); + }, cb).on('error', thrower); https.get({ method: 'GET', @@ -131,6 +131,6 @@ function testHttps() { setHost: null, port: this.address().port, rejectUnauthorized: false - }, cb).on('error', thrower).end(); + }, cb).on('error', thrower); }); } From 02c474fdbb6ae78fa8ce1b39da1e0b4e47090ca5 Mon Sep 17 00:00:00 2001 From: XadillaX Date: Sat, 24 Mar 2018 22:18:16 +0800 Subject: [PATCH 3/5] doc --- doc/api/http.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/api/http.md b/doc/api/http.md index 91309e79fdbace..f9ad01d067c22b 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}: When values `true`, force set request header `host` to + `options.host` if no this header. Default to `true`. * `callback` {Function} * Returns: {http.ClientRequest} From 63dff0041f0d6de1718ad3db15c85e0e11101fcb Mon Sep 17 00:00:00 2001 From: XadillaX Date: Sat, 24 Mar 2018 22:27:16 +0800 Subject: [PATCH 4/5] update --- doc/api/http.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/doc/api/http.md b/doc/api/http.md index f9ad01d067c22b..99d505f769b55e 100644 --- a/doc/api/http.md +++ b/doc/api/http.md @@ -1858,8 +1858,7 @@ 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}: When values `true`, force set request header `host` to - `options.host` if no this header. Default to `true`. + * `setHost` {boolean}: Specifies whether or not to automatically add the `Host` header. Defaults to `true`. * `callback` {Function} * Returns: {http.ClientRequest} From e9035aebc981fa5bc800aaf5a1637823cc9e53d0 Mon Sep 17 00:00:00 2001 From: XadillaX Date: Sat, 24 Mar 2018 22:31:02 +0800 Subject: [PATCH 5/5] update --- doc/api/http.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/api/http.md b/doc/api/http.md index 99d505f769b55e..fc19be72d1405d 100644 --- a/doc/api/http.md +++ b/doc/api/http.md @@ -1858,7 +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`. + * `setHost` {boolean}: Specifies whether or not to automatically add the + `Host` header. Defaults to `true`. * `callback` {Function} * Returns: {http.ClientRequest}