Skip to content

Commit e5693d2

Browse files
committed
Merge pull request #110 from nodejitsu/gh-107
#107: Set x-forwarded-for header (amongst others)
2 parents 787370e + 1f33943 commit e5693d2

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

lib/node-http-proxy/http-proxy.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,10 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
128128
// * `x-forwarded-proto`: Protocol of the original request
129129
// * `x-forwarded-port`: Port of the original request.
130130
//
131-
if (this.enable.xforward && req.connection && req.connection.socket) {
132-
req.headers['x-forwarded-for'] = req.connection.remoteAddress || req.connection.socket.remoteAddress;
133-
req.headers['x-forwarded-port'] = req.connection.remotePort || req.connection.socket.remotePort;
131+
132+
if (this.enable.xforward && req.connection && req.socket) {
133+
req.headers['x-forwarded-for'] = req.connection.remoteAddress || req.socket.remoteAddress;
134+
req.headers['x-forwarded-port'] = req.connection.remotePort || req.socket.remotePort;
134135
req.headers['x-forwarded-proto'] = req.connection.pair ? 'https' : 'http';
135136
}
136137

@@ -763,4 +764,4 @@ HttpProxy.prototype._forwardRequest = function (req) {
763764
req.on('end', function () {
764765
forwardProxy.end();
765766
});
766-
};
767+
};

test/helpers.js

+35-1
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,40 @@ TestRunner.prototype.assertResponseCode = function (proxyPort, statusCode, creat
128128
return test;
129129
};
130130

131+
// A test helper to check and see if the http headers were set properly.
132+
TestRunner.prototype.assertHeaders = function (proxyPort, headerName, createProxy) {
133+
var assertion = "should receive http header \"" + headerName + "\"",
134+
protocol = this.source.protocols.http;
135+
136+
var test = {
137+
topic: function () {
138+
var that = this, options = {
139+
method: 'GET',
140+
uri: protocol + '://localhost:' + proxyPort,
141+
headers: {
142+
host: 'unknown.com'
143+
}
144+
};
145+
146+
if (createProxy) {
147+
return createProxy(function () {
148+
request(options, that.callback);
149+
});
150+
}
151+
152+
request(options, this.callback);
153+
}
154+
};
155+
156+
test[assertion] = function (err, res, body) {
157+
assert.isNull(err);
158+
assert.isNotNull(res.headers[headerName]);
159+
};
160+
161+
return test;
162+
};
163+
164+
131165
//
132166
// WebSocketTest
133167
//
@@ -368,4 +402,4 @@ function merge (target) {
368402
});
369403
});
370404
return target;
371-
}
405+
}

test/http/http-proxy-test.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,11 @@ vows.describe('node-http-proxy/http-proxy/' + testName).addBatch({
7474
"and a valid target server": runner.assertProxied('localhost', 8120, 8121, function (callback) {
7575
runner.startProxyServerWithForwarding(8120, 8121, 'localhost', forwardOptions, callback);
7676
}),
77-
"and without a valid forward server": runner.assertProxied('localhost', 8122, 8123, function (callback) {
78-
runner.startProxyServerWithForwarding(8122, 8123, 'localhost', badForwardOptions, callback);
77+
"and also a valid target server": runner.assertHeaders(8122, "x-forwarded-for", function (callback) {
78+
runner.startProxyServerWithForwarding(8122, 8123, 'localhost', forwardOptions, callback);
79+
}),
80+
"and without a valid forward server": runner.assertProxied('localhost', 8124, 8125, function (callback) {
81+
runner.startProxyServerWithForwarding(8124, 8125, 'localhost', badForwardOptions, callback);
7982
})
8083
}
8184
}

0 commit comments

Comments
 (0)