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

support array of headers in WrapHandler #3941

Merged
merged 2 commits into from
Dec 11, 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
9 changes: 3 additions & 6 deletions lib/handler/wrap-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ module.exports = class WrapHandler {
onRequestUpgrade (controller, statusCode, headers, socket) {
const rawHeaders = []
for (const [key, val] of Object.entries(headers)) {
// TODO (fix): What if val is Array
rawHeaders.push(Buffer.from(key), Buffer.from(val))
rawHeaders.push(Buffer.from(key), Array.isArray(val) ? val.map(v => Buffer.from(v)) : Buffer.from(val))
}

this.#handler.onUpgrade?.(statusCode, rawHeaders, socket)
Expand All @@ -63,8 +62,7 @@ module.exports = class WrapHandler {
onResponseStart (controller, statusCode, headers, statusMessage) {
const rawHeaders = []
for (const [key, val] of Object.entries(headers)) {
// TODO (fix): What if val is Array
rawHeaders.push(Buffer.from(key), Buffer.from(val))
rawHeaders.push(Buffer.from(key), Array.isArray(val) ? val.map(v => Buffer.from(v)) : Buffer.from(val))
}

if (this.#handler.onHeaders?.(statusCode, rawHeaders, () => controller.resume(), statusMessage) === false) {
Expand All @@ -81,8 +79,7 @@ module.exports = class WrapHandler {
onResponseEnd (controller, trailers) {
const rawTrailers = []
for (const [key, val] of Object.entries(trailers)) {
// TODO (fix): What if val is Array
rawTrailers.push(Buffer.from(key), Buffer.from(val))
rawTrailers.push(Buffer.from(key), Array.isArray(val) ? val.map(v => Buffer.from(v)) : Buffer.from(val))
}

this.#handler.onComplete?.(rawTrailers)
Expand Down
4 changes: 2 additions & 2 deletions test/client-node-max-header-size.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe("Node.js' --max-http-header-size cli option", () => {

test("respect Node.js' --max-http-header-size", async (t) => {
t = tspl(t, { plan: 6 })
const command = 'node -e "require(\'.\').request(\'http://localhost:' + server.address().port + '\')"'
const command = 'node --disable-warning=ExperimentalWarning -e "require(\'.\').request(\'http://localhost:' + server.address().port + '\')"'

exec(`${command} --max-http-header-size=1`, { stdio: 'pipe' }, (err, stdout, stderr) => {
t.strictEqual(err.code, 1)
Expand All @@ -44,7 +44,7 @@ describe("Node.js' --max-http-header-size cli option", () => {

test('--max-http-header-size with Client API', async (t) => {
t = tspl(t, { plan: 6 })
const command = 'node -e "new (require(\'.\').Client)(new URL(\'http://localhost:200\'))"'
const command = 'node --disable-warning=ExperimentalWarning -e "new (require(\'.\').Client)(new URL(\'http://localhost:200\'))"'

exec(`${command} --max-http-header-size=0`, { stdio: 'pipe' }, (err, stdout, stderr) => {
t.strictEqual(err.code, 1)
Expand Down
32 changes: 32 additions & 0 deletions test/issue-3934.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict'

const { test } = require('node:test')
const { createServer } = require('node:http')
const { once } = require('node:events')
const assert = require('node:assert')
const { Agent, RetryAgent, request } = require('..')

// https://github.com/nodejs/undici/issues/3934
test('WrapHandler works with multiple header values', async (t) => {
const server = createServer(async (_req, res) => {
const headers = [
['set-cookie', 'a'],
['set-cookie', 'b'],
['set-cookie', 'c']
]
res.writeHead(200, headers)
res.end()
}).listen(0)

await once(server, 'listening')
t.after(() => server.close())

const agent = new Agent()
const retryAgent = new RetryAgent(agent)

const {
headers
} = await request(`http://localhost:${server.address().port}`, { dispatcher: retryAgent })

assert.deepStrictEqual(headers['set-cookie'], ['a', 'b', 'c'])
})
Loading