Skip to content

Manipulate or cancel proxyRes #850

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions lib/http-proxy/passes/web-incoming.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
37 changes: 37 additions & 0 deletions test/lib-http-proxy-passes-web-incoming-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down