Skip to content

Instantly share code, notes, and snippets.

@nestorlafon
Created September 21, 2010 13:16
Show Gist options
  • Save nestorlafon/589675 to your computer and use it in GitHub Desktop.
Save nestorlafon/589675 to your computer and use it in GitHub Desktop.
var host='www.educacion.es';
var uri='/404.html';
var sys = require('sys');
var http = require('http');
var Iconv = require('iconv').Iconv;
var Buffer = require('buffer').Buffer;
var httpcli = http.createClient(80, host);
var request = httpcli.request( 'GET', uri, {
'host': host,
'User-Agent': 'NodeJS HTTP Client',
});
request.end();
request.on('response', function (response) {
var resBuf = new Buffer(10485760); //10MB should be enough
var resIdx = 0;
sys.debug('STATUS= ' + response.statusCode);
sys.debug('HEADERS= ' + JSON.stringify(response.headers));
response.setEncoding('binary');
response.on('data', function(chunk){
resIdx += resBuf.write(chunk, resIdx);
});
response.on('end', function(){
if ( 200 == response.statusCode ) {
var latinBuf = binISO88591toUTF8sliced( resBuf, resIdx );
}
});
});
function binISO88591toUTF8sliced( iBuf, size ) {
var siBuf = iBuf.slice(0, size);
sys.debug("siBuf size: " + siBuf.length);
sys.debug("siBuf contents: " + siBuf.toString('binary', 0, 300));
var iconv = new Iconv('ISO-8859-15', 'UTF-8');
var oBuf = iconv.convert(siBuf);
sys.debug("oBuf size: " + oBuf.length);
sys.debug("oBuf contents: " + oBuf.toString('utf8', 0, 300) );
return oBuf;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment