From d637b964206c4267e5278c9e9ff5efa040819e0a Mon Sep 17 00:00:00 2001
From: Jay Harris <jay@aranasoft.com>
Date: Fri, 9 May 2014 20:48:30 -0400
Subject: [PATCH 1/2] Don't override connection header if Upgrading

---
 lib/http-proxy/common.js | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/lib/http-proxy/common.js b/lib/http-proxy/common.js
index d682047..d4186f0 100644
--- a/lib/http-proxy/common.js
+++ b/lib/http-proxy/common.js
@@ -47,12 +47,14 @@ common.setupOutgoing = function(outgoing, options, req, forward) {
   outgoing.localAddress = options.localAddress;
 
   //
-  // Remark: If we are false set the connection: close. This is the right thing to do
+  // Remark: If we are false and not upgrading, set the connection: close. This is the right thing to do
   // as node core doesn't handle this COMPLETELY properly yet.
   //
   if(!outgoing.agent) {
     outgoing.headers = outgoing.headers || {};
-    outgoing.headers.connection = 'close';
+    if(typeof outgoing.headers.connection !== 'string' || outgoing.headers.connection.toLowerCase() !== 'upgrade') {
+      outgoing.headers.connection = 'close';
+    }
   }
 
   //
-- 
GitLab


From 8aa7c519b15f734af7db34d2102781adbeae10aa Mon Sep 17 00:00:00 2001
From: Jay Harris <jay@aranasoft.com>
Date: Fri, 9 May 2014 22:49:23 -0400
Subject: [PATCH 2/2] Adding test cases on preventing upgrade override

---
 test/lib-http-proxy-common-test.js | 42 ++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)

diff --git a/test/lib-http-proxy-common-test.js b/test/lib-http-proxy-common-test.js
index 8cb9391..dab2d26 100644
--- a/test/lib-http-proxy-common-test.js
+++ b/test/lib-http-proxy-common-test.js
@@ -38,6 +38,48 @@ describe('lib/http-proxy/common.js', function () {
       expect(outgoing.localAddress).to.eql('local.address');
     });
 
+    it('should not override agentless upgrade header', function () {
+      var outgoing = {};
+      common.setupOutgoing(outgoing,
+        {
+          agent: undefined,
+          target: {
+            host      : 'hey',
+            hostname  : 'how',
+            socketPath: 'are',
+            port      : 'you',
+          },
+          headers: {'connection': 'upgrade'},
+        },
+        {
+          method    : 'i',
+          url      : 'am',
+          headers   : {'pro':'xy','overwritten':false}
+        });
+      expect(outgoing.headers.connection).to.eql('upgrade');
+    });
+
+    it('should override agentless non-upgrade header to close', function () {
+      var outgoing = {};
+      common.setupOutgoing(outgoing,
+        {
+          agent: undefined,
+          target: {
+            host      : 'hey',
+            hostname  : 'how',
+            socketPath: 'are',
+            port      : 'you',
+          },
+          headers: {'connection': 'xyz'},
+        },
+        {
+          method    : 'i',
+          url      : 'am',
+          headers   : {'pro':'xy','overwritten':false}
+        });
+      expect(outgoing.headers.connection).to.eql('close');
+    });
+
     it('should set the agent to false if none is given', function () {
       var outgoing = {};
       common.setupOutgoing(outgoing, {target:
-- 
GitLab