Skip to content

Commit

Permalink
Update utils.js
Browse files Browse the repository at this point in the history
This fix an issue with zlib: unexpected end of file with node v7.2.1 - Follow the PR: More lenient gzip decompression #2492 request/request#2492 -
  • Loading branch information
VisualFox committed Jan 24, 2017
1 parent 790775a commit 1235ef6
Showing 1 changed file with 39 additions and 23 deletions.
62 changes: 39 additions & 23 deletions lib/utils.js
Expand Up @@ -84,29 +84,45 @@ var getUrl = exports.getUrl = function(url, options) {
})
.on('response', function(res) {

if (supportGzip && ['gzip', 'deflate'].indexOf(res.headers['content-encoding']) > -1) {

var gunzip = zlib.createUnzip();
gunzip.request = res.request;
gunzip.statusCode = res.statusCode;
gunzip.headers = res.headers;

if (!options.asBuffer) {
gunzip.setEncoding("binary");
}

req.emit('response', gunzip);

res.pipe(gunzip);

} else {

if (!options.asBuffer) {
res.setEncoding("binary");
}

req.emit('response', res);
}
var contentEncoding = res.headers['content-encoding'] || 'identity';
contentEncoding = contentEncoding.trim().toLowerCase();

var zlibOptions = {
flush: zlib.Z_SYNC_FLUSH,
finishFlush: zlib.Z_SYNC_FLUSH
};

if (contentEncoding === 'gzip') {
var gunzip = zlib.createGunzip(zlibOptions);
gunzip.request = res.request;
gunzip.statusCode = res.statusCode;
gunzip.headers = res.headers;

if (!options.asBuffer) {
gunzip.setEncoding("binary");
}

req.emit('response', gunzip);
res.pipe(gunzip);
} else if (contentEncoding === 'deflate') {
var inflate = zlib.createInflate(zlibOptions);
inflate.request = res.request;
inflate.statusCode = res.statusCode;
inflate.headers = res.headers;

if (!options.asBuffer) {
inflate.setEncoding("binary");
}

req.emit('response', inflate);
res.pipe(inflate);
} else {
if (!options.asBuffer) {
res.setEncoding("binary");
}

req.emit('response', res);
}
});

req.emit('request', r);
Expand Down

0 comments on commit 1235ef6

Please sign in to comment.