diff --git a/lib/http-proxy/passes/web-incoming.js b/lib/http-proxy/passes/web-incoming.js index 4070eb316..04857c4d3 100644 --- a/lib/http-proxy/passes/web-incoming.js +++ b/lib/http-proxy/passes/web-incoming.js @@ -144,17 +144,24 @@ 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(); 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',