Skip to content

Move timeout from the incoming socket to the outgoing one. #649

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/http-proxy/passes/web-incoming.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ web_o = Object.keys(web_o).map(function(pass) {
common.setupOutgoing(options.ssl || {}, options, req)
);

if(options.proxy_timeout) {
proxyReq.on('socket', function (socket) {
socket.setTimeout(options.proxy_timeout);
socket.on('timeout', function() {
proxyReq.abort();
});
});
}

// Ensure we abort proxy if request is aborted
req.on('aborted', function () {
proxyReq.abort();
Expand Down
2 changes: 1 addition & 1 deletion test/lib-http-proxy-passes-web-incoming-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,4 @@ describe('#createProxyServer.web() using own http server', function () {
source.listen('8080');
http.request('http://127.0.0.1:8084', function() {}).end();
});
});
});
39 changes: 38 additions & 1 deletion test/lib-http-proxy-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,44 @@ describe('lib/http-proxy.js', function() {

testReq.end();
})
})
});

describe('#createProxyServer setting the correct proxy timeout value', function () {
it('should hang up the socket at the timeout', function (done) {
this.timeout(30);
var ports = { source: gen.port, proxy: gen.port };
var proxy = httpProxy.createProxyServer({
target: 'http://127.0.0.1:' + ports.source,
proxy_timeout: 3
}).listen(ports.proxy);

proxy.on('error', function (err, req, res) {
res.writeHead(500, {'Content-Type': 'text/plain'});
res.end('Something went wrong with the request: ' + err);
});

var source = http.createServer(function(req, res) {
setTimeout(function () {
res.end('At this point the socket should be closed');
}, 5)
});

source.listen(ports.source);

var testReq = http.request({
hostname: '127.0.0.1',
port: ports.proxy,
method: 'GET',
}, function(res) {
expect(res.statusCode).to.be.eql(500);
proxy._server.close();
source.close();
done();
});

testReq.end();
})
});

// describe('#createProxyServer using the web-incoming passes', function () {
// it('should emit events correclty', function(done) {
Expand Down