Skip to content
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

Implement get_all function to return non-folding set-cookie headers #597

Merged
merged 5 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions worker-sys/src/ext.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
mod abort_controller;
mod cache_storage;
mod headers;
mod request;
mod response;
mod response_init;
mod websocket;

pub use abort_controller::*;
pub use cache_storage::*;
pub use headers::HeadersExt;
pub use request::*;
pub use response::*;
pub use response_init::*;
Expand Down
24 changes: 24 additions & 0 deletions worker-sys/src/ext/headers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use wasm_bindgen::prelude::*;

mod glue {
use super::*;

#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(extends = js_sys::Object)]
pub type Headers;

#[wasm_bindgen(method, js_name = getAll)]
pub fn get_all(this: &Headers, name: &str) -> js_sys::Array;
}
}

pub trait HeadersExt {
fn get_all(&self, name: &str) -> js_sys::Array;
}

impl HeadersExt for web_sys::Headers {
fn get_all(&self, name: &str) -> js_sys::Array {
self.unchecked_ref::<glue::Headers>().get_all(name)
}
}
13 changes: 13 additions & 0 deletions worker/src/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::{
use http::{header::HeaderName, HeaderMap, HeaderValue};
use js_sys::Array;
use wasm_bindgen::JsValue;
use worker_sys::ext::HeadersExt;

/// A [Headers](https://developer.mozilla.org/en-US/docs/Web/API/Headers) representation used in
/// Request and Response objects.
Expand Down Expand Up @@ -90,6 +91,18 @@ impl Headers {
// The values iterator.next() will always return a proper value containing a string
.map(|a| a.unwrap().as_string().unwrap())
}

/// Returns all the values of a header within a `Headers` object with a given name.
pub fn get_all(&self, name: &str) -> Result<Vec<String>> {
let array = self.0.get_all(name);
array
.iter()
.map(|v| {
v.as_string()
.ok_or_else(|| Error::JsError("Invalid header value".into()))
})
.collect()
}
}

impl Default for Headers {
Expand Down
Loading