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

fix: define conditions when content-length should be sent #2305

Merged
merged 7 commits into from
Oct 26, 2023
7 changes: 6 additions & 1 deletion lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -1538,7 +1538,12 @@ function write (client, request) {
contentLength = null
}

if (request.contentLength !== null && request.contentLength !== contentLength) {
// https://github.com/nodejs/undici/issues/2046
// A user agent may send a Content-Length header with 0 value, this should be
// allowed.
const bypassDelete = method === 'DELETE' && contentLength === null && request.contentLength === 0

if (!bypassDelete && request.contentLength !== null && request.contentLength !== contentLength) {
metcoder95 marked this conversation as resolved.
Show resolved Hide resolved
if (client[kStrictContentLength]) {
errorRequest(client, request, new RequestContentLengthMismatchError())
return false
Expand Down
37 changes: 36 additions & 1 deletion test/content-length.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const { Readable } = require('stream')
const { maybeWrapStream, consts } = require('./utils/async-iterators')

test('request invalid content-length', (t) => {
t.plan(10)
t.plan(11)

const server = createServer((req, res) => {
res.end()
Expand Down Expand Up @@ -134,6 +134,16 @@ test('request invalid content-length', (t) => {
}, (err, data) => {
t.type(err, errors.RequestContentLengthMismatchError)
})

client.request({
path: '/',
method: 'DELETE',
headers: {
'content-length': 4
}
}, (err, data) => {
t.type(err, errors.RequestContentLengthMismatchError)
})
})
})

Expand Down Expand Up @@ -329,3 +339,28 @@ test('request streaming with Readable.from(buf)', (t) => {
})
})
})

test('request DELETE and content-length=0', (t) => {
t.plan(2)
const server = createServer((req, res) => {
res.end()
metcoder95 marked this conversation as resolved.
Show resolved Hide resolved
})
t.teardown(server.close.bind(server))
server.listen(0, () => {
const client = new Client(`http://localhost:${server.address().port}`)
t.teardown(client.destroy.bind(client))

client.request({
path: '/',
method: 'DELETE',
headers: {
'content-length': 0
}
}, (err) => {
t.error(err)
})
client.on('disconnect', () => {
t.pass()
})
})
})