From d7a4c70fe3642e87e1692225181580a79576b937 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Mon, 10 Jul 2017 17:36:55 -0700 Subject: [PATCH] only include the port number in the Host header when non-default port Fixes #22. --- index.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 3448b71d..69985780 100644 --- a/index.js +++ b/index.js @@ -206,7 +206,15 @@ HttpsProxyAgent.prototype.callback = function connect(req, opts, fn) { headers['Proxy-Authorization'] = 'Basic ' + new Buffer(proxy.auth).toString('base64'); } - headers['Host'] = hostname; + + // the Host header should only include the port + // number when it is a non-standard port + var host = opts.host; + if (!isDefaultPort(opts.port, opts.secureEndpoint)) { + host += ':' + opts.port; + } + headers['Host'] = host; + headers['Connection'] = 'close'; Object.keys(headers).forEach(function(name) { msg += name + ': ' + headers[name] + '\r\n'; @@ -214,3 +222,7 @@ HttpsProxyAgent.prototype.callback = function connect(req, opts, fn) { socket.write(msg + '\r\n'); }; + +function isDefaultPort(port, secure) { + return Boolean((!secure && port === 80) || (secure && port === 443)); +}