CookieJar

Struct CookieJar 

Source
pub struct CookieJar { /* private fields */ }
Expand description

A generic CookieJar for cookie management. Can be used to read update or delete cookies from a user session.

§axum feature

Note that to set the cookies, the jar must be returned from the handler. Otherwise the cookies are not updated.

§Example

use cookie_monster::{CookieJar, Cookie};

static COOKIE_NAME: &str = "session";

async fn handler(mut jar: CookieJar) -> CookieJar {

    if let Some(cookie) = jar.get(COOKIE_NAME) {
        println!("Removing cookie {cookie:?}");
        jar.remove(Cookie::named(COOKIE_NAME));
    } else {
        let cookie = Cookie::new(COOKIE_NAME, "hello, world");
        println!("Setting cookie {cookie:?}");
        jar.add(cookie);
    }

    // Important, return the jar to update the cookies!
    jar
}

Implementations§

Source§

impl CookieJar

Source

pub fn empty() -> Self

Creates an empty CookieJar.

Parses the given cookie header value and return a CookieJar. This function ignores cookies that were not able to be parsed.

Parses the given cookie header value and return a CookieJar. The cookie name and values are percent-decoded. Cookies that were not able to be parsed are ignored.

Source

pub fn add_original(&mut self, cookie: Cookie)

Adds an original cookie to the jar. These are never sent back to the user-agent, but are visible in the cookie jar.

Source

pub fn from_original<T: IntoIterator<Item = Cookie>>(cookies: T) -> Self

Source

pub fn get(&self, name: &str) -> Option<&Cookie>

Get a cookie by name. Gives back either an original or newly added cookie.

Iterate over all changes. This returns all removed and newly created cookies.

Source

pub fn remove(&mut self, cookie: impl Into<Cookie>)

Removes the cookie from the local cookie store and issues a cookie with an Expires attribute in the past and Max-Age of 0 seconds.

If one of the time, chrono or jiff features are enabled, the Expires tag is set to the current time minus one year. If none of the those features are enabled, the Expires attribute is set to 1 Jan 1970 00:00.

To ensure a cookie is removed from the user-agent, set the Path and Domain attributes with the same values that were used to create the cookie.

Source

pub fn add(&mut self, cookie: impl Into<Cookie>)

Adds a cookie to the jar. If a cookie with the same name is already in the jar, it is replaced with the given cookie.

Source§

impl CookieJar

Source

pub fn from_headers(headers: &HeaderMap) -> Self

Source

pub fn write_cookies(self, headers: &mut HeaderMap)

Trait Implementations§

Source§

impl Debug for CookieJar

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for CookieJar

Source§

fn default() -> CookieJar

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

impl<S> FromRequestParts<S> for CookieJar
where S: Send + Sync,

Source§

type Rejection = Infallible

If the extractor fails it’ll use this “rejection” type. A rejection is a kind of error that can be converted into a response.
Source§

async fn from_request_parts( parts: &mut Parts, _: &S, ) -> Result<Self, Self::Rejection>

Perform the extraction.
Source§

impl IntoResponse for CookieJar

Source§

fn into_response(self) -> Response

Create a response.
Source§

impl IntoResponseParts for CookieJar

Source§

type Error = Infallible

The type returned in the event of an error. Read more
Source§

fn into_response_parts( self, res: ResponseParts, ) -> Result<ResponseParts, Self::Error>

Set parts of the response

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<S, T> FromRequest<S, ViaParts> for T
where S: Send + Sync, T: FromRequestParts<S>,

Source§

type Rejection = <T as FromRequestParts<S>>::Rejection

If the extractor fails it’ll use this “rejection” type. A rejection is a kind of error that can be converted into a response.
Source§

fn from_request( req: Request<Body>, state: &S, ) -> impl Future<Output = Result<T, <T as FromRequest<S, ViaParts>>::Rejection>>

Perform the extraction.
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.