Skip to content

Instantly share code, notes, and snippets.

@cvan
Created April 3, 2014 00:35
Show Gist options
  • Save cvan/9946155 to your computer and use it in GitHub Desktop.
Save cvan/9946155 to your computer and use it in GitHub Desktop.
var fs = require('fs');
var http = require('http');
var request = require('request');
var zlib = require('zlib');
// res.headers = {
// "Date": "Thu, 03 Apr 2014 00:05:39 GMT",
// "Content-Encoding": "gzip",
// "Last-Modified": "Wed, 02 Apr 2014 23:29:18 GMT",
// "Server": "nginx",
// "Vary": "Accept-Encoding",
// "Content-Type": "application/x-javascript; charset=utf-8",
// "Cache-Control": "max-age=315357817",
// "Connection": "keep-alive",
// "Accept-Ranges": "bytes",
// "X-Backend-Server": "web11",
// "Content-Length": "1390",
// "Expires": "Sat, 30 Mar 2024 23:29:16 GMT"
// };
var headers = {
"Accept-Encoding": "gzip,deflate,sdch",
"Host": "marketplace.cdn.mozilla.net",
"Accept-Language": "en-US,en;q=0.8",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.152 Safari/537.36",
"Accept": "*/*",
"Referer": "https://marketplace.firefox.com/",
"Cookie": "__utma=47135163.2010848095.1372800783.1378849530.1390451775.3; __utmz=47135163.1372800783.1.1.utmcsr=bugzilla.mozilla.org|utmccn=(referral)|utmcmd=referral|utmcct=/show_bug.cgi",
"Connection": "keep-alive",
//"If-Modified-Since": "Wed, 02 Apr 2014 23:29:18 GMT",
"Cache-Control": "max-age=0"
};
var opts = {
method: 'GET',
// url: 'https://marketplace.cdn.mozilla.net/media/fireplace/css/include.css?b=1395770989729',
url: 'https://marketplace.cdn.mozilla.net/media/fireplace/js/l10n.js?b=1396481346261',
headers: headers
}
var req = request(opts, function(err, res, body) {
}).on('response', function(res) {
var gzip;
var rawHeaders = ('HTTP/' + res.httpVersion + ' ' + res.statusCode +
' ' + http.STATUS_CODES[res.statusCode] + '\r\n');
Object.keys(res.headers).forEach(function(headerKey) {
rawHeaders += headerKey + ': ' + res.headers[headerKey] + '\r\n';
});
rawHeaders += '\r\n';
console.log(rawHeaders)
if (res.headers['content-encoding'] == 'gzip') {
gzip = zlib.createGunzip();
var uncompressedSize = 0;
var bodySize = 0;
var body = '';
res.on('data', function (data) {
console.log(data.length)
bodySize += data.length;
})
gzip.on('data', function (data) {
body += data;
uncompressedSize += data.length;
});
gzip.on('end', function() {
console.log(body);
console.log('headersSize:', Buffer.byteLength(rawHeaders, 'utf8')); // 386
console.log('content.size uncompressed:', uncompressedSize); // 3957
console.log('bodySize compressed:', bodySize); // 1390
});
res.pipe(gzip);
} else {
output = res;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment