Struct poem::web::cookie::CookieJar [−][src]
pub struct CookieJar { /* fields omitted */ }
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
Adds cookie to this jar. If a cookie with the same name already exists, it is replaced with cookie.
Returns a reference to the Cookie
inside this jar with the name
.
If no such cookie exists, returns None
.
Removes all delta cookies.
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());
Similar to the private_with_key
function, but using the key specified
by the CookieJarManager::with_key
.
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());
Similar to the signed_with_key
function, but using the key specified
by the CookieJarManager::with_key
.
Trait Implementations
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,
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,
Perform the extraction.
Auto Trait Implementations
impl !RefUnwindSafe for CookieJar
impl !UnwindSafe for CookieJar
Blanket Implementations
Mutably borrows from an owned value. Read more
pub fn vzip(self) -> V
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more