Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More lenient gzip decompression (#239)
Ref: nodejs/node#8701 (comment) Ref: request/request#2482 Ref: request/request#2492 Fixes: http://www.mantovanispa.it Fixes: #139
- Loading branch information
Showing
3 changed files
with
33 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -141,9 +141,18 @@ export default function fetch(url, opts) { | |
return; | ||
} | ||
|
||
// Be less strict when decoding compressed responses, since sometimes | ||
// servers send slightly invalid responses that are still accepted | ||
// by common browsers. | ||
// Always using Z_SYNC_FLUSH is what cURL does. | ||
This comment has been minimized.
Sorry, something went wrong. |
||
const zlibOptions = { | ||
flush: zlib.Z_SYNC_FLUSH, | ||
finishFlush: zlib.Z_SYNC_FLUSH | ||
}; | ||
|
||
// for gzip | ||
if (codings == 'gzip' || codings == 'x-gzip') { | ||
body = body.pipe(zlib.createGunzip()); | ||
body = body.pipe(zlib.createGunzip(zlibOptions)); | ||
resolve(new Response(body, response_options)); | ||
return; | ||
} | ||
|
@@ -156,9 +165,9 @@ export default function fetch(url, opts) { | |
raw.once('data', chunk => { | ||
// see http://stackoverflow.com/questions/37519828 | ||
if ((chunk[0] & 0x0F) === 0x08) { | ||
body = body.pipe(zlib.createInflate()); | ||
body = body.pipe(zlib.createInflate(zlibOptions)); | ||
} else { | ||
body = body.pipe(zlib.createInflateRaw()); | ||
body = body.pipe(zlib.createInflateRaw(zlibOptions)); | ||
This comment has been minimized.
Sorry, something went wrong.
bitinn
Collaborator
|
||
} | ||
resolve(new Response(body, response_options)); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I think these comments can be better, as it doesn't really explain what the code does... it simply says, browsers and curls are doing it so this is fine.
We should mention this is because node >= 6 update, using Z_SYNC_FLUSH makes zlib outputs response more digestible by Node gzip decoder.