nanha
2/9/2012 - 2:52 AM

node.https.leak.test.js


var HTTP = require('http'), Sys = require('sys');

function readableSize(bytes) {
	var s = ['bytes', 'kb', 'MB', 'GB', 'TB', 'PB'];
	var e = Math.floor(Math.log(bytes)/Math.log(1024));
	return (bytes/Math.pow(1024, Math.floor(e))).toFixed(2)+" "+s[e];
}

function getMem(nice) {
	var mem = process.memoryUsage();
	if (nice) for (var i in mem) mem[i] = readableSize(mem[i]);
	return JSON.stringify(mem);
}

var getHTTPS = function() {

	var htmldata = "";
	var secure = true;
	var port = 443;
	var hostname = 'www.google.com';
	var client = HTTP.createClient(port, hostname, secure);
		
	var headers = {
		'host': hostname, 
		'user-agent': 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6', 
		'accept': '	text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
		'accept-Language': 'en-us,en;q=0.5',
		'accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.7'
	};

	var method = 'GET';
	
	var make_request = function() {
		
		request = client.request('GET', '/accounts/ServiceLogin?service=mail', headers );
		
		Sys.log('GET /accounts/ServiceLogin?service=mail');
		request.addListener('response', function (response) {
			Sys.log('STATUS: ' + response.statusCode);
			Sys.log('HEADERS: ' + JSON.stringify(response.headers));
			
			response.setEncoding('binary');
			response.addListener('data', function (chunk) {
				htmldata += chunk;
			});
			response.addListener('end', function(){ 
				Sys.log("\nMemory after request:" + getMem(true));
				//GC.collect();
				//Sys.log("\nMemory after request + GC:" + getMem(true));
			});
		});
		
		request.end();
	}
	
	Sys.log("\nMemory before request:" + getMem(true));
	make_request();
}

//var gc = require("../gc");
//global.GC = new gc.GC(); // http://github.com/billywhizz/node-gc

var interval = setInterval(getHTTPS, 2000);