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

Add spin feature for use_cookie #99

Merged
merged 1 commit into from
Apr 19, 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ lazy_static = "1"
leptos = "0.6"
leptos_axum = { version = "0.6", optional = true }
leptos_actix = { version = "0.6", optional = true }
leptos-spin = { version = "0.1", optional = true }
num = { version = "0.4", optional = true }
paste = "1"
prost = { version = "0.12", optional = true }
Expand Down Expand Up @@ -142,6 +143,7 @@ docs = []
math = ["num"]
prost = ["base64", "dep:prost"]
serde = ["dep:serde", "serde_json"]
spin = ["dep:leptos-spin", "dep:http1"]
ssr = []
msgpack = ["dep:rmp-serde", "dep:serde"]

Expand Down
73 changes: 56 additions & 17 deletions src/use_cookie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,17 +485,23 @@ impl<T, Err> Default for UseCookieOptions<T, Err> {
#[cfg(all(feature = "actix", feature = "axum"))]
compile_error!("You cannot enable only one of features \"actix\" and \"axum\" at the same time");

#[cfg(all(feature = "actix", feature = "spin"))]
compile_error!("You cannot enable only one of features \"actix\" and \"spin\" at the same time");

#[cfg(all(feature = "axum", feature = "spin"))]
compile_error!("You cannot enable only one of features \"axum\" and \"spin\" at the same time");

#[cfg(feature = "actix")]
const COOKIE: http0_2::HeaderName = http0_2::header::COOKIE;
#[cfg(feature = "axum")]
#[cfg(any(feature = "axum", feature = "spin"))]
const COOKIE: http1::HeaderName = http1::header::COOKIE;

#[cfg(feature = "actix")]
type HeaderValue = http0_2::HeaderValue;
#[cfg(feature = "axum")]
type HeaderValue = http1::HeaderValue;

#[cfg(any(feature = "axum", feature = "actix"))]
#[cfg(any(feature = "axum", feature = "actix", feature = "spin"))]
let headers;
#[cfg(feature = "actix")]
{
Expand All @@ -506,23 +512,43 @@ impl<T, Err> Default for UseCookieOptions<T, Err> {
{
headers = use_context::<http1::request::Parts>().map(|parts| parts.headers);
}
#[cfg(feature = "spin")]
{
headers = use_context::<leptos_spin::RequestParts>()
.map(|parts| parts.headers().clone());
}

#[cfg(all(not(feature = "axum"), not(feature = "actix")))]
#[cfg(all(
not(feature = "axum"),
not(feature = "actix"),
not(feature = "spin")
))]
{
leptos::logging::warn!("If you're using use_cookie without the feature `axum` or `actix` enabled, you should provide the option `ssr_cookies_header_getter`");
leptos::logging::warn!("If you're using use_cookie without the feature `axum`, `actix` or `spin` enabled, you should provide the option `ssr_cookies_header_getter`");
None
}

#[cfg(any(feature = "axum", feature = "actix"))]
headers.map(|headers| {
headers
.get(COOKIE)
.cloned()
.unwrap_or_else(|| HeaderValue::from_static(""))
.to_str()
.unwrap_or_default()
.to_owned()
})
{
headers.map(|headers| {
headers
.get(COOKIE)
.cloned()
.unwrap_or_else(|| HeaderValue::from_static(""))
.to_str()
.unwrap_or_default()
.to_owned()
})
}
#[cfg(feature = "spin")]
{
headers.and_then(|headers| {
headers
.iter()
.find(|(key, _)| **key == COOKIE)
.and_then(|(_, value)| String::from_utf8(value.to_vec()).ok())
})
}
}
#[cfg(not(feature = "ssr"))]
None
Expand All @@ -534,21 +560,27 @@ impl<T, Err> Default for UseCookieOptions<T, Err> {
use leptos_actix::ResponseOptions;
#[cfg(feature = "axum")]
use leptos_axum::ResponseOptions;
#[cfg(feature = "spin")]
use leptos_spin::ResponseOptions;

#[cfg(feature = "actix")]
const SET_COOKIE: http0_2::HeaderName = http0_2::header::SET_COOKIE;
#[cfg(feature = "axum")]
#[cfg(any(feature = "axum", feature = "spin"))]
const SET_COOKIE: http1::HeaderName = http1::header::SET_COOKIE;

#[cfg(feature = "actix")]
type HeaderValue = http0_2::HeaderValue;
#[cfg(feature = "axum")]
#[cfg(any(feature = "axum", feature = "spin"))]
type HeaderValue = http1::HeaderValue;

#[cfg(all(not(feature = "axum"), not(feature = "actix")))]
#[cfg(all(
not(feature = "axum"),
not(feature = "actix"),
not(feature = "spin")
))]
{
let _ = cookie;
leptos::logging::warn!("If you're using use_cookie without the feature `axum` or `actix` enabled, you should provide the option `ssr_set_cookie`");
leptos::logging::warn!("If you're using use_cookie without the feature `axum`, `actix` or `spin` enabled, you should provide the option `ssr_set_cookie`");
}

#[cfg(any(feature = "axum", feature = "actix"))]
Expand All @@ -561,6 +593,13 @@ impl<T, Err> Default for UseCookieOptions<T, Err> {
}
}
}
#[cfg(feature = "spin")]
{
if let Some(response_options) = use_context::<ResponseOptions>() {
let header_value = cookie.encoded().to_string().as_bytes().to_vec();
response_options.insert_header(SET_COOKIE.as_str(), header_value);
}
}
}

let _ = cookie;
Expand Down
Loading