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: ignore content-length when dumping HEAD #3222

Merged
merged 2 commits into from
May 10, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/api/api-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class RequestHandler extends AsyncResource {
throw err
}

this.method = method
this.responseHeaders = responseHeaders || null
this.opaque = opaque || null
this.callback = callback
Expand Down Expand Up @@ -114,7 +115,15 @@ class RequestHandler extends AsyncResource {
const parsedHeaders = responseHeaders === 'raw' ? util.parseHeaders(rawHeaders) : headers
const contentType = parsedHeaders['content-type']
const contentLength = parsedHeaders['content-length']
const res = new Readable({ resume, abort, contentType, contentLength, highWaterMark })
const res = new Readable({
resume,
abort,
contentType,
contentLength: this.method !== 'HEAD' && contentLength
? Number(contentLength)
: null,
highWaterMark
})

if (this.removeAbortListener) {
res.on('close', this.removeAbortListener)
Expand Down
33 changes: 33 additions & 0 deletions test/client-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,39 @@ const { promisify } = require('node:util')
const { NotSupportedError, InvalidArgumentError } = require('../lib/core/errors')
const { parseFormDataString } = require('./utils/formdata')

test('request dump head', async (t) => {
t = tspl(t, { plan: 3 })

const server = createServer((req, res) => {
res.setHeader('content-length', 5 * 100)
res.flushHeaders()
res.write('hello'.repeat(100))
})
after(() => server.close())

server.listen(0, () => {
const client = new Client(`http://localhost:${server.address().port}`)
after(() => client.destroy())

let dumped = false
client.on('disconnect', () => {
t.strictEqual(dumped, true)
})
client.request({
path: '/',
method: 'HEAD'
}, (err, { body }) => {
t.ifError(err)
body.dump({ limit: 1 }).then(() => {
dumped = true
t.ok(true, 'pass')
})
})
})

await t.completed
})

test('request dump big', async (t) => {
t = tspl(t, { plan: 3 })

Expand Down
Loading