From 7d68d0205eff1726bf248bd929e981a699595698 Mon Sep 17 00:00:00 2001 From: Deividy Metheler Date: Wed, 24 Feb 2016 16:43:39 -0300 Subject: [PATCH] Emit disconnected event instead of error when ECONNRESET ECONNRESET means the other side of the TCP conversation abruptly closed its end of the connection. If we get an ECONNRESET error from the proxy request and the socket is destroyed this means that the client has closed his connection, and emit this as an error can lead to confusion and hacks to filter that kind of message. I think that the best we can do is abort and emit another event, so if any caller wants to handle with that kind of error, he can by listen the disconnected event. https://github.com/nodejitsu/node-http-proxy/issues/813 --- lib/http-proxy/passes/web-incoming.js | 10 +++++++++- test/lib-http-proxy-passes-web-incoming-test.js | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/http-proxy/passes/web-incoming.js b/lib/http-proxy/passes/web-incoming.js index af28546b3..e82559cae 100644 --- a/lib/http-proxy/passes/web-incoming.js +++ b/lib/http-proxy/passes/web-incoming.js @@ -132,7 +132,15 @@ web_o = Object.keys(web_o).map(function(pass) { req.on('error', proxyError); // Error Handler - proxyReq.on('error', proxyError); + proxyReq.on('error', function (err) { + if (req.socket.destroyed && err.code === 'ECONNRESET') { + server.emit('disconnected', err, req, res, options.target); + proxyReq.abort(); + } else { + proxyError(err); + } + }); + function proxyError (err){ if (clb) { diff --git a/test/lib-http-proxy-passes-web-incoming-test.js b/test/lib-http-proxy-passes-web-incoming-test.js index 6ca63d398..8b8f4c4e5 100644 --- a/test/lib-http-proxy-passes-web-incoming-test.js +++ b/test/lib-http-proxy-passes-web-incoming-test.js @@ -229,7 +229,7 @@ describe('#createProxyServer.web() using own http server', function () { var started = new Date().getTime(); function requestHandler(req, res) { - proxy.once('error', function (err, errReq, errRes) { + proxy.once('disconnected', function (err, errReq, errRes) { proxyServer.close(); expect(err).to.be.an(Error); expect(errReq).to.be.equal(req);