Skip to content

Commit

Permalink
Routing Proxy was not sending x-forward-*. Fixing It...
Browse files Browse the repository at this point in the history
  • Loading branch information
otaviojr authored and indexzero committed Mar 9, 2013
1 parent 025adc2 commit 916d44e
Showing 1 changed file with 73 additions and 1 deletion.
74 changes: 73 additions & 1 deletion lib/node-http-proxy/routing-proxy.js
Expand Up @@ -163,6 +163,10 @@ RoutingProxy.prototype.close = function () {
});
};

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

//
// ### function proxyRequest (req, res, [port, host, paused])
// #### @req {ServerRequest} Incoming HTTP Request to proxy.
Expand All @@ -176,7 +180,41 @@ RoutingProxy.prototype.close = function () {
//
RoutingProxy.prototype.proxyRequest = function (req, res, options) {
options = options || {};


//
// Add common proxy headers to the request so that they can
// be availible to the proxy target server. If the proxy is
// part of proxy chain it will append the address:
//
// * `x-forwarded-for`: IP Address of the original request
// * `x-forwarded-proto`: Protocol of the original request
// * `x-forwarded-port`: Port of the original request.
//
if (this.enable.xforward && req.connection && req.socket) {
if (req.headers['x-forwarded-for']){
var addressToAppend = "," + req.connection.remoteAddress || req.socket.remoteAddress;
req.headers['x-forwarded-for'] += addressToAppend;
}
else {
req.headers['x-forwarded-for'] = req.connection.remoteAddress || req.socket.remoteAddress;
}

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

if (req.headers['x-forwarded-proto']){
var protoToAppend = "," + getProto(req);
req.headers['x-forwarded-proto'] += protoToAppend;
}
else {
req.headers['x-forwarded-proto'] = getProto(req);
}
}
var location;

//
Expand Down Expand Up @@ -248,6 +286,40 @@ RoutingProxy.prototype.proxyRequest = function (req, res, options) {
RoutingProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options) {
options = options || {};

//
// Add common proxy headers to the request so that they can
// be availible to the proxy target server. If the proxy is
// part of proxy chain it will append the address:
//
// * `x-forwarded-for`: IP Address of the original request
// * `x-forwarded-proto`: Protocol of the original request
// * `x-forwarded-port`: Port of the original request.
//
if (this.enable.xforward && req.connection && req.socket) {
if (req.headers['x-forwarded-for']){
var addressToAppend = "," + req.connection.remoteAddress || req.socket.remoteAddress;
req.headers['x-forwarded-for'] += addressToAppend;
}
else {
req.headers['x-forwarded-for'] = req.connection.remoteAddress || req.socket.remoteAddress;
}

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

if (req.headers['x-forwarded-proto']){
var protoToAppend = "," + getProto(req);
req.headers['x-forwarded-proto'] += protoToAppend;
}
else {
req.headers['x-forwarded-proto'] = getProto(req);
}
}
var location,
proxy,
key;
Expand Down

0 comments on commit 916d44e

Please sign in to comment.