Merge pull request #163 from samyakbhuta/master

Allows node-http-proxy to append new values to existing headers for incoming "x-forward-for","x-forward-proto" and "x-forward-port"
This commit is contained in:
Bradley Meck 2011-12-08 14:07:21 -08:00
commit e2dccbca48

View File

@ -122,19 +122,38 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
//
// Add common proxy headers to the request so that they can
// be availible to the proxy target server:
// 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 = "," + req.connection.pair ? 'https' : 'http';
req.headers['x-forwarded-proto'] += protoToAppend;
} else {
req.headers['x-forwarded-proto'] = req.connection.pair ? 'https' : 'http';
}
}
//
// Emit the `start` event indicating that we have begun the proxy operation.
//
@ -299,6 +318,7 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
// `req` write it to the `reverseProxy` request.
//
req.on('data', function (chunk) {
if (!errState) {
var flushed = reverseProxy.write(chunk);
if (!flushed) {
@ -375,18 +395,38 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
//
// Add common proxy headers to the request so that they can
// be availible to the proxy target server:
// 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.connection.socket) {
if (req.headers['x-forwarded-for']){
var addressToAppend = "," + req.connection.remoteAddress || req.connection.socket.remoteAddress;
req.headers['x-forwarded-for'] += addressToAppend;
} else {
req.headers['x-forwarded-for'] = req.connection.remoteAddress || req.connection.socket.remoteAddress;
}
if (req.headers['x-forwarded-port']){
var portToAppend = "," + req.connection.remotePort || req.connection.socket.remotePort;
req.headers['x-forwarded-port'] += portToAppend;
} else {
req.headers['x-forwarded-port'] = req.connection.remotePort || req.connection.socket.remotePort;
}
if (req.headers['x-forwarded-proto']){
var protoToAppend = "," + req.connection.pair ? 'https' : 'http';
req.headers['x-forwarded-proto'] += protoToAppend;
} else {
req.headers['x-forwarded-proto'] = req.connection.pair ? 'https' : 'http';
}
}
//
// Helper function for setting appropriate socket values:
// 1. Turn of all bufferings