From 626cba7685c2ff5e3a7ca42ebe66821f04c53642 Mon Sep 17 00:00:00 2001 From: "Martinez, Andrew" Date: Fri, 18 Sep 2015 14:13:43 -0400 Subject: [PATCH 1/2] resolves #882 Adds a proxyWsReq event that can be used to modify the outgoing websocket handshake. --- lib/http-proxy/passes/ws-incoming.js | 1 + .../lib-http-proxy-passes-ws-incoming-test.js | 62 ++++++++++++++++++- 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/lib/http-proxy/passes/ws-incoming.js b/lib/http-proxy/passes/ws-incoming.js index f51785749..1fea38da8 100644 --- a/lib/http-proxy/passes/ws-incoming.js +++ b/lib/http-proxy/passes/ws-incoming.js @@ -140,6 +140,7 @@ var passes = exports; server.emit('proxySocket', proxySocket); //DEPRECATED. }); + server.emit('proxyWsReq', proxyReq, req, socket); return proxyReq.end(); // XXX: CHECK IF THIS IS THIS CORRECT function onOutgoingError(err) { diff --git a/test/lib-http-proxy-passes-ws-incoming-test.js b/test/lib-http-proxy-passes-ws-incoming-test.js index bfed888c0..a32046a30 100644 --- a/test/lib-http-proxy-passes-ws-incoming-test.js +++ b/test/lib-http-proxy-passes-ws-incoming-test.js @@ -1,5 +1,7 @@ var httpProxy = require('../lib/http-proxy/passes/ws-incoming'), - expect = require('expect.js'); + expect = require('expect.js'), + http = require('http'), + httpProxyLib = require('../lib/http-proxy'); describe('lib/http-proxy/passes/ws-incoming.js', function () { describe('#checkMethodAndHeader', function () { @@ -9,7 +11,7 @@ describe('lib/http-proxy/passes/ws-incoming.js', function () { method: 'DELETE', headers: {} }, - stubSocket = { + stubSocket = { destroy: function () { // Simulate Socket.destroy() method when call destroyCalled = true; @@ -74,6 +76,62 @@ describe('lib/http-proxy/passes/ws-incoming.js', function () { expect(returnValue).to.be(undefined); expect(destroyCalled).to.be(false); }) + + + it('should detect a proxyWsReq event and modify headers', function (done) { + var proxy = httpProxyLib.createProxyServer({ + target: 'http://127.0.0.1:8080', + ws: true + }); + + proxy.on('proxyWsReq', function(proxyWsReq, req, res, options) { + proxyWsReq.setHeader('X-Special-Proxy-Header', 'foobar'); + }); + + function requestHandler(req, res) { + proxy.web(req, res); + } + + var proxyServer = http.createServer(requestHandler); + + proxyServer.on('upgrade', function(req, res, head){ + proxy.ws(req, res, head); + }); + + var source = http.createServer(function(req, res) { + res.end(); + }); + + source.on('upgrade', function(req, res, head){ + expect(req.headers['x-special-proxy-header']).to.eql('foobar'); + source.close(); + proxyServer.close(); + done(); + }); + + source.on('error', function(){}); + proxy.on('error', function(){}); + proxyServer.on('error', function(){}); + + proxyServer.listen('8081'); + source.listen('8080'); + + var request = http.request({ + host: '127.0.0.1', + port: '8081', + method: 'GET', + path: '/', + headers: { + 'upgrade': 'websocket', + 'connection': 'Upgrade', + 'sec-websocket-key': 'dGhlIHNhbXBsZSBub25jZQ==', + 'sec-websocket-protocol': 'chat, superchat', + 'sec-websocket-version': 13 + } + }, function() {}); + request.on('error', function(){}); + request.end(); + }); }); describe('#XHeaders', function () { From 39951d568029bfb9100c9ac442b14d2bbfe288a7 Mon Sep 17 00:00:00 2001 From: Andrew Martinez Date: Sat, 19 Sep 2015 04:08:16 -0400 Subject: [PATCH 2/2] resolves #882 Fixes style issues w/ original #882 additions. --- .../lib-http-proxy-passes-ws-incoming-test.js | 45 +++++++++---------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/test/lib-http-proxy-passes-ws-incoming-test.js b/test/lib-http-proxy-passes-ws-incoming-test.js index a32046a30..9aef20850 100644 --- a/test/lib-http-proxy-passes-ws-incoming-test.js +++ b/test/lib-http-proxy-passes-ws-incoming-test.js @@ -1,7 +1,7 @@ -var httpProxy = require('../lib/http-proxy/passes/ws-incoming'), +var wsPasses = require('../lib/http-proxy/passes/ws-incoming'), expect = require('expect.js'), http = require('http'), - httpProxyLib = require('../lib/http-proxy'); + httpProxy = require('../lib/http-proxy'); describe('lib/http-proxy/passes/ws-incoming.js', function () { describe('#checkMethodAndHeader', function () { @@ -17,7 +17,7 @@ describe('lib/http-proxy/passes/ws-incoming.js', function () { destroyCalled = true; } } - returnValue = httpProxy.checkMethodAndHeader(stubRequest, stubSocket); + returnValue = wsPasses.checkMethodAndHeader(stubRequest, stubSocket); expect(returnValue).to.be(true); expect(destroyCalled).to.be(true); }) @@ -34,7 +34,7 @@ describe('lib/http-proxy/passes/ws-incoming.js', function () { destroyCalled = true; } } - returnValue = httpProxy.checkMethodAndHeader(stubRequest, stubSocket); + returnValue = wsPasses.checkMethodAndHeader(stubRequest, stubSocket); expect(returnValue).to.be(true); expect(destroyCalled).to.be(true); }) @@ -53,7 +53,7 @@ describe('lib/http-proxy/passes/ws-incoming.js', function () { destroyCalled = true; } } - returnValue = httpProxy.checkMethodAndHeader(stubRequest, stubSocket); + returnValue = wsPasses.checkMethodAndHeader(stubRequest, stubSocket); expect(returnValue).to.be(true); expect(destroyCalled).to.be(true); }) @@ -72,14 +72,13 @@ describe('lib/http-proxy/passes/ws-incoming.js', function () { destroyCalled = true; } } - returnValue = httpProxy.checkMethodAndHeader(stubRequest, stubSocket); + returnValue = wsPasses.checkMethodAndHeader(stubRequest, stubSocket); expect(returnValue).to.be(undefined); expect(destroyCalled).to.be(false); }) - it('should detect a proxyWsReq event and modify headers', function (done) { - var proxy = httpProxyLib.createProxyServer({ + var proxy = httpProxy.createProxyServer({ target: 'http://127.0.0.1:8080', ws: true }); @@ -95,18 +94,18 @@ describe('lib/http-proxy/passes/ws-incoming.js', function () { var proxyServer = http.createServer(requestHandler); proxyServer.on('upgrade', function(req, res, head){ - proxy.ws(req, res, head); + proxy.ws(req, res, head); }); var source = http.createServer(function(req, res) { - res.end(); + res.end(); }); source.on('upgrade', function(req, res, head){ - expect(req.headers['x-special-proxy-header']).to.eql('foobar'); - source.close(); - proxyServer.close(); - done(); + expect(req.headers['x-special-proxy-header']).to.eql('foobar'); + source.close(); + proxyServer.close(); + done(); }); source.on('error', function(){}); @@ -122,11 +121,11 @@ describe('lib/http-proxy/passes/ws-incoming.js', function () { method: 'GET', path: '/', headers: { - 'upgrade': 'websocket', - 'connection': 'Upgrade', - 'sec-websocket-key': 'dGhlIHNhbXBsZSBub25jZQ==', - 'sec-websocket-protocol': 'chat, superchat', - 'sec-websocket-version': 13 + 'upgrade': 'websocket', + 'connection': 'Upgrade', + 'sec-websocket-key': 'dGhlIHNhbXBsZSBub25jZQ==', + 'sec-websocket-protocol': 'chat, superchat', + 'sec-websocket-version': 13 } }, function() {}); request.on('error', function(){}); @@ -136,7 +135,7 @@ describe('lib/http-proxy/passes/ws-incoming.js', function () { describe('#XHeaders', function () { it('return if no forward request', function () { - var returnValue = httpProxy.XHeaders({}, {}, {}); + var returnValue = wsPasses.XHeaders({}, {}, {}); expect(returnValue).to.be(undefined); }); @@ -149,8 +148,8 @@ describe('lib/http-proxy/passes/ws-incoming.js', function () { headers: { host: '192.168.1.2:8080' } - } - httpProxy.XHeaders(stubRequest, {}, { xfwd: true }); + }; + wsPasses.XHeaders(stubRequest, {}, { xfwd: true }); expect(stubRequest.headers['x-forwarded-for']).to.be('192.168.1.2'); expect(stubRequest.headers['x-forwarded-port']).to.be('8080'); expect(stubRequest.headers['x-forwarded-proto']).to.be('ws'); @@ -169,7 +168,7 @@ describe('lib/http-proxy/passes/ws-incoming.js', function () { host: '192.168.1.3:8181' } }; - httpProxy.XHeaders(stubRequest, {}, { xfwd: true }); + wsPasses.XHeaders(stubRequest, {}, { xfwd: true }); expect(stubRequest.headers['x-forwarded-for']).to.be('192.168.1.3'); expect(stubRequest.headers['x-forwarded-port']).to.be('8181'); expect(stubRequest.headers['x-forwarded-proto']).to.be('wss');