Skip to content

Instantly share code, notes, and snippets.

@abecciu
Created February 23, 2010 02:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abecciu/311783 to your computer and use it in GitHub Desktop.
Save abecciu/311783 to your computer and use it in GitHub Desktop.
var sys = require('sys');
var http = require('http');
var url_mod = require('url');
var url = process.ARGV[2];
var parsed_url = url_mod.parse(url);
var client = http.createClient(parsed_url.port || 80, parsed_url.hostname);
client.setTimeout(1000);
var timeoutCount = 0;
var lastDate = (new Date()).getTime();
client.addListener('error', function(e) {
sys.debug('Error: ' + e);
});
client.addListener('timeout', function(e) {
var now = (new Date()).getTime();
var diff = now - lastDate;
lastDate = now;
sys.debug('[' + diff + '] Timeout: ' + e);
timeoutCount += 1;
if (timeoutCount >= 4) {
sys.debug("Closing...");
client.forceClose();
}
});
var req = client.request('GET', parsed_url.pathname, {host: parsed_url.host});
req.addListener('response', function (response) {
var body = '';
response.setBodyEncoding("utf8");
response.addListener("data", function (chunk) {
body += chunk;
});
response.addListener('end', function() {
sys.puts("STATUS: " + response.statusCode);
sys.puts("HEADERS: " + JSON.stringify(response.headers));
sys.puts("BODY: " + body);
});
});
req.close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment