[fix] handle error on incoming request as well and properly abort proxy if client request is aborted

This commit is contained in:
Jarrett Cruger 2014-04-14 13:08:10 -04:00
parent d908e2ad61
commit 77a1cff9bc

View File

@ -91,9 +91,7 @@ web_o = Object.keys(web_o).map(function(pass) {
function stream(req, res, options, _, server, clb) {
//
// And we begin!
//
if (!clb) {
server.emit('start', req, res, options.target)
}
@ -111,14 +109,24 @@ web_o = Object.keys(web_o).map(function(pass) {
common.setupOutgoing(options.ssl || {}, options, req)
);
// Ensure we abort proxy if request is aborted
req.on('aborted', function () {
proxyReq.abort();
});
// Handle errors on incoming request as well as it makes sense to
req.on('error', proxyError);
// Error Handler
proxyReq.on('error', function(err){
proxyReq.on('error', proxyError);
function proxyError (err){
if (clb) {
clb(err, req, res);
} else {
server.emit('error', err, req, res);
}
});
}
(options.buffer || req).pipe(proxyReq);
@ -128,9 +136,7 @@ web_o = Object.keys(web_o).map(function(pass) {
if(web_o[i](req, res, proxyRes)) { break; }
}
//
// Allow us to listen when the proxy has completed
//
proxyRes.on('end', function () {
if (!clb) {
server.emit('end', req, res, proxyRes);