Skip to content

Commit 2d42709

Browse files
committed
[fix] Optimize fix for x-forwarded-for-port.
1 parent d4e91eb commit 2d42709

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

Diff for: lib/node-http-proxy/http-proxy.js

+12-7
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ var events = require('events'),
3030
url = require('url'),
3131
httpProxy = require('../node-http-proxy');
3232

33+
//
34+
// @private {RegExp} extractPort
35+
// Reusable regular expression for getting the
36+
// port from a host string.
37+
//
38+
var extractPort = /:(\d+)$/;
39+
3340
//
3441
// ### function HttpProxy (options)
3542
// #### @options {Object} Options for this instance.
@@ -957,14 +964,12 @@ HttpProxy.prototype._forwardRequest = function (req) {
957964
};
958965

959966
function getPortFromHostHeader(req) {
960-
var portMatch = req.headers.host.match(/:(\d+)$/);
961-
962-
if(portMatch) {
963-
return parseInt(portMatch[1]);
964-
}
965-
else {
966-
return getProto(req) === 'https' ? 443 : 80;
967+
var match;
968+
if ((match = extractPort.exec(req.headers.host))) {
969+
return parseInt(match[1]);
967970
}
971+
972+
return getProto(req) === 'https' ? 443 : 80;
968973
}
969974

970975
function getProto(req) {

0 commit comments

Comments
 (0)