From e4286baf38feaf00faa93660cd351576a101980c Mon Sep 17 00:00:00 2001 From: Zak Stucke Date: Tue, 9 Jul 2024 18:49:34 +0100 Subject: [PATCH] Merge --- .zetch.lock | 3 +- rust/bitbazaar/cookies/cookies.rs | 56 +++++++++++++++++-------------- 2 files changed, 33 insertions(+), 26 deletions(-) diff --git a/.zetch.lock b/.zetch.lock index f62287a8..6012671c 100644 --- a/.zetch.lock +++ b/.zetch.lock @@ -18,6 +18,7 @@ "py_rust/LICENSE.zetch.md": "d2c12e539d357957b950a54a5477c3a9f87bd2b3ee707be7a4db7adaf5aacc2b", "py_rust/README.zetch.md": "29069ce3e8571383bb6872a764d92933e44f529b20a1fe96cd60b09bacfb385f", "rust/LICENSE.zetch.md": "d2c12e539d357957b950a54a5477c3a9f87bd2b3ee707be7a4db7adaf5aacc2b", - "rust/README.zetch.md": "29069ce3e8571383bb6872a764d92933e44f529b20a1fe96cd60b09bacfb385f" + "rust/README.zetch.md": "29069ce3e8571383bb6872a764d92933e44f529b20a1fe96cd60b09bacfb385f", + "rust/pkg/LICENSE.zetch.md": "d2c12e539d357957b950a54a5477c3a9f87bd2b3ee707be7a4db7adaf5aacc2b" } } \ No newline at end of file diff --git a/rust/bitbazaar/cookies/cookies.rs b/rust/bitbazaar/cookies/cookies.rs index a402a484..08066010 100644 --- a/rust/bitbazaar/cookies/cookies.rs +++ b/rust/bitbazaar/cookies/cookies.rs @@ -80,33 +80,39 @@ pub fn set_cookie_raw(name: &str, value: &str, options: CookieOptions<'_>) { use crate::prelude::*; - let axum_response = leptos::expect_context::(); - let mut cookie = Cookie::build((name, value)).http_only(options.http_only); - if let Some(path) = options.path { - cookie = cookie.path(path); - } - if let Some(domain) = options.domain { - cookie = cookie.domain(domain); - } - if let Some(expires) = options.expires { - cookie = cookie.max_age(time::Duration::milliseconds(expires.num_milliseconds())); - } - if options.secure { - cookie = cookie.secure(true); - } - cookie = match options.same_site { - SameSite::Lax => cookie.same_site(axum_extra::extract::cookie::SameSite::Lax), - SameSite::Strict => cookie.same_site(axum_extra::extract::cookie::SameSite::Strict), - SameSite::None => cookie.same_site(axum_extra::extract::cookie::SameSite::None), - }; - - match http::HeaderValue::from_str(&cookie.to_string()).change_context(AnyErr) { - Ok(cookie) => { - axum_response.append_header(http::header::SET_COOKIE, cookie); + if let Some(resp_opts) = leptos::use_context::() { + let mut cookie = Cookie::build((name, value)).http_only(options.http_only); + if let Some(path) = options.path { + cookie = cookie.path(path); } - Err(e) => { - record_exception("Failed to set cookie.", format!("{:?}", e)); + if let Some(domain) = options.domain { + cookie = cookie.domain(domain); + } + if let Some(expires) = options.expires { + cookie = cookie.max_age(time::Duration::milliseconds(expires.num_milliseconds())); } + if options.secure { + cookie = cookie.secure(true); + } + cookie = match options.same_site { + SameSite::Lax => cookie.same_site(axum_extra::extract::cookie::SameSite::Lax), + SameSite::Strict => cookie.same_site(axum_extra::extract::cookie::SameSite::Strict), + SameSite::None => cookie.same_site(axum_extra::extract::cookie::SameSite::None), + }; + + match http::HeaderValue::from_str(&cookie.to_string()).change_context(AnyErr) { + Ok(cookie) => { + resp_opts.append_header(http::header::SET_COOKIE, cookie); + } + Err(e) => { + record_exception("Failed to set cookie.", format!("{:?}", e)); + } + } + } else { + record_exception( + "Couldn't get response options from leptos_axum::ResponseOptions context.", + "", + ); } } }