Repository navigation
node:http: copy the array values of a header list in writeHead() and res.headers= #44778
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
Open
robobun
wants to merge
3
commits into
main
Choose a base branch
from
robobun/5cba35a2/writehead-header-list-copy-in
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,200 @@ | ||
| // A header list is the array form of a headers argument: [name, value, name, value] or [[name, value], ...]. | ||
| // A value slot can hold an array: one header line per element. | ||
| import { describe, expect, test } from "bun:test"; | ||
| import { once } from "node:events"; | ||
| import { createServer, IncomingMessage, ServerResponse } from "node:http"; | ||
| import type { AddressInfo } from "node:net"; | ||
|
|
||
| describe("a header list does not write into an array that the caller keeps", () => { | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| // A server can keep the array of a value slot in a constant and name the same header again in the same list. | ||
| // The later value must not go into the constant, or every later response carries it. | ||
| const THEME = "theme=light; Path=/"; | ||
| const LANG = "lang=en; Path=/"; | ||
| const users = ["alice", "bob", "carol"]; | ||
| type Respond = (res: any, kept: string[], sid: string) => void; | ||
|
|
||
| // Answers one request per user with `respond`. Returns the Set-Cookie lines that each user received, and the | ||
| // kept array as the last response left it. | ||
| async function cookiesPerUser(respond: Respond, kept: string[] = [THEME, LANG]) { | ||
| const errors: string[] = []; | ||
| const server = createServer((req, res) => { | ||
| try { | ||
| respond(res, kept, `sid=${req.headers["x-user"]}`); | ||
| } catch (error) { | ||
| errors.push(String(error)); | ||
| } | ||
| res.end(); | ||
| }); | ||
| try { | ||
| await once(server.listen(0, "127.0.0.1"), "listening"); | ||
| const { port } = server.address() as AddressInfo; | ||
| const received: string[][] = []; | ||
| for (const user of users) { | ||
| const response = await fetch(`http://127.0.0.1:${port}/`, { headers: { "x-user": user } }); | ||
| received.push(response.headers.getSetCookie()); | ||
| await response.arrayBuffer(); | ||
| } | ||
| return { received, kept, errors }; | ||
| } finally { | ||
| server.closeAllConnections(); | ||
| server.close(); | ||
| } | ||
| } | ||
| const ownCookieOnly = { received: users.map(user => [THEME, LANG, `sid=${user}`]), kept: [THEME, LANG], errors: [] }; | ||
|
|
||
| // The `name` lines of the head that a ServerResponse with no socket wrote. | ||
| function headLines(res: ServerResponse, name: string) { | ||
| const head: string = (res as any).outputData.map((x: any) => String(x.data)).join(""); | ||
| return head | ||
| .split("\r\n") | ||
| .filter(line => line.startsWith(`${name}: `)) | ||
| .map(line => line.slice(name.length + 2)); | ||
| } | ||
|
|
||
| describe("writeHead(), with the same result on Node.js v26.3.0", () => { | ||
| const lists: [string, Respond][] = [ | ||
| ["a flat list", (res, kept, sid) => res.writeHead(200, ["Set-Cookie", kept, "Set-Cookie", sid])], | ||
| [ | ||
| "a list of pairs", | ||
| (res, kept, sid) => | ||
| res.writeHead(200, [ | ||
| ["Set-Cookie", kept], | ||
| ["Set-Cookie", sid], | ||
| ]), | ||
| ], | ||
| [ | ||
| "a status message, then a flat list", | ||
| (res, kept, sid) => res.writeHead(200, "OK", ["Set-Cookie", kept, "Set-Cookie", sid]), | ||
| ], | ||
| [ | ||
| "a name repeated in another case", | ||
| (res, kept, sid) => res.writeHead(200, ["Set-Cookie", kept, "set-cookie", sid]), | ||
| ], | ||
| [ | ||
| "an array value after the kept array", | ||
| (res, kept, sid) => res.writeHead(200, ["Set-Cookie", kept, "Set-Cookie", [sid]]), | ||
| ], | ||
| ]; | ||
| test.each(lists)("%s", async (_name, respond) => { | ||
| expect(await cookiesPerUser(respond)).toEqual(ownCookieOnly); | ||
| }); | ||
|
|
||
| test("a frozen array in a flat list", async () => { | ||
| const respond: Respond = (res, kept, sid) => res.writeHead(200, ["Set-Cookie", kept, "Set-Cookie", sid]); | ||
| expect(await cookiesPerUser(respond, Object.freeze([THEME, LANG]) as string[])).toEqual(ownCookieOnly); | ||
| }); | ||
|
|
||
| test.each(["Set-Cookie", "Link"])("a ServerResponse with no socket, %s", name => { | ||
| const kept = [THEME, LANG]; | ||
| const received = users.map(user => { | ||
| const res = new ServerResponse(new IncomingMessage(null as any)); | ||
| res.writeHead(200, [name, kept, name, `sid=${user}`]); | ||
| res.end(); | ||
| return headLines(res, name); | ||
| }); | ||
| expect({ received, kept, errors: [] }).toEqual(ownCookieOnly); | ||
| }); | ||
|
|
||
| // Node renders the head inside writeHead(). A list value is fixed there, so a later push is not sent. | ||
| const lateWrites: [string, (res: any, cookies: string[]) => void][] = [ | ||
| ["a flat list", (res, cookies) => res.writeHead(200, ["Set-Cookie", cookies])], | ||
| ["a list of pairs", (res, cookies) => res.writeHead(200, [["Set-Cookie", cookies]])], | ||
| ]; | ||
| test.each(lateWrites)("a value pushed after writeHead() with %s is not sent", async (_name, writeHead) => { | ||
| const { received } = await cookiesPerUser((res, _kept, sid) => { | ||
| const cookies = [THEME, LANG]; | ||
| writeHead(res, cookies); | ||
| cookies.push(sid); | ||
| }); | ||
| expect(received).toEqual(users.map(() => [THEME, LANG])); | ||
| }); | ||
| }); | ||
|
|
||
| // Deliberate divergence from Node v26.3.0. With a header already set, Node merges a flat list into its header | ||
| // store and appends to the caller's array there: the second user receives the cookie of the first user. | ||
| // https://github.com/nodejs/node/blob/v26.3.0/lib/_http_server.js#L450-L453 | ||
| // In that state Node takes no list of pairs at all (ERR_INVALID_ARG_TYPE, or ERR_INVALID_ARG_VALUE for an odd | ||
| // count). Bun takes both forms and appends to a copy. | ||
| describe("writeHead() after a header was set", () => { | ||
| const lists: [string, Respond][] = [ | ||
| [ | ||
| "a flat list", | ||
| (res, kept, sid) => { | ||
| res.setHeader("X-Any", "1"); | ||
| res.writeHead(200, ["Set-Cookie", kept, "Set-Cookie", sid]); | ||
| }, | ||
| ], | ||
| [ | ||
| "a list of pairs", | ||
| (res, kept, sid) => { | ||
| res.setHeader("X-Any", "1"); | ||
| res.writeHead(200, [ | ||
| ["Set-Cookie", kept], | ||
| ["Set-Cookie", sid], | ||
| ]); | ||
| }, | ||
| ], | ||
| [ | ||
| "a list of pairs that names a header given to setHeader() as an array", | ||
| (res, kept, sid) => { | ||
| res.setHeader("Set-Cookie", kept); | ||
| res.writeHead(200, [["Set-Cookie", sid]]); | ||
| }, | ||
| ], | ||
| [ | ||
| "a list of pairs that names a header given to appendHeader() as an array", | ||
| (res, kept, sid) => { | ||
| res.appendHeader("Set-Cookie", kept); | ||
| res.writeHead(200, [["Set-Cookie", sid]]); | ||
| }, | ||
| ], | ||
| ]; | ||
| test.each(lists)("%s", async (_name, respond) => { | ||
| expect(await cookiesPerUser(respond)).toEqual(ownCookieOnly); | ||
| }); | ||
|
|
||
| test("a list of pairs that names a header given to setHeader() as an array, Link", () => { | ||
| const kept = [THEME, LANG]; | ||
| const received = users.map(user => { | ||
| const res = new ServerResponse(new IncomingMessage(null as any)); | ||
| res.setHeader("Link", kept); | ||
| res.writeHead(200, [["Link", `sid=${user}`]]); | ||
| res.end(); | ||
| return headLines(res, "Link"); | ||
| }); | ||
| expect({ received, kept, errors: [] }).toEqual(ownCookieOnly); | ||
| }); | ||
| }); | ||
|
|
||
| // `res.headers =` is a Bun extension. Each form takes a copy of an array value. | ||
| describe("res.headers =", () => { | ||
| const forms: [string, Respond][] = [ | ||
| [ | ||
| "a list of pairs", | ||
| (res, kept, sid) => | ||
| (res.headers = [ | ||
| ["Set-Cookie", kept], | ||
| ["Set-Cookie", sid], | ||
| ]), | ||
| ], | ||
| [ | ||
| "an object with entries()", | ||
| (res, kept, sid) => | ||
| (res.headers = new Map<string, string | string[]>([ | ||
| ["Set-Cookie", kept], | ||
| ["set-cookie", sid], | ||
| ])), | ||
| ], | ||
| [ | ||
| "a plain object, then appendHeader()", | ||
| (res, kept, sid) => { | ||
| res.headers = { "Set-Cookie": kept }; | ||
| res.appendHeader("Set-Cookie", sid); | ||
| }, | ||
| ], | ||
| ]; | ||
| test.each(forms)("%s", async (_name, respond) => { | ||
| expect(await cookiesPerUser(respond)).toEqual(ownCookieOnly); | ||
| }); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.