Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of client disconnect so that econnreset event is emitted instead of error #1542

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/http-proxy/passes/web-incoming.js
Expand Up @@ -154,7 +154,7 @@ module.exports = {

function createErrorHandler(proxyReq, url) {
return function proxyError(err) {
if (req.socket.destroyed && err.code === 'ECONNRESET') {
if (req.aborted && err.code === 'ECONNRESET') {
server.emit('econnreset', err, req, res, url);
return proxyReq.abort();
}
Expand Down
50 changes: 50 additions & 0 deletions test/lib-http-proxy-passes-web-incoming-test.js
Expand Up @@ -334,6 +334,56 @@ describe('#createProxyServer.web() using own http server', function () {
req.end();
});

it('should proxy the request and handle client disconnect error', function(done) {
var proxy = httpProxy.createProxyServer({
target: 'http://127.0.0.1:45002',
});

require('net').createServer().listen(45002);

var proxyServer = http.createServer(requestHandler);

var cnt = 0;
var doneOne = function() {
cnt += 1;
if(cnt === 2) done();
}

var started = new Date().getTime();
function requestHandler(req, res) {
proxy.once('econnreset', function (err, errReq, errRes) {
proxyServer.close();
expect(err).to.be.an(Error);
expect(errReq).to.be.equal(req);
expect(errRes).to.be.equal(res);
expect(err.code).to.be('ECONNRESET');
doneOne();
});

proxy.web(req, res);
}

proxyServer.listen('8086');

var req = http.request({
hostname: '127.0.0.1',
port: '8086',
method: 'GET',
}, function() {});

req.on('error', function(err) {
expect(err).to.be.an(Error);
expect(err.code).to.be('ECONNRESET');
expect(new Date().getTime() - started).to.be.greaterThan(99);
doneOne();
});
req.end();

setTimeout(function () {
req.destroy();
}, 100);
});

it('should proxy the request and provide a proxyRes event with the request and response parameters', function(done) {
var proxy = httpProxy.createProxyServer({
target: 'http://127.0.0.1:8080'
Expand Down