From beb1db967b01ec79455ffa825f0e3b6faeedfaf6 Mon Sep 17 00:00:00 2001 From: pfg Date: Tue, 20 May 2025 21:32:07 -0700 Subject: [PATCH] Fix numeric header in node http server (#19811) --- src/bun.js/bindings/NodeHTTP.cpp | 2 +- test/js/node/http/numeric-header.test.ts | 41 ++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 test/js/node/http/numeric-header.test.ts diff --git a/src/bun.js/bindings/NodeHTTP.cpp b/src/bun.js/bindings/NodeHTTP.cpp index e193d7b5e9..540b179842 100644 --- a/src/bun.js/bindings/NodeHTTP.cpp +++ b/src/bun.js/bindings/NodeHTTP.cpp @@ -675,7 +675,7 @@ static void assignHeadersFromUWebSocketsForCall(uWS::HttpRequest* request, JSVal RETURN_IF_EXCEPTION(scope, void()); } else { - headersObject->putDirect(vm, nameIdentifier, jsValue, 0); + headersObject->putDirectMayBeIndex(globalObject, nameIdentifier, jsValue); arrayValues.append(nameString); arrayValues.append(jsValue); RETURN_IF_EXCEPTION(scope, void()); diff --git a/test/js/node/http/numeric-header.test.ts b/test/js/node/http/numeric-header.test.ts new file mode 100644 index 0000000000..7856f10a4d --- /dev/null +++ b/test/js/node/http/numeric-header.test.ts @@ -0,0 +1,41 @@ +import { describe, expect, test } from "bun:test"; +import { createServer } from "http"; + +describe("HTTP numeric headers", () => { + test("should handle numeric header names", async () => { + const server = createServer((req, res) => { + // Get the custom header value + expect(req.headers["1234"]).toBe("Hello from client!"); + expect("1234" in req.headers).toBe(true); + expect(req.headers[1234]).toBe("Hello from client!"); + + const customHeader = req.headers["1234"]; + + // Send response with the header value + res.writeHead(200, { "Content-Type": "text/plain" }); + res.end(`Received header value: ${customHeader}`); + }); + + // Start server on random port + const port = await new Promise(resolve => { + server.listen(0, () => { + const address = server.address(); + if (address && typeof address === "object") { + resolve(address.port); + } + }); + }); + + // Make fetch request to the server + const response = await fetch(`http://localhost:${port}/`, { + headers: { + "1234": "Hello from client!", + }, + }); + + const data = await response.text(); + expect(data).toBe("Received header value: Hello from client!"); + + server.close(); + }); +});