pub struct CookieJar { /* private fields */ }
cookie
only.Expand description
A collection of cookies that tracks its modifications.
Example
use poem::{
get, handler,
http::{header, StatusCode},
middleware::CookieJarManager,
web::cookie::{Cookie, CookieJar},
Endpoint, EndpointExt, Request, Route,
};
#[handler]
fn index(cookie_jar: &CookieJar) -> String {
let count = match cookie_jar.get("count") {
Some(cookie) => cookie.value::<i32>().unwrap() + 1,
None => 1,
};
cookie_jar.add(Cookie::new("count", count));
format!("count: {}", count)
}
let app = Route::new()
.at("/", get(index))
.with(CookieJarManager::new());
let resp = app.call(Request::default()).await.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let cookie = resp.headers().get(header::SET_COOKIE).cloned().unwrap();
assert_eq!(resp.into_body().into_string().await.unwrap(), "count: 1");
let resp = app
.call(Request::builder().header(header::COOKIE, cookie).finish())
.await
.unwrap();
assert_eq!(resp.into_body().into_string().await.unwrap(), "count: 2");
Implementations
sourceimpl CookieJar
impl CookieJar
sourcepub fn add(&self, cookie: Cookie)
pub fn add(&self, cookie: Cookie)
Adds cookie to this jar. If a cookie with the same name already exists, it is replaced with cookie.
sourcepub fn get(&self, name: &str) -> Option<Cookie>
pub fn get(&self, name: &str) -> Option<Cookie>
Returns a reference to the Cookie
inside this jar with the name
.
If no such cookie exists, returns None
.
sourcepub fn reset_delta(&self)
pub fn reset_delta(&self)
Removes all delta cookies.
sourcepub fn private_with_key<'a>(
&'a self,
key: &'a CookieKey
) -> PrivateCookieJar<'a>
pub fn private_with_key<'a>(
&'a self,
key: &'a CookieKey
) -> PrivateCookieJar<'a>
Returns a PrivateJar with self as its parent jar using the key to encrypt and decrypt cookies added/retrieved from the child jar.
Example
use poem::web::cookie::{Cookie, CookieJar, CookieKey};
let key = CookieKey::generate();
let cookie_jar = CookieJar::default();
cookie_jar
.private_with_key(&key)
.add(Cookie::new_with_str("foo", "bar"));
assert_ne!(cookie_jar.get("foo").unwrap().value_str(), "bar");
assert_eq!(
cookie_jar
.private_with_key(&key)
.get("foo")
.unwrap()
.value_str(),
"bar"
);
let key2 = CookieKey::generate();
assert!(cookie_jar.private_with_key(&key2).get("foo").is_none());
sourcepub fn private(&self) -> PrivateCookieJar<'_>
pub fn private(&self) -> PrivateCookieJar<'_>
Similar to the private_with_key
function, but using the key specified
by the CookieJarManager::with_key
.
sourcepub fn signed_with_key<'a>(&'a self, key: &'a CookieKey) -> SignedCookieJar<'a>
pub fn signed_with_key<'a>(&'a self, key: &'a CookieKey) -> SignedCookieJar<'a>
Returns a SignedJar with self as its parent jar using the key key to verify cookies retrieved from the child jar. Any retrievals from the child jar will be made from the parent jar.
Example
use poem::web::cookie::{Cookie, CookieJar, CookieKey};
let key = CookieKey::generate();
let cookie_jar = CookieJar::default();
cookie_jar
.signed_with_key(&key)
.add(Cookie::new_with_str("foo", "bar"));
assert!(cookie_jar.get("foo").unwrap().value_str().contains("bar"));
assert_eq!(
cookie_jar
.signed_with_key(&key)
.get("foo")
.unwrap()
.value_str(),
"bar"
);
let key2 = CookieKey::generate();
assert!(cookie_jar.signed_with_key(&key2).get("foo").is_none());
sourcepub fn signed(&self) -> SignedCookieJar<'_>
pub fn signed(&self) -> SignedCookieJar<'_>
Similar to the signed_with_key
function, but using the key specified
by the CookieJarManager::with_key
.
Trait Implementations
sourceimpl<'a> FromRequest<'a> for &'a CookieJar
impl<'a> FromRequest<'a> for &'a CookieJar
sourcefn from_request<'life0, 'async_trait>(
req: &'a Request,
_body: &'life0 mut RequestBody
) -> Pin<Box<dyn Future<Output = Result<Self>> + Send + 'async_trait>> where
'a: 'async_trait,
'life0: 'async_trait,
Self: 'async_trait,
fn from_request<'life0, 'async_trait>(
req: &'a Request,
_body: &'life0 mut RequestBody
) -> Pin<Box<dyn Future<Output = Result<Self>> + Send + 'async_trait>> where
'a: 'async_trait,
'life0: 'async_trait,
Self: 'async_trait,
Extract from request head and body.
Auto Trait Implementations
impl !RefUnwindSafe for CookieJar
impl Send for CookieJar
impl Sync for CookieJar
impl Unpin for CookieJar
impl !UnwindSafe for CookieJar
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> FutureExt for T
impl<T> FutureExt for T
sourcefn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
sourcefn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more