Skip to content

Commit

Permalink
fix(fetch): remove content-length header on redirect (#2022)
Browse files Browse the repository at this point in the history
Fixes #2021
  • Loading branch information
KhafraDev authored Mar 25, 2023
1 parent e6fc80f commit f0271d4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/fetch/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,17 @@ const requestCache = [
'only-if-cached'
]

// https://fetch.spec.whatwg.org/#request-body-header-name
const requestBodyHeader = [
'content-encoding',
'content-language',
'content-location',
'content-type'
'content-type',
// See https://github.com/nodejs/undici/issues/2021
// 'Content-Length' is a forbidden header name, which is typically
// removed in the Headers implementation. However, undici doesn't
// filter out headers, so we add it here.
'content-length'
]

// https://fetch.spec.whatwg.org/#enumdef-requestduplex
Expand Down
32 changes: 32 additions & 0 deletions test/fetch/issue-2021.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict'

const { test } = require('tap')
const { once } = require('events')
const { createServer } = require('http')
const { fetch } = require('../..')

// https://github.com/nodejs/undici/issues/2021
test('content-length header is removed on redirect', async (t) => {
const server = createServer((req, res) => {
if (req.url === '/redirect') {
res.writeHead(302, { Location: '/redirect2' })
res.end()
return
}

res.end()
}).listen(0).unref()

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

const body = 'a+b+c'

await t.resolves(fetch(`http://localhost:${server.address().port}/redirect`, {
method: 'POST',
body,
headers: {
'content-length': Buffer.byteLength(body)
}
}))
})

0 comments on commit f0271d4

Please sign in to comment.