pub struct CookieJar { /* private fields */ }
Available 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,
    test::TestClient,
    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 cli = TestClient::new(app);

let resp = cli.get("/").send().await;
resp.assert_status_is_ok();
let cookie = resp.0.headers().get(header::SET_COOKIE).cloned().unwrap();
resp.assert_text("count: 1").await;

let resp = cli.get("/").header(header::COOKIE, cookie).send().await;
resp.assert_status_is_ok();
resp.assert_text("count: 2").await;

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.

Wraps an iterator over all of the cookies present in this jar with a closure.

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

Returns the argument unchanged.

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

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
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