Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parsing of gzipped distant files #111

Merged
merged 2 commits into from
Nov 9, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
71 changes: 60 additions & 11 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ var cli = require('cli'),
resolve = require('path').resolve,
analyzer = require('./index'),
preprocessors = new(require('./preprocessors'))(),
url = require('url');
url = require('url'),
zlib = require('zlib');

/**
* Return user agent to be used by analyze-css when making HTTP requests (issue #75)
Expand Down Expand Up @@ -46,15 +47,62 @@ function request(requestOptions, callback) {
client.get(requestOptions, function(resp) {
var out = '';

resp.on('data', function(chunk) {
out += chunk;
});

resp.on('end', function() {
debug('HTTP %d', resp.statusCode);
debug('Headers: %j', resp.headers);
callback(null, resp, out);
});
switch (resp.headers['content-encoding']) {
case 'gzip':
var gzip = zlib.createGunzip();

gzip.on('data', function (chunk) {
out += chunk;
});

gzip.on('end', function() {
debug('HTTP %d', resp.statusCode);
debug('Headers: %j', resp.headers);
callback(null, resp, out);
});

gzip.on('error', function(err) {
err = new Error('Error while decoding ' + requestOptions.href + ': ' + err.toString());
callback(err);
});

resp.pipe(gzip);

break;
case 'deflate':
var deflate = zlib.createInflate();

deflate.on('data', function (chunk) {
out += chunk;
});

deflate.on('end', function() {
debug('HTTP %d', resp.statusCode);
debug('Headers: %j', resp.headers);
callback(null, resp, out);
});

gzip.on('error', function(err) {
err = new Error('Error while decoding ' + requestOptions.href + ': ' + err.toString());
callback(err);
});

resp.pipe(deflate);

break;
default:
resp.on('data', function(chunk) {
out += chunk;
});

resp.on('end', function() {
debug('HTTP %d', resp.statusCode);
debug('Headers: %j', resp.headers);
callback(null, resp, out);
});

break;
}
}).on('error', function(err) {
debug(err);
callback(err);
Expand All @@ -81,7 +129,8 @@ function runner(options, callback) {
var requestOptions = url.parse(options.url);
requestOptions.rejectUnauthorized = !options.ignoreSslErrors;
requestOptions.headers = {
'User-Agent': getUserAgent()
'User-Agent': getUserAgent(),
'Accept-Encoding': 'gzip, deflate'
};

if (options.authUser && options.authPass) {
Expand Down