From 2677bb6c44244ea0b584db744955bedf7aee2c62 Mon Sep 17 00:00:00 2001
From: Joshua Holbrook <josh.holbrook@gmail.com>
Date: Tue, 20 Sep 2011 13:00:59 -0700
Subject: [PATCH 1/3] [fix] x-forwarded http headers should set properly.

---
 lib/node-http-proxy/http-proxy.js | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/lib/node-http-proxy/http-proxy.js b/lib/node-http-proxy/http-proxy.js
index 9b4ef59..84ba1ac 100644
--- a/lib/node-http-proxy/http-proxy.js
+++ b/lib/node-http-proxy/http-proxy.js
@@ -128,9 +128,10 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
   // * `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 (this.enable.xforward && req.connection && req.socket) {
     req.headers['x-forwarded-for']   = req.connection.remoteAddress || req.connection.socket.remoteAddress;
-    req.headers['x-forwarded-port']  = req.connection.remotePort || req.connection.socket.remotePort;
+    req.headers['x-forwarded-port']  = req.connection.remotePort || req.socket.remotePort;
     req.headers['x-forwarded-proto'] = req.connection.pair ? 'https' : 'http';
   }
 
@@ -763,4 +764,4 @@ HttpProxy.prototype._forwardRequest = function (req) {
   req.on('end', function () {
     forwardProxy.end();
   });
-};
\ No newline at end of file
+};
-- 
GitLab


From 66e982060c6c41ad7dfadce1403c8e13d267781a Mon Sep 17 00:00:00 2001
From: Joshua Holbrook <josh.holbrook@gmail.com>
Date: Tue, 20 Sep 2011 13:33:07 -0700
Subject: [PATCH 2/3] [test] Added a test for the "x-forwarded-for" header

---
 test/helpers.js              | 36 +++++++++++++++++++++++++++++++++++-
 test/http/http-proxy-test.js |  7 +++++--
 2 files changed, 40 insertions(+), 3 deletions(-)

diff --git a/test/helpers.js b/test/helpers.js
index b1bbd22..56ba4aa 100644
--- a/test/helpers.js
+++ b/test/helpers.js
@@ -128,6 +128,40 @@ TestRunner.prototype.assertResponseCode = function (proxyPort, statusCode, creat
   return test;
 };
 
+// A test helper to check and see if the http headers were set properly.
+TestRunner.prototype.assertHeaders = function (proxyPort, headerName, createProxy) {
+  var assertion = "should receive http header \"" + headerName + "\"",
+      protocol = this.source.protocols.http;
+
+  var test = {
+    topic: function () {
+      var that = this, options = {
+        method: 'GET',
+        uri: protocol + '://localhost:' + proxyPort,
+        headers: {
+          host: 'unknown.com'
+        }
+      };
+
+      if (createProxy) {
+        return createProxy(function () {
+          request(options, that.callback);
+        });
+      }
+
+      request(options, this.callback);
+    }
+  };
+
+  test[assertion] = function (err, res, body) {
+    assert.isNull(err);
+    assert.isNotNull(res.headers[headerName]);
+  };
+
+  return test;
+};
+
+
 //
 // WebSocketTest
 //
@@ -368,4 +402,4 @@ function merge (target) {
     });
   });
   return target;
-}
\ No newline at end of file
+}
diff --git a/test/http/http-proxy-test.js b/test/http/http-proxy-test.js
index c202f97..e3b08f9 100644
--- a/test/http/http-proxy-test.js
+++ b/test/http/http-proxy-test.js
@@ -74,8 +74,11 @@ vows.describe('node-http-proxy/http-proxy/' + testName).addBatch({
         "and a valid target server": runner.assertProxied('localhost', 8120, 8121, function (callback) {
           runner.startProxyServerWithForwarding(8120, 8121, 'localhost', forwardOptions, callback);
         }),
-        "and without a valid forward server": runner.assertProxied('localhost', 8122, 8123, function (callback) {
-          runner.startProxyServerWithForwarding(8122, 8123, 'localhost', badForwardOptions, callback);
+        "and also a valid target server": runner.assertHeaders(8122, "x-forwarded-for", function (callback) {
+          runner.startProxyServerWithForwarding(8122, 8123, 'localhost', forwardOptions, callback);
+        }),
+        "and without a valid forward server": runner.assertProxied('localhost', 8124, 8125, function (callback) {
+          runner.startProxyServerWithForwarding(8124, 8125, 'localhost', badForwardOptions, callback);
         })
       }
     }
-- 
GitLab


From 1f33943b231cdf2cb619977801c7b0d4e98ab6df Mon Sep 17 00:00:00 2001
From: Joshua Holbrook <josh.holbrook@gmail.com>
Date: Tue, 20 Sep 2011 14:34:03 -0700
Subject: [PATCH 3/3] [fix] connection.socket -> socket for source of
 x-forwarded-for data

---
 lib/node-http-proxy/http-proxy.js | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/node-http-proxy/http-proxy.js b/lib/node-http-proxy/http-proxy.js
index 84ba1ac..faf133f 100644
--- a/lib/node-http-proxy/http-proxy.js
+++ b/lib/node-http-proxy/http-proxy.js
@@ -130,7 +130,7 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
   //
 
   if (this.enable.xforward && req.connection && req.socket) {
-    req.headers['x-forwarded-for']   = req.connection.remoteAddress || req.connection.socket.remoteAddress;
+    req.headers['x-forwarded-for']   = req.connection.remoteAddress || req.socket.remoteAddress;
     req.headers['x-forwarded-port']  = req.connection.remotePort || req.socket.remotePort;
     req.headers['x-forwarded-proto'] = req.connection.pair ? 'https' : 'http';
   }
-- 
GitLab