Skip to content

Instantly share code, notes, and snippets.

@baudehlo
Created November 4, 2011 21:40
Show Gist options
  • Save baudehlo/1340555 to your computer and use it in GitHub Desktop.
Save baudehlo/1340555 to your computer and use it in GitHub Desktop.
Cannot figure out why this isn't working.
var http = require('http');
var Iconv = require('iconv').Iconv;
var bt = require('buffertools');
var google = http.createClient(80, 'www.google.it');
var assert = require('assert');
var request = google.request('GET', '/',
{
'host': 'www.google.it',
'Accept-Charset': 'UTF-8',
// Uncomment this to get the data in UTF-8 (and then it works fine)
// 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.106 Safari/535',
'Accept-Language': 'en-GB',
'Accept': 'text/html',
}
);
request.end();
request.on('response', function (response) {
var bufflist = [];
response.on('data', function (chunk) {
bufflist.push(chunk);
});
response.on('end', function () {
var ct = response.headers['content-type'];
var matches = /\bcharset\s*=\s*(?:\"|3D|')?([\w_\-]*)(?:\"|3D|')?/.exec(ct);
var enc = 'UTF-8';
if (matches) {
enc = matches[1].toUpperCase();
}
var iconv = new Iconv(enc, "UTF-8");
console.log("Converting to: " + enc);
var outbuf = bt.concat.apply(bt, bufflist);
// if I do this, it doesn't work right - the data is incorrect and I get:
// "il server non č riuscito"
// in the output
process.stdout.write(iconv.convert(outbuf));
// If however I comment that out, and uncomment this one, and run:
// node scriptname.js | iconv -f ISO-8859-1 -t UTF-8
// then I get the required output, which contains:
// "il server non è riuscito"
//process.stdout.write(outbuf);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment