Skip to content

Commit f0271d4

Browse files
authoredMar 25, 2023
fix(fetch): remove content-length header on redirect (#2022)
Fixes #2021
1 parent e6fc80f commit f0271d4

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed
 

‎lib/fetch/constants.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,17 @@ const requestCache = [
4848
'only-if-cached'
4949
]
5050

51+
// https://fetch.spec.whatwg.org/#request-body-header-name
5152
const requestBodyHeader = [
5253
'content-encoding',
5354
'content-language',
5455
'content-location',
55-
'content-type'
56+
'content-type',
57+
// See https://github.com/nodejs/undici/issues/2021
58+
// 'Content-Length' is a forbidden header name, which is typically
59+
// removed in the Headers implementation. However, undici doesn't
60+
// filter out headers, so we add it here.
61+
'content-length'
5662
]
5763

5864
// https://fetch.spec.whatwg.org/#enumdef-requestduplex

‎test/fetch/issue-2021.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict'
2+
3+
const { test } = require('tap')
4+
const { once } = require('events')
5+
const { createServer } = require('http')
6+
const { fetch } = require('../..')
7+
8+
// https://github.com/nodejs/undici/issues/2021
9+
test('content-length header is removed on redirect', async (t) => {
10+
const server = createServer((req, res) => {
11+
if (req.url === '/redirect') {
12+
res.writeHead(302, { Location: '/redirect2' })
13+
res.end()
14+
return
15+
}
16+
17+
res.end()
18+
}).listen(0).unref()
19+
20+
t.teardown(server.close.bind(server))
21+
await once(server, 'listening')
22+
23+
const body = 'a+b+c'
24+
25+
await t.resolves(fetch(`http://localhost:${server.address().port}/redirect`, {
26+
method: 'POST',
27+
body,
28+
headers: {
29+
'content-length': Buffer.byteLength(body)
30+
}
31+
}))
32+
})

0 commit comments

Comments
 (0)