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

Do not decode the body while we are following a redirect #1554

Merged
merged 1 commit into from Jul 18, 2022
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
7 changes: 5 additions & 2 deletions lib/fetch/index.js
Expand Up @@ -1942,6 +1942,7 @@ async function httpNetworkFetch (
}

let codings = []
let location = ''

const headers = new Headers()
for (let n = 0; n < headersList.length; n += 2) {
Expand All @@ -1950,6 +1951,8 @@ async function httpNetworkFetch (

if (key.toLowerCase() === 'content-encoding') {
codings = val.split(',').map((x) => x.trim())
} else if (key.toLowerCase() === 'location') {
location = val
}

headers.append(key, val)
Expand All @@ -1960,7 +1963,7 @@ async function httpNetworkFetch (
const decoders = []

// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Encoding
if (request.method !== 'HEAD' && request.method !== 'CONNECT' && !nullBodyStatus.includes(status)) {
if (request.method !== 'HEAD' && request.method !== 'CONNECT' && !nullBodyStatus.includes(status) && !(request.redirect === 'follow' && location)) {
for (const coding of codings) {
if (/(x-)?gzip/.test(coding)) {
decoders.push(zlib.createGunzip())
Expand All @@ -1980,7 +1983,7 @@ async function httpNetworkFetch (
statusText,
headersList: headers[kHeadersList],
body: decoders.length
? pipeline(this.body, ...decoders, () => {})
? pipeline(this.body, ...decoders, () => { })
: this.body.on('error', () => {})
})

Expand Down
28 changes: 28 additions & 0 deletions test/fetch/client-fetch.js
Expand Up @@ -10,6 +10,7 @@ const { fetch, Response, Request, FormData, File } = require('../..')
const { Client, setGlobalDispatcher, Agent } = require('../..')
const nodeFetch = require('../../index-fetch')
const { once } = require('events')
const { gzipSync } = require('zlib')

setGlobalDispatcher(new Agent({
keepAliveTimeout: 1,
Expand Down Expand Up @@ -440,3 +441,30 @@ test('fetching with Request object - issue #1527', async (t) => {
await t.resolves(fetch(request))
t.end()
})

test('do not decode redirect body', (t) => {
t.plan(3)

const obj = { asd: true }
const server = createServer((req, res) => {
if (req.url === '/resource') {
t.pass('we redirect')
res.statusCode = 301
res.setHeader('location', '/resource/')
// Some dumb http servers set the content-encoding gzip
// even if there is no response
res.setHeader('content-encoding', 'gzip')
res.end()
return
}
t.pass('actual response')
res.setHeader('content-encoding', 'gzip')
res.end(gzipSync(JSON.stringify(obj)))
})
t.teardown(server.close.bind(server))

server.listen(0, async () => {
const body = await fetch(`http://localhost:${server.address().port}/resource`)
t.strictSame(JSON.stringify(obj), await body.text())
})
})