-
Notifications
You must be signed in to change notification settings - Fork 265
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
Implement get_all function to return non-folding set-cookie headers #597
Conversation
Why limit only to |
The workerd code I used as reference for implementation made an error when a header other than |
I see, I can ask internally about this. If this is already implemented in the runtime, would it make sense to just add the |
Yes, I think that is the simplest way. If you have any suggestions for improving this implementation, |
524adeb
to
e4c9aa1
Compare
Can we invoke the JavaScript |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great! Were you able to test this?
worker-sys/src/ext/headers.rs
Outdated
@@ -0,0 +1,20 @@ | |||
use wasm_bindgen::prelude::*; | |||
|
|||
#[wasm_bindgen] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this need to be buried in a glue
module like some of the other extensions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I have included it in the glue module to match the others. Included in the glue module along with others
- Update HeadersExt trait to return js_sys::Array directly - Modify Headers::get_all to handle potential invalid values - Adjust import order in ext.rs for consistency
I created the following application under the example directory and verified its operation with curl. /// create sample application. examples/cookie/src/lib.rs
use worker::*;
// Main entry point for the worker
#[event(fetch)]
pub async fn main(req: Request, env: Env, _ctx: worker::Context) -> Result<Response> {
// Set up a router and define a route for "/test_cookies"
Router::with_data(())
.get_async("/test_cookies", test_cookies)
.run(req, env)
.await
}
// Handler function for the "/test_cookies" route
async fn test_cookies(req: Request, _ctx: RouteContext<()>) -> Result<Response> {
// Retrieve all Set-Cookie headers from the input request
let input_cookies = req.headers().get_all("Set-Cookie")?;
// Create a new response
let mut res = Response::ok("Cookies processed")?;
// Add the input Set-Cookie headers to the new response
for cookie in &input_cookies {
// Append each Set-Cookie header to the response
res.headers_mut().append("Set-Cookie", cookie)?;
}
// Log the request and response headers for debugging
console_log!("Request Set-Cookie headers: {:?}", input_cookies);
console_log!("Response headers: {:?}", res.headers());
// Return the response
Ok(res)
} # curl command in console
% curl 'http://localhost:8787/test_cookies' \
-H 'Set-Cookie: a=1; Expires=Wed, 21 Oct 2015 07:28:00 GMT' \
-H 'Set-Cookie: b=2; Expires=Wed, 21 Oct 2015 07:28:00 GMT' \
-v
* Host localhost:8787 was resolved.
> GET /test_cookies HTTP/1.1
> ・・・・・・・
> Set-Cookie: a=1; Expires=Wed, 21 Oct 2015 07:28:00 GMT
> Set-Cookie: b=2; Expires=Wed, 21 Oct 2015 07:28:00 GMT
>
< HTTP/1.1 200 OK
< Content-Length: 17
< Content-Type: text/plain; charset=utf-8
< Set-Cookie: a=1; Expires=Wed, 21 Oct 2015 07:28:00 GMT
< Set-Cookie: b=2; Expires=Wed, 21 Oct 2015 07:28:00 GMT
<
* Connection #0 to host localhost left intact
Cookies processed # wrangler dev console
% cd workers-rs/examples/cookie
% npx wrangler dev
[wrangler:inf] Ready on http://localhost:8787
Request Set-Cookie headers: ["a=1; Expires=Wed, 21 Oct 2015 07:28:00 GMT", "b=2; Expires=Wed, 21 Oct 2015 07:28:00 GMT"]
Response headers: Headers {
content-type = text/plain; charset=utf-8
set-cookie = a=1; Expires=Wed, 21 Oct 2015 07:28:00 GMT
set-cookie = b=2; Expires=Wed, 21 Oct 2015 07:28:00 GMT
}
[wrangler:inf] GET /test_cookies 200 OK (12ms) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
…loudflare#597) * Add `get_all` function. (cloudflare#423) * Implement get_all using wasm-bindgen * Refactor Headers get_all method and improve error handling - Update HeadersExt trait to return js_sys::Array directly - Modify Headers::get_all to handle potential invalid values - Adjust import order in ext.rs for consistency * Included in glue module along with other modules
This PR contains the following updates: | Package | Type | Update | Change | |---|---|---|---| | [worker](https://redirect.github.com/cloudflare/workers-rs) | workspace.dependencies | minor | `0.3.0` -> `0.4.0` | --- ### Release Notes <details> <summary>cloudflare/workers-rs (worker)</summary> ### [`v0.4.0`](https://redirect.github.com/cloudflare/workers-rs/releases/tag/v0.4.0) [Compare Source](https://redirect.github.com/cloudflare/workers-rs/compare/v0.3.4...v0.4.0) #### What's Changed - Relax type of callback arguments to `Router` methods. This uses APIT to allow more types than a function pointer to implement handlers (i.e. async closure) by [@​compiler-errors](https://redirect.github.com/compiler-errors) in [cloudflare/workers-rs#605 - Fix typos in CORS argument names by [@​OliverEvans96](https://redirect.github.com/OliverEvans96) in [cloudflare/workers-rs#629 - Implement `get_all` function to return non-folding set-cookie headers by [@​nakamura-shuta](https://redirect.github.com/nakamura-shuta) in [cloudflare/workers-rs#597 - Add `FormData` conversion into `JsValue` by [@​thibmeu](https://redirect.github.com/thibmeu) in [cloudflare/workers-rs#634 > \[!CAUTION] > Breaking: Make R2 `Object::size` return `u64` by [@​lkolbly](https://redirect.github.com/lkolbly) in [cloudflare/workers-rs#625 #### New Contributors - [@​lkolbly](https://redirect.github.com/lkolbly) made their first contribution in [cloudflare/workers-rs#625 - [@​compiler-errors](https://redirect.github.com/compiler-errors) made their first contribution in [cloudflare/workers-rs#605 - [@​OliverEvans96](https://redirect.github.com/OliverEvans96) made their first contribution in [cloudflare/workers-rs#629 - [@​nakamura-shuta](https://redirect.github.com/nakamura-shuta) made their first contribution in [cloudflare/workers-rs#597 - [@​thibmeu](https://redirect.github.com/thibmeu) made their first contribution in [cloudflare/workers-rs#634 **Full Changelog**: cloudflare/workers-rs@v0.3.4...v0.4.0 </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Enabled. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR was generated by [Mend Renovate](https://mend.io/renovate/). View the [repository job log](https://developer.mend.io/github/spiraldb/vortex). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzOC43NC4xIiwidXBkYXRlZEluVmVyIjoiMzguNzQuMSIsInRhcmdldEJyYW5jaCI6ImRldmVsb3AiLCJsYWJlbHMiOltdfQ==--> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
issue : #423
Implement get_all method for Headers using wasm-bindgen
This PR implements the get_all method for Headers with the following approach:
Key changes:
Specification of the get_all method:
This implementation allows for retrieving multiple values for any header, not just 'Set-Cookie',
providing more flexibility and aligning with the Web API's Headers.getAll() method.