From 501e8c2a9b61e5edc19251ece01b6f643dc1b19e Mon Sep 17 00:00:00 2001 From: Jarrett Cruger Date: Mon, 8 Dec 2014 16:14:16 -0500 Subject: [PATCH] [fix] properly include port in host header with changeOrigin in all cases fixes #750 --- lib/http-proxy/common.js | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/lib/http-proxy/common.js b/lib/http-proxy/common.js index 24ed323..15e0392 100644 --- a/lib/http-proxy/common.js +++ b/lib/http-proxy/common.js @@ -1,6 +1,7 @@ -var common = exports, - url = require('url'), - extend = require('util')._extend; +var common = exports, + url = require('url'), + extend = require('util')._extend, + required = require('requires-port'); var upgradeHeader = /(^|,)\s*upgrade\s*($|,)/i; /** @@ -74,9 +75,11 @@ common.setupOutgoing = function(outgoing, options, req, forward) { outgoing.path = common.urlJoin(targetPath, outgoingPath); if (options.changeOrigin) { - outgoing.headers.host = outgoing.host; + outgoing.headers.host = + required(outgoing.port, options[forward || 'target'].protocol) && !hasPort(outgoing.host) + ? outgoing.host + ':' + outgoing.port + : outgoing.host; } - return outgoing; }; @@ -161,3 +164,14 @@ common.urlJoin = function() { return retSegs.join('?') }; + +/** + * Check the host and see if it potentially has a port in it (keep it simple) + * + * @returns {Boolean} Whether we have one or not + * + * @api private + */ +function hasPort(host) { + return !!~host.indexOf(':'); +};