// we need to keep track of the order they were sent.
var responses = [];
+ socket.setTimeout(2*60*1000); // 2 minute timeout
+ socket.addListener('timeout', function () {
+ socket.destroy();
+ });
+
var parser = parsers.alloc();
parser.reinitialize('request');
parser.socket = socket;
client.https = true;
}
+ var callbackSent = false;
+
req.addListener('response', function (res) {
if (res.statusCode < 200 || res.statusCode >= 300) {
- if (callback) callback(res.statusCode);
+ if (callback && !callbackSent) {
+ callback(res.statusCode);
+ callbackSent = true;
+ }
client.end();
return;
}
res.setBodyEncoding(encoding);
res.addListener('data', function (chunk) { content += chunk; });
res.addListener('end', function () {
- if (callback) callback(null, content);
+ if (callback && !callbackSent) {
+ callback(null, content);
+ callbackSent = true;
+ }
});
});
client.addListener("error", function (err) {
- if (callback) callback(err);
+ if (callback && !callbackSent) {
+ callback(err);
+ callbackSent = true;
+ }
});
+ client.addListener("close", function () {
+ if (callback && !callbackSent) {
+ callback(new Error('Connection closed unexpectedly'));
+ callbackSent = true;
+ }
+ });
req.end();
};
req.connection.addListener('timeout', function(){
sys.debug("TIMEOUT");
-
- var body="timeout\n";
- res.writeHead(200, {
- 'Content-Type': 'text/plain',
- 'Content-Length': body.length,
- 'Connection':'close'
- });
- res.end(body);
- req.connection.end();
server.close();
});
});
http.cat('http://localhost:8000/', 'utf8', function (err, content) {
clearTimeout(errorTimer);
- if (err) throw err;
sys.puts('HTTP REQUEST COMPLETE (this is good)');
- sys.puts(content);
});
});