From 2d42709c3283637de16a49e815b03e63432bbd29 Mon Sep 17 00:00:00 2001 From: indexzero Date: Thu, 26 Dec 2013 23:59:48 -0800 Subject: [PATCH] [fix] Optimize fix for `x-forwarded-for-port`. --- lib/node-http-proxy/http-proxy.js | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/node-http-proxy/http-proxy.js b/lib/node-http-proxy/http-proxy.js index f453db8..43a6489 100644 --- a/lib/node-http-proxy/http-proxy.js +++ b/lib/node-http-proxy/http-proxy.js @@ -30,6 +30,13 @@ var events = require('events'), url = require('url'), httpProxy = require('../node-http-proxy'); +// +// @private {RegExp} extractPort +// Reusable regular expression for getting the +// port from a host string. +// +var extractPort = /:(\d+)$/; + // // ### function HttpProxy (options) // #### @options {Object} Options for this instance. @@ -957,14 +964,12 @@ HttpProxy.prototype._forwardRequest = function (req) { }; function getPortFromHostHeader(req) { - var portMatch = req.headers.host.match(/:(\d+)$/); + var match; + if ((match = extractPort.exec(req.headers.host))) { + return parseInt(match[1]); + } - if(portMatch) { - return parseInt(portMatch[1]); - } - else { - return getProto(req) === 'https' ? 443 : 80; - } + return getProto(req) === 'https' ? 443 : 80; } function getProto(req) {