Skip to content

Commit

Permalink
Fix newly introduced error in error handler for ECONNREFUSED in forwa…
Browse files Browse the repository at this point in the history
…rd proxy (#1100)
  • Loading branch information
maartenth authored and jcrugzz committed Dec 2, 2016
1 parent 4edbb62 commit 927357b
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions lib/http-proxy/passes/web-incoming.js
Expand Up @@ -97,15 +97,19 @@ module.exports = {
stream: function stream(req, res, options, _, server, clb) {

// And we begin!
server.emit('start', req, res, options.target)
server.emit('start', req, res, options.target || options.forward);

if(options.forward) {
// If forward enable, so just pipe the request
var forwardReq = (options.forward.protocol === 'https:' ? https : http).request(
common.setupOutgoing(options.ssl || {}, options, req, 'forward')
);

// error handler (e.g. ECONNREFUSED)
forwardReq.on('error', proxyError);
// error handler (e.g. ECONNRESET, ECONNREFUSED)
// Handle errors on incoming request as well as it makes sense to
var forwardError = createErrorHandler(forwardReq, options.forward);
req.on('error', forwardError);
forwardReq.on('error', forwardError);

(options.buffer || req).pipe(forwardReq);
if(!options.target) { return res.end(); }
Expand Down Expand Up @@ -134,22 +138,23 @@ module.exports = {
proxyReq.abort();
});

// Handle errors on incoming request as well as it makes sense to
// handle errors in proxy and incoming request, just like for forward proxy
var proxyError = createErrorHandler(proxyReq, options.target);
req.on('error', proxyError);

// Error Handler
proxyReq.on('error', proxyError);

function proxyError (err){
if (req.socket.destroyed && err.code === 'ECONNRESET') {
server.emit('econnreset', err, req, res, options.target || options.forward);
return proxyReq.abort();
}

if (clb) {
clb(err, req, res, options.target);
} else {
server.emit('error', err, req, res, options.target);
function createErrorHandler(proxyReq, url) {
return function proxyError(err) {
if (req.socket.destroyed && err.code === 'ECONNRESET') {
server.emit('econnreset', err, req, res, url);
return proxyReq.abort();
}

if (clb) {
clb(err, req, res, url);
} else {
server.emit('error', err, req, res, url);
}
}
}

Expand Down

0 comments on commit 927357b

Please sign in to comment.