2.6 KiB
Fix for Node.js HTTP Automatic Headers Issue (BUN-13559)
Problem
The Node.js test test-http-automatic-headers.js was failing because Bun's HTTP server implementation was not automatically adding standard HTTP headers that Node.js adds by default:
connection: keep-alivefor HTTP/1.1 connectionscontent-length: 0when no body is sentdateheader with current timestamp
The test was specifically failing on this assertion:
assert.strictEqual(res.headers.connection, 'keep-alive');
Root Cause
The issue was in the NodeHTTPServer__writeHead function in src/bun.js/bindings/NodeHTTP.cpp. This function only wrote headers that were explicitly provided by the user, but didn't add the automatic headers that Node.js adds by default.
Solution
Changes Made
-
Modified
NodeHTTPServer__writeHeadfunction: Added logic to track which headers are explicitly set and automatically add missing standard headers. -
Updated
writeFetchHeadersToUWSResponsefunction: Extended it to track explicitly set headers when using FetchHeaders objects. -
Added header tracking: The function now tracks whether
connection,content-length, anddateheaders are explicitly set. -
Added automatic header logic: After processing all explicit headers, the function adds:
Connection: keep-aliveif not explicitly setContent-Length: 0if not explicitly set (for responses with no body)Date: <current_timestamp>if not explicitly set
Files Modified
src/bun.js/bindings/NodeHTTP.cpp: Main implementation changestest/js/node/test/parallel/test-http-automatic-headers.js: Copied Node.js test
Technical Details
The fix ensures Node.js compatibility by:
- Connection Header: Automatically adds
Connection: keep-alivefor HTTP/1.1 unless explicitly overridden - Content-Length Header: Adds
Content-Length: 0for responses that don't explicitly set it (matching Node.js behavior for empty responses) - Date Header: Adds current GMT timestamp in RFC format
- Backward Compatibility: Only adds headers when they're not explicitly set, preserving user-defined values
The implementation handles both regular JavaScript objects and FetchHeaders objects used for header management.
Testing
The test test/js/node/test/parallel/test-http-automatic-headers.js now passes, verifying that:
- Custom headers (x-date, x-connection, x-content-length) are preserved
- Automatic headers (connection, content-length, date) are added when not explicitly set
- The behavior matches Node.js exactly
This fix improves Node.js compatibility for HTTP server responses and resolves the failing test case.