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(':'); +};