From e307a091348973c023cb2791454658bf0d34a6ab Mon Sep 17 00:00:00 2001 From: Cory Mawhorter Date: Mon, 6 Jul 2015 09:47:53 -0700 Subject: [PATCH 1/2] intercept proxyied response --- lib/http-proxy/passes/web-incoming.js | 25 ++++++++----- ...lib-http-proxy-passes-web-incoming-test.js | 37 +++++++++++++++++++ 2 files changed, 53 insertions(+), 9 deletions(-) diff --git a/lib/http-proxy/passes/web-incoming.js b/lib/http-proxy/passes/web-incoming.js index 4070eb316..a18ab09c1 100644 --- a/lib/http-proxy/passes/web-incoming.js +++ b/lib/http-proxy/passes/web-incoming.js @@ -144,20 +144,27 @@ web_o = Object.keys(web_o).map(function(pass) { (options.buffer || req).pipe(proxyReq); proxyReq.on('response', function(proxyRes) { + proxyRes.abort = function() { + proxyRes.aborted = true; + proxyReq.abort(); + proxyRes.abort = function(){}; + }; if(server) { server.emit('proxyRes', proxyRes, req, res); } - for(var i=0; i < web_o.length; i++) { - if(web_o[i](req, res, proxyRes, options)) { break; } - } + if (!proxyRes.aborted) { + for(var i=0; i < web_o.length; i++) { + if(web_o[i](req, res, proxyRes, options)) { break; } + } - // Allow us to listen when the proxy has completed - proxyRes.on('end', function () { - server.emit('end', req, res, proxyRes); - }); + // Allow us to listen when the proxy has completed + proxyRes.on('end', function () { + server.emit('end', req, res, proxyRes); + }); - proxyRes.pipe(res); + proxyRes.pipe(res); + } }); - //proxyReq.end(); + proxyReq.end(); } ] // <-- diff --git a/test/lib-http-proxy-passes-web-incoming-test.js b/test/lib-http-proxy-passes-web-incoming-test.js index cf9bf6b75..b8fecb7f3 100644 --- a/test/lib-http-proxy-passes-web-incoming-test.js +++ b/test/lib-http-proxy-passes-web-incoming-test.js @@ -274,6 +274,43 @@ describe('#createProxyServer.web() using own http server', function () { http.request('http://127.0.0.1:8086', function() {}).end(); }); + it('should allow the proxyRes event to be aborted and manipulated', function(done) { + var changed_response = 'response_changed'; + var proxy = httpProxy.createProxyServer({ + target: 'http://127.0.0.1:8082' + }); + + function requestHandler(req, res) { + proxy.once('proxyRes', function (proxyRes, pReq, pRes) { + proxyRes.abort(); + expect(pReq).to.be.equal(req); + expect(pRes).to.be.equal(res); + res.end(changed_response); + }); + + proxy.web(req, res); + } + + var proxyServer = http.createServer(requestHandler); + + var source = http.createServer(function(req, res) { + res.end('Response'); + }); + + proxyServer.listen('8087'); + source.listen('8082'); + http.request('http://127.0.0.1:8087', function(res) { + var chunks = []; + res.on('data', Array.prototype.push.bind(chunks)); + res.on('end', function() { + source.close(); + proxyServer.close(); + expect(chunks.join('')).to.be.equal(changed_response); + done(); + }); + }).end(); + }); + it('should proxy the request and handle changeOrigin option', function (done) { var proxy = httpProxy.createProxyServer({ target: 'http://127.0.0.1:8080', From 0feea6471c2563a173ae38e93d2debc221d47b5e Mon Sep 17 00:00:00 2001 From: Cory Mawhorter Date: Mon, 6 Jul 2015 09:48:46 -0700 Subject: [PATCH 2/2] recommented --- lib/http-proxy/passes/web-incoming.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/http-proxy/passes/web-incoming.js b/lib/http-proxy/passes/web-incoming.js index a18ab09c1..04857c4d3 100644 --- a/lib/http-proxy/passes/web-incoming.js +++ b/lib/http-proxy/passes/web-incoming.js @@ -164,7 +164,7 @@ web_o = Object.keys(web_o).map(function(pass) { } }); - proxyReq.end(); + //proxyReq.end(); } ] // <--