Skip to content

Commit

Permalink
Implement timeout handling for node 0.4.x
Browse files Browse the repository at this point in the history
Since there is no way to reliably abort a request in node 0.2.x this
feature won't be ported back ...
  • Loading branch information
pfleidi committed May 28, 2011
1 parent fe27976 commit afa8c2f
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions lib/wwwdude/node-versions/v0.4.x.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ exports.request = function (context) {
var url = context.url;
var headers = context.options.headers;
var payload = context.options.payload;
var request;
var request, timeoutId;

var options = {
host: url.hostname,
Expand All @@ -40,19 +40,30 @@ exports.request = function (context) {
headers['Content-Length'] = Buffer.byteLength(payload);
}

function handlerWrapper(response) {
clearTimeout(timeoutId);
context.handler(response);
}

if (url.protocol === 'https:') {
request = Https.request(options, context.handler);
request = Https.request(options, handlerWrapper);
} else {
request = Http.request(options, context.handler);
request = Http.request(options, handlerWrapper);
}

request.on('error', function (err) {
clearTimeout(timeoutId);
context.respond('error', err);
});

if (payload) {
request.write(payload);
}

timeoutId = setTimeout(function () {
request.abort();
context.respond('error', new Error('HTTP Timeout was triggered!'));
}, context.timeout);

request.end();
};

0 comments on commit afa8c2f

Please sign in to comment.