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

More lenient gzip decompression #2492

Merged
merged 1 commit into from Dec 21, 2016
Merged
Show file tree
Hide file tree
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
13 changes: 11 additions & 2 deletions request.js
Expand Up @@ -946,11 +946,20 @@ Request.prototype.onRequestResponse = function (response) {
var contentEncoding = response.headers['content-encoding'] || 'identity'
contentEncoding = contentEncoding.trim().toLowerCase()

// Be more lenient with decoding compressed responses, since (very rarely)
// servers send slightly invalid gzip responses that are still accepted
// by common browsers.
// Always using Z_SYNC_FLUSH is what cURL does.
var zlibOptions = {
flush: zlib.Z_SYNC_FLUSH
, finishFlush: zlib.Z_SYNC_FLUSH
}

if (contentEncoding === 'gzip') {
responseContent = zlib.createGunzip()
responseContent = zlib.createGunzip(zlibOptions)
response.pipe(responseContent)
} else if (contentEncoding === 'deflate') {
responseContent = zlib.createInflate()
responseContent = zlib.createInflate(zlibOptions)
response.pipe(responseContent)
} else {
// Since previous versions didn't check for Content-Encoding header,
Expand Down
16 changes: 16 additions & 0 deletions tests/test-gzip.js
Expand Up @@ -39,6 +39,12 @@ var server = http.createServer(function(req, res) {
res.writeHead(200)
res.write(testContentBigGzip.slice(0, 4096))
setTimeout(function() { res.end(testContentBigGzip.slice(4096)) }, 10)
} else if (req.url === '/just-slightly-truncated') {
zlib.gzip(testContent, function(err, data) {
assert.equal(err, null)
// truncate the CRC checksum and size check at the end of the stream
res.end(data.slice(0, data.length-8))
})
} else {
zlib.gzip(testContent, function(err, data) {
assert.equal(err, null)
Expand Down Expand Up @@ -96,6 +102,16 @@ tape('transparently supports gzip decoding to callbacks', function(t) {
})
})

tape('supports slightly invalid gzip content', function(t) {
var options = { url: server.url + '/just-slightly-truncated', gzip: true }
request.get(options, function(err, res, body) {
t.equal(err, null)
t.equal(res.headers['content-encoding'], 'gzip')
t.equal(body, testContent)
t.end()
})
})

tape('transparently supports gzip decoding to pipes', function(t) {
var options = { url: server.url + '/foo', gzip: true }
var chunks = []
Expand Down