Skip to content

Commit

Permalink
determine x-forwarded-port from host header
Browse files Browse the repository at this point in the history
`req.remotePort' returns the ephemeral port, which is not useful.
node v0.10.0 added `req.localPort' which returns what we want, but
we want to maintain backwards compatibility. Fixes #341 & #227
  • Loading branch information
blahed authored and indexzero committed Dec 27, 2013
1 parent 7e8041d commit d4e91eb
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions lib/node-http-proxy/http-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,11 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
}

if (req.headers['x-forwarded-port']) {
var portToAppend = "," + req.connection.remotePort || req.socket.remotePort;
var portToAppend = "," + getPortFromHostHeader(req);
req.headers['x-forwarded-port'] += portToAppend;
}
else {
req.headers['x-forwarded-port'] = req.connection.remotePort || req.socket.remotePort;
req.headers['x-forwarded-port'] = getPortFromHostHeader(req);
}

if (req.headers['x-forwarded-proto']) {
Expand Down Expand Up @@ -489,11 +489,11 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, upgradeHead,
}

if (req.headers['x-forwarded-port']) {
var portToAppend = "," + req.connection.remotePort || socket.remotePort;
var portToAppend = "," + getPortFromHostHeader(req);
req.headers['x-forwarded-port'] += portToAppend;
}
else {
req.headers['x-forwarded-port'] = req.connection.remotePort || socket.remotePort;
req.headers['x-forwarded-port'] = getPortFromHostHeader(req);
}

if (req.headers['x-forwarded-proto']) {
Expand Down Expand Up @@ -956,6 +956,17 @@ HttpProxy.prototype._forwardRequest = function (req) {
});
};

function getPortFromHostHeader(req) {
var portMatch = req.headers.host.match(/:(\d+)$/);

if(portMatch) {
return parseInt(portMatch[1]);
}
else {
return getProto(req) === 'https' ? 443 : 80;
}
}

function getProto(req) {
return req.isSpdy ? 'https' : (req.connection.pair ? 'https' : 'http');
}

0 comments on commit d4e91eb

Please sign in to comment.