From 41cc05f4d5f38865612578aaa4dcb93448d1709f Mon Sep 17 00:00:00 2001 From: GP Date: Wed, 4 Apr 2018 11:36:44 +0530 Subject: [PATCH 1/4] Strip port suffix from Host header if the protocol is known. This partially revert ff6d6c6e7a3b2c36618b5d1db662e10c929696e3, and still works for IPv6 addresses as well. --- request.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/request.js b/request.js index ff19ca39c..59543f481 100644 --- a/request.js +++ b/request.js @@ -288,10 +288,14 @@ Request.prototype.init = function (options) { self.setHost = false if (!self.hasHeader('host')) { var hostHeaderName = self.originalHostHeaderName || 'host' - // When used with an IPv6 address, `host` will provide - // the correct bracketed format, unlike using `hostname` and - // optionally adding the `port` when necessary. self.setHeader(hostHeaderName, self.uri.host) + // Drop :port suffix from Host header if known protocol. + if (self.uri.port) { + if ((self.uri.port === 80 && self.uri.protocol === 'http:') || + (self.uri.port === 443 && self.uri.protocol === 'https:')) { + self.setHeader(hostHeaderName, self.uri.hostname) + } + } self.setHost = true } From 1e799775f1c657bf44c4bf86a79a7e0e81362071 Mon Sep 17 00:00:00 2001 From: GP Date: Tue, 10 Apr 2018 11:27:03 +0530 Subject: [PATCH 2/4] Port is a string out of url.parse(). See https://nodejs.org/api/url.html#url_url_port --- request.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/request.js b/request.js index 59543f481..b3ea1f14f 100644 --- a/request.js +++ b/request.js @@ -291,8 +291,8 @@ Request.prototype.init = function (options) { self.setHeader(hostHeaderName, self.uri.host) // Drop :port suffix from Host header if known protocol. if (self.uri.port) { - if ((self.uri.port === 80 && self.uri.protocol === 'http:') || - (self.uri.port === 443 && self.uri.protocol === 'https:')) { + if ((self.uri.port == 80 && self.uri.protocol === 'http:') || + (self.uri.port == 443 && self.uri.protocol === 'https:')) { self.setHeader(hostHeaderName, self.uri.hostname) } } From ab2d63de6d19296d50029a3f8603dbb09adb8d3d Mon Sep 17 00:00:00 2001 From: GP Date: Tue, 10 Apr 2018 12:17:47 +0530 Subject: [PATCH 3/4] Port is a string out of url.parse(). See https://nodejs.org/api/url.html#url_url_port --- request.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/request.js b/request.js index b3ea1f14f..e900339d9 100644 --- a/request.js +++ b/request.js @@ -291,8 +291,8 @@ Request.prototype.init = function (options) { self.setHeader(hostHeaderName, self.uri.host) // Drop :port suffix from Host header if known protocol. if (self.uri.port) { - if ((self.uri.port == 80 && self.uri.protocol === 'http:') || - (self.uri.port == 443 && self.uri.protocol === 'https:')) { + if ((self.uri.port === '80' && self.uri.protocol === 'http:') || + (self.uri.port === '443' && self.uri.protocol === 'https:')) { self.setHeader(hostHeaderName, self.uri.hostname) } } From 237231e07fc165e6d7fba17aebd228314b402109 Mon Sep 17 00:00:00 2001 From: GP Date: Tue, 10 Apr 2018 12:18:10 +0530 Subject: [PATCH 4/4] Add tests for the new Host header changes. --- tests/test-headers.js | 50 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/tests/test-headers.js b/tests/test-headers.js index c1c29622a..bed2875cf 100644 --- a/tests/test-headers.js +++ b/tests/test-headers.js @@ -171,6 +171,56 @@ tape('undefined headers', function (t) { }) }) +tape('preserve port in host header if non-standard port', function (t) { + var r = request({ + url: s.url + '/headers.json' + }, function (err, res, body) { + t.equal(err, null) + t.equal(r.originalHost, 'localhost:' + s.port) + t.end() + }) +}) + +tape('strip port in host header if explicit standard port (:80) & protocol (HTTP)', function (t) { + var r = request({ + url: 'http://localhost:80/headers.json' + }, function (err, res, body) { + t.notEqual(err, null) + t.equal(r.req.socket._host, 'localhost') + t.end() + }) +}) + +tape('strip port in host header if explicit standard port (:443) & protocol (HTTPS)', function (t) { + var r = request({ + url: 'https://localhost:443/headers.json' + }, function (err, res, body) { + t.notEqual(err, null) + t.equal(r.req.socket._host, 'localhost') + t.end() + }) +}) + +tape('strip port in host header if implicit standard port & protocol (HTTP)', function (t) { + var r = request({ + url: 'http://localhost/headers.json' + }, function (err, res, body) { + t.notEqual(err, null) + t.equal(r.req.socket._host, 'localhost') + t.end() + }) +}) + +tape('strip port in host header if implicit standard port & protocol (HTTPS)', function (t) { + var r = request({ + url: 'https://localhost/headers.json' + }, function (err, res, body) { + t.notEqual(err, null) + t.equal(r.req.socket._host, 'localhost') + t.end() + }) +}) + tape('catch invalid characters error - GET', function (t) { request({ url: s.url + '/headers.json',