fix(node:http): emit headers passed via res.writeHead (#2132) - #2335
Merged
Conversation
writeHead's headers argument was typed NA_STR in the native dispatch table, so a headers *object* was coerced to the literal string "[object Object]" and every header set through res.writeHead(status, headers) / res.writeHead(status, statusMessage, headers) was silently dropped from the response. Pass arg2/arg3 as raw JSValues (NA_JSV) and resolve the Node overloads in the runtime: a string in slot 2 is the statusMessage, a heap object in slot 2 or 3 is the bulk headers, which are JSON-serialized via js_json_stringify and merged (lowercase lookup key, original case retained for getHeaderNames).
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes a concrete byte-level diff from #2132: headers set via
res.writeHead(...)were silently dropped from the http(s) server response.writeHead's headers argument was typedNA_STRin the native dispatch table (native_table/http.rs), so when a user passed a headers object it was coerced to the literal string"[object Object]"— never valid JSON — and the runtime discarded it. ThewriteHead(status, statusMessage, headers)overload also couldn't be distinguished fromwriteHead(status, headers)because both arg slots were fixed strings.Before / after (raw response bytes vs Node)
All overload forms now emit their headers:
writeHead(status),writeHead(status, headersObj), andwriteHead(status, statusMessage, headersObj).How
native_table/http.rs:writeHeadargs[NA_F64, NA_STR, NA_STR]→[NA_F64, NA_JSV, NA_JSV]so arg2/arg3 reach the runtime as raw NaN-boxed JSValues. The codegen extern decl was already[I64, DOUBLE, I64, I64], so this is ABI-compatible.response.rs::js_node_http_res_write_head: resolve the overloads in the runtime —is_string()(STRING_TAG) ⇒statusMessage,is_pointer()(POINTER_TAG heap object) ⇒ bulkheaders. Headers objects are serialized withjs_json_stringifyand merged via a newapply_headers_jsonhelper (lowercase lookup key, original case retained forgetHeaderNames()).Scope
This fixes the dropped-header class of #2132's diffs. The remaining
#2132differences — lowercase header casing, header ordering, custom reason-phrase (202 Accepted!), and chunked-vs-content-length framing — are all serialized by hyper (which lowercases names, controls ordering, and derives canonical reason phrases) and are out of scope for this change; they'd need a separate response-writer change. The issue explicitly invites such per-cluster sub-fixes.Test plan
response.rsforapply_headers_json(case preservation, non-string value stringification, empty/sentinel handling).cargo test -p perry-ext-http-server— 17 passed.setHeaderand all threewriteHeadoverloads.