Created
February 23, 2010 02:31
-
-
Save abecciu/311783 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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