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

Proxy requests are closed when server response already sent #906

Merged
merged 3 commits into from
May 31, 2013
Merged
Show file tree
Hide file tree
Changes from 2 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
23 changes: 20 additions & 3 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ exports.request = function (method, url, options, callback, _trace) {
// Setup request

var uri = Url.parse(url);
var timeoutId;
var finishListener;
uri.method = method.toUpperCase();
uri.headers = options.headers;

Expand Down Expand Up @@ -49,6 +51,11 @@ exports.request = function (method, url, options, callback, _trace) {

req.removeAllListeners();
req.on('error', Utils.ignore);
clearTimeout(timeoutId);

if (options.response && finishListener) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't need this since request._reply will clean it up.

options.response.removeListener('finish', finishListener);
}

return callback(err, res);
}
Expand Down Expand Up @@ -122,11 +129,21 @@ exports.request = function (method, url, options, callback, _trace) {
}

if (options.timeout) {
req.setTimeout(options.timeout, function () {
timeoutId = setTimeout(function () {

req.destroy();
req.abort();
return finish(Boom.internal('Client request timeout'));
});
}, options.timeout);
}

if (options.response) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

call is downstreamRes to be more explicit

finishListener = function () {

req.abort();
return finish(Boom.internal('Server response finished before client response'));
};

options.response.once('finish', finishListener);
}

// Finalize request
Expand Down
3 changes: 2 additions & 1 deletion lib/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ internals.Proxy.prototype.handler = function () {
payload: null,
redirects: self.settings.redirects,
timeout: self.settings.timeout,
rejectUnauthorized: self.settings.rejectUnauthorized
rejectUnauthorized: self.settings.rejectUnauthorized,
response: request.raw.res
};

if (self.settings.passThrough) { // Never set with cache
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "hapi",
"description": "HTTP Server framework",
"homepage": "http://hapijs.com",
"version": "1.6.1",
"version": "1.6.2",
"author": "Eran Hammer <[email protected]> (http://hueniverse.com)",
"contributors": [
{
Expand Down
57 changes: 56 additions & 1 deletion test/integration/proxy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Load modules

var Async = require('async');
var Lab = require('lab');
var Fs = require('fs');
var Zlib = require('zlib');
Expand All @@ -25,6 +26,7 @@ describe('Proxy', function () {

var server = null;
var sslServer = null;
var timeoutServer = null;

before(function (done) {

Expand Down Expand Up @@ -218,10 +220,20 @@ describe('Proxy', function () {
{ method: 'GET', path: '/sslDefault', handler: { proxy: { mapUri: mapSslUri } } }
]);

timeoutServer = new Hapi.Server(0, { timeout: { server: 5 }});
timeoutServer.route([
{ method: 'GET', path: '/timeout1', handler: { proxy: { host: 'localhost', port: backendPort, timeout: 15 } } },
{ method: 'GET', path: '/timeout2', handler: { proxy: { host: 'localhost', port: backendPort, timeout: 2 } } },
{ method: 'GET', path: '/item', handler: { proxy: { host: 'localhost', port: backendPort } } }
]);

server.state('auto', { autoValue: 'xyz' });
server.start(function () {

sslServer.start(done);
sslServer.start(function () {

timeoutServer.start(done);
});
});
});
});
Expand Down Expand Up @@ -545,5 +557,48 @@ describe('Proxy', function () {
done();
});
});

it('doesn\'t consume all sockets when server times out before proxy', function (done) {

var wrappedReq = function (next) {

timeoutServer.inject('/timeout1', next);
};

Async.series([
wrappedReq,
wrappedReq,
wrappedReq,
wrappedReq,
wrappedReq,
wrappedReq,
wrappedReq
], function () {

timeoutServer.inject('/item', function (res) {

expect(res.statusCode).to.equal(200);
done();
});
});
});

it('times out when proxy timeout is less than server', function (done) {

timeoutServer.inject('/timeout2', function (res) {

expect(res.statusCode).to.equal(500);
done();
});
});

it('times out when server timeout is less than proxy', function (done) {

timeoutServer.inject('/timeout1', function (res) {

expect(res.statusCode).to.equal(503);
done();
});
});
});