logo
pub struct CookieJar { /* private fields */ }
This is supported on crate feature 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.

Removes cookie from this jar.

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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Returns the “default value” for a type. Read more

Extract from request head and body.

Extract from request head. Read more

The associated error which can be returned from parsing.

Parses a string s to return a value of this type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Attaches the provided Context to this type, returning a WithContext wrapper. Read more

Attaches the current Context to this type, returning a WithContext wrapper. Read more

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Performs the conversion.

Should always be Self

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

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