Skip to content

Commit 501e8c2

Browse files
committed
[fix] properly include port in host header with changeOrigin in all cases fixes #750
1 parent 81874f7 commit 501e8c2

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

lib/http-proxy/common.js

+19-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
var common = exports,
2-
url = require('url'),
3-
extend = require('util')._extend;
1+
var common = exports,
2+
url = require('url'),
3+
extend = require('util')._extend,
4+
required = require('requires-port');
45

56
var upgradeHeader = /(^|,)\s*upgrade\s*($|,)/i;
67
/**
@@ -74,9 +75,11 @@ common.setupOutgoing = function(outgoing, options, req, forward) {
7475
outgoing.path = common.urlJoin(targetPath, outgoingPath);
7576

7677
if (options.changeOrigin) {
77-
outgoing.headers.host = outgoing.host;
78+
outgoing.headers.host =
79+
required(outgoing.port, options[forward || 'target'].protocol) && !hasPort(outgoing.host)
80+
? outgoing.host + ':' + outgoing.port
81+
: outgoing.host;
7882
}
79-
8083
return outgoing;
8184
};
8285

@@ -161,3 +164,14 @@ common.urlJoin = function() {
161164

162165
return retSegs.join('?')
163166
};
167+
168+
/**
169+
* Check the host and see if it potentially has a port in it (keep it simple)
170+
*
171+
* @returns {Boolean} Whether we have one or not
172+
*
173+
* @api private
174+
*/
175+
function hasPort(host) {
176+
return !!~host.indexOf(':');
177+
};

0 commit comments

Comments
 (0)