mirror of
https://github.com/http-party/node-http-proxy.git
synced 2025-12-08 20:59:18 +00:00
Fix newly introduced error in error handler for ECONNREFUSED in forward proxy (#1100)
This commit is contained in:
parent
4edbb62cc5
commit
927357bedc
@ -97,15 +97,19 @@ module.exports = {
|
|||||||
stream: function stream(req, res, options, _, server, clb) {
|
stream: function stream(req, res, options, _, server, clb) {
|
||||||
|
|
||||||
// And we begin!
|
// And we begin!
|
||||||
server.emit('start', req, res, options.target)
|
server.emit('start', req, res, options.target || options.forward);
|
||||||
|
|
||||||
if(options.forward) {
|
if(options.forward) {
|
||||||
// If forward enable, so just pipe the request
|
// If forward enable, so just pipe the request
|
||||||
var forwardReq = (options.forward.protocol === 'https:' ? https : http).request(
|
var forwardReq = (options.forward.protocol === 'https:' ? https : http).request(
|
||||||
common.setupOutgoing(options.ssl || {}, options, req, 'forward')
|
common.setupOutgoing(options.ssl || {}, options, req, 'forward')
|
||||||
);
|
);
|
||||||
|
|
||||||
// error handler (e.g. ECONNREFUSED)
|
// error handler (e.g. ECONNRESET, ECONNREFUSED)
|
||||||
forwardReq.on('error', proxyError);
|
// 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);
|
(options.buffer || req).pipe(forwardReq);
|
||||||
if(!options.target) { return res.end(); }
|
if(!options.target) { return res.end(); }
|
||||||
@ -134,22 +138,23 @@ module.exports = {
|
|||||||
proxyReq.abort();
|
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);
|
req.on('error', proxyError);
|
||||||
|
|
||||||
// Error Handler
|
|
||||||
proxyReq.on('error', proxyError);
|
proxyReq.on('error', proxyError);
|
||||||
|
|
||||||
function proxyError (err){
|
function createErrorHandler(proxyReq, url) {
|
||||||
|
return function proxyError(err) {
|
||||||
if (req.socket.destroyed && err.code === 'ECONNRESET') {
|
if (req.socket.destroyed && err.code === 'ECONNRESET') {
|
||||||
server.emit('econnreset', err, req, res, options.target || options.forward);
|
server.emit('econnreset', err, req, res, url);
|
||||||
return proxyReq.abort();
|
return proxyReq.abort();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (clb) {
|
if (clb) {
|
||||||
clb(err, req, res, options.target);
|
clb(err, req, res, url);
|
||||||
} else {
|
} else {
|
||||||
server.emit('error', err, req, res, options.target);
|
server.emit('error', err, req, res, url);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user