Skip to content
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,8 @@ proxyServer.listen(8015);
* **proxyTimeout**: timeout (in millis) for outgoing proxy requests
* **timeout**: timeout (in millis) for incoming requests
* **followRedirects**: true/false, Default: false - specify whether you want to follow redirects
* **selfHandleResponse** true/false, if set to true, none of the webOutgoing passes are called and it's your responsibility to appropriately return the response by listening and acting on the `proxyRes` event
* **selfHandleResponse** true/false/(proxyRes, req, res): boolean, if set to true or true returned, none of the webOutgoing passes are called and it's your responsibility to appropriately return the response by listening and acting on the `proxyRes` event
* **forcePasses** true/false, if set to true the web passes will be run even if `selfHandleResponse` is also set to true.
* **buffer**: stream of data to send as the request body. Maybe you have some middleware that consumes the request stream before proxying it on e.g. If you read the body of a request into a field called 'req.rawbody' you could restream this field in the buffer option:

```
Expand Down
6 changes: 4 additions & 2 deletions lib/http-proxy/passes/web-incoming.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,9 @@ module.exports = {
proxyReq.on('response', function(proxyRes) {
if(server) { server.emit('proxyRes', proxyRes, req, res); }

if(!res.headersSent && !options.selfHandleResponse) {
const selfHandle = typeof(options.selfHandleResponse) === 'function' ? options.selfHandleResponse(proxyRes, req, res) : options.selfHandleResponse;

if(!res.headersSent && (!selfHandle || options.forcePasses)) {
for(var i=0; i < web_o.length; i++) {
if(web_o[i](req, res, proxyRes, options)) { break; }
}
Expand All @@ -184,7 +186,7 @@ module.exports = {
if (server) server.emit('end', req, res, proxyRes);
});
// We pipe to the response unless its expected to be handled by the user
if (!options.selfHandleResponse) proxyRes.pipe(res);
if (!selfHandle) proxyRes.pipe(res);
} else {
if (server) server.emit('end', req, res, proxyRes);
}
Expand Down
114 changes: 114 additions & 0 deletions test/lib-http-proxy-passes-web-incoming-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,120 @@ describe('#createProxyServer.web() using own http server', function () {
})
});

it('selfHandleResponse as a function when true is returned should not pipe response', function(done) {
var proxy = httpProxy.createProxyServer({
target: 'http://127.0.0.1:8080',
selfHandleResponse: (proxyRes, req, res) => {
return true;
}
});

function requestHandler(req, res) {
proxy.once('proxyRes', function (proxyRes, pReq, pRes) {
proxyRes.pipe(concat(function (body) {
expect(body.toString('utf8')).eql('Response');
pRes.end(Buffer.from('my-custom-response'));
}))
});

proxy.web(req, res);
}

var proxyServer = http.createServer(requestHandler);

var source = http.createServer(function(req, res) {
res.end('Response');
});

async.parallel([
next => proxyServer.listen(8086, next),
next => source.listen(8080, next)
], function (err) {
http.get('http://127.0.0.1:8086', function(res) {
res.pipe(concat(function(body) {
expect(body.toString('utf8')).eql('my-custom-response');
source.close();
proxyServer.close();
done();
}));
}).once('error', done);
})
});

it('selfHandleResponse as a function when false is returned should not pipe response', function(done) {
var proxy = httpProxy.createProxyServer({
target: 'http://127.0.0.1:8080',
selfHandleResponse: (proxyRes, req, res) => {
return false;
}
});

function requestHandler(req, res) {
proxy.web(req, res);
}

var proxyServer = http.createServer(requestHandler);

var source = http.createServer(function(req, res) {
res.end('Response');
});

async.parallel([
next => proxyServer.listen(8086, next),
next => source.listen(8080, next)
], function (err) {
http.get('http://127.0.0.1:8086', function(res) {
res.pipe(concat(function(body) {
expect(body.toString('utf8')).eql('Response');
source.close();
proxyServer.close();
done();
}));
}).once('error', done);
})
});

it('should proxy the request and provide and respond to manual user response when using modifyResponse after calling the passes', function(done) {
var proxy = httpProxy.createProxyServer({
target: 'http://127.0.0.1:8080',
selfHandleResponse: true,
autoRewrite: true,
forcePasses: true
});

function requestHandler(req, res) {
proxy.once('proxyRes', function (proxyRes, pReq, pRes) {
proxyRes.pipe(pRes);
});

proxy.web(req, res);
}

var proxyServer = http.createServer(requestHandler);

var source = http.createServer(function(req, res) {
res.setHeader('location', 'http://127.0.0.1:8080/banana');
res.statusCode = 301;
res.end();
});

async.parallel([
next => proxyServer.listen(8086, next),
next => source.listen(8080, next)
], function (err) {
http.get('http://127.0.0.1:8086', function(res) {
expect(res.statusCode).eql(301);
expect(res.headers.location).eql('http://127.0.0.1:8086/banana');

res.pipe(concat(function(body) {
source.close();
proxyServer.close();
done();
}));
}).once('error', done);
})
});

it('should proxy the request and handle changeOrigin option', function (done) {
var proxy = httpProxy.createProxyServer({
target: 'http://127.0.0.1:8080',
Expand Down