-
-
Notifications
You must be signed in to change notification settings - Fork 132
fix(http): surface res.headers['set-cookie'] as an array (#5079) #5102
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
00d722a
fix(http): surface res.headers['set-cookie'] as an array (#5079)
0707082
style: rustfmt build_response_headers_object (#5079)
cc1d82a
fix(http): join duplicate Cookie request headers with '; ' per Node m…
3ffbb56
refactor(http): extract build_response_headers_object to its own modu…
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
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,120 @@ | ||
| //! Combined `IncomingMessage.headers` view construction (#5079) — split out | ||
| //! of `lib.rs` to keep that file under the 2000-line file-size cap. | ||
| //! | ||
| //! Applies Node's `_http_incoming.js` `matchKnownFields` rules to the raw | ||
| //! `(name, value)` header pairs: `set-cookie` always becomes a string array, | ||
| //! a small set of single-value headers keep the first value, `cookie` | ||
| //! duplicates join with `"; "`, and everything else joins with `", "`. | ||
|
|
||
| use std::collections::HashMap; | ||
|
|
||
| use perry_ffi::{alloc_string, JsValue, ObjectHeader}; | ||
|
|
||
| const TAG_UNDEFINED: u64 = 0x7FFC_0000_0000_0001; | ||
|
|
||
| /// Single-value response headers: per Node's `_http_incoming.js` | ||
| /// `matchKnownFields`, a duplicate of any of these is discarded (the | ||
| /// first value wins) rather than joined with `, `. `set-cookie` is not | ||
| /// in this list — it always accumulates into an array. | ||
| fn is_single_value_header(name: &str) -> bool { | ||
| matches!( | ||
| name, | ||
| "age" | ||
| | "authorization" | ||
| | "content-length" | ||
| | "content-type" | ||
| | "etag" | ||
| | "expires" | ||
| | "from" | ||
| | "host" | ||
| | "if-modified-since" | ||
| | "if-unmodified-since" | ||
| | "last-modified" | ||
| | "location" | ||
| | "max-forwards" | ||
| | "proxy-authorization" | ||
| | "referer" | ||
| | "retry-after" | ||
| | "server" | ||
| | "user-agent" | ||
| ) | ||
| } | ||
|
|
||
| /// Build the combined `IncomingMessage.headers` object from the raw | ||
| /// `(name, value)` pairs, applying Node's `matchKnownFields` rules | ||
| /// (#5079): | ||
| /// | ||
| /// * `set-cookie` → **always** a string array, even for one cookie; | ||
| /// * single-value fields ([`is_single_value_header`]) → first value wins; | ||
| /// * `cookie` → duplicates joined with `; `; | ||
| /// * everything else → duplicates joined with `, `. | ||
| /// | ||
| /// Header names are lower-cased, matching Node's `headers` view. | ||
| pub(crate) fn build_response_headers_object(raw: &[(String, String)]) -> f64 { | ||
| let mut out = f64::from_bits(TAG_UNDEFINED); | ||
|
|
||
| // Insertion-ordered accumulation. `set_cookie` is collected | ||
| // separately so a single cookie still surfaces as an array. | ||
| let mut order: Vec<String> = Vec::new(); | ||
| let mut combined: HashMap<String, String> = HashMap::new(); | ||
| let mut set_cookie: Vec<String> = Vec::new(); | ||
| let mut saw_set_cookie = false; | ||
|
|
||
| for (name, value) in raw { | ||
| let key = name.to_ascii_lowercase(); | ||
| if key == "set-cookie" { | ||
| if !saw_set_cookie { | ||
| saw_set_cookie = true; | ||
| order.push(key); | ||
| } | ||
| set_cookie.push(value.clone()); | ||
| continue; | ||
| } | ||
| match combined.get_mut(&key) { | ||
| Some(existing) => { | ||
| if !is_single_value_header(&key) { | ||
| // Node's `matchKnownFields`: duplicate `cookie` headers join | ||
| // with "; ", everything else with ", ". | ||
| existing.push_str(if key == "cookie" { "; " } else { ", " }); | ||
| existing.push_str(value); | ||
| } | ||
| } | ||
| None => { | ||
| order.push(key.clone()); | ||
| combined.insert(key, value.clone()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| let count = order.len() as u32; | ||
| let key_refs: Vec<&str> = order.iter().map(|s| s.as_str()).collect(); | ||
| let (packed, shape_id) = perry_ffi::build_object_shape(&key_refs); | ||
| let obj: *mut ObjectHeader = unsafe { | ||
| perry_ffi::js_object_alloc_with_shape(shape_id, count, packed.as_ptr(), packed.len() as u32) | ||
| }; | ||
| if !obj.is_null() { | ||
| for (i, key) in order.iter().enumerate() { | ||
| let v = if key == "set-cookie" { | ||
| let mut arr = perry_runtime::js_array_alloc(set_cookie.len() as u32); | ||
| for cookie in &set_cookie { | ||
| let ptr = | ||
| perry_runtime::js_string_from_bytes(cookie.as_ptr(), cookie.len() as u32); | ||
| arr = | ||
| perry_runtime::js_array_push(arr, perry_runtime::JSValue::string_ptr(ptr)); | ||
| } | ||
| JsValue::from_bits(perry_runtime::JSValue::array_ptr(arr).bits()) | ||
| } else if let Some(val) = combined.get(key) { | ||
| let s = alloc_string(val); | ||
| JsValue::from_string_ptr(s.as_raw()) | ||
| } else { | ||
| continue; | ||
| }; | ||
| unsafe { | ||
| perry_ffi::js_object_set_field(obj, i as u32, v); | ||
| } | ||
| } | ||
| let v = JsValue::from_object_ptr(obj as *mut u8); | ||
| out = f64::from_bits(v.bits()); | ||
| } | ||
| out | ||
| } |
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
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.