Skip to content

Commit

Permalink
fix: reverse decompression order of "Content-Encoding" encodings (fixes
Browse files Browse the repository at this point in the history
#2158) (#2159)

Co-authored-by: rychkog-ma <georgii.ry@moonactive.com>
  • Loading branch information
rychkog and rychkog-ma committed Jun 14, 2023
1 parent 2032e89 commit c415fbb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/fetch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1986,7 +1986,7 @@ async function httpNetworkFetch (
if (key.toLowerCase() === 'content-encoding') {
// https://www.rfc-editor.org/rfc/rfc7231#section-3.1.2.1
// "All content-coding values are case-insensitive..."
codings = val.toLowerCase().split(',').map((x) => x.trim())
codings = val.toLowerCase().split(',').map((x) => x.trim()).reverse()
} else if (key.toLowerCase() === 'location') {
location = val
}
Expand Down
33 changes: 29 additions & 4 deletions test/fetch/encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { test } = require('tap')
const { createServer } = require('http')
const { once } = require('events')
const { fetch } = require('../..')
const { createBrotliCompress, createGzip } = require('zlib')
const { createBrotliCompress, createGzip, createDeflate } = require('zlib')

test('content-encoding header is case-iNsENsITIve', async (t) => {
const contentCodings = 'GZiP, bR'
Expand All @@ -17,10 +17,10 @@ test('content-encoding header is case-iNsENsITIve', async (t) => {
res.setHeader('Content-Encoding', contentCodings)
res.setHeader('Content-Type', 'text/plain')

brotli.pipe(gzip).pipe(res)
gzip.pipe(brotli).pipe(res)

brotli.write(text)
brotli.end()
gzip.write(text)
gzip.end()
}).listen(0)

t.teardown(server.close.bind(server))
Expand All @@ -31,3 +31,28 @@ test('content-encoding header is case-iNsENsITIve', async (t) => {
t.equal(await response.text(), text)
t.equal(response.headers.get('content-encoding'), contentCodings)
})

test('response decompression according to content-encoding should be handled in a correct order', async (t) => {
const contentCodings = 'deflate, gzip'
const text = 'Hello, World!'

const server = createServer((req, res) => {
const gzip = createGzip()
const deflate = createDeflate()

res.setHeader('Content-Encoding', contentCodings)
res.setHeader('Content-Type', 'text/plain')

deflate.pipe(gzip).pipe(res)

deflate.write(text)
deflate.end()
}).listen(0)

t.teardown(server.close.bind(server))
await once(server, 'listening')

const response = await fetch(`http://localhost:${server.address().port}`)

t.equal(await response.text(), text)
})

0 comments on commit c415fbb

Please sign in to comment.