Crate cookie_monster

Crate cookie_monster 

Source
Expand description

An HTTP Cookie crate.

§Overview

Exposes types like Cookie and CookieJar for working with HTTP cookies. This crate focuses on server side applications. The main goals are simplicity and ease of use.

§Usage

Add cookie-monster in your Cargo.toml:

[dependencies]
cookie-monster = "0.1"

§Features

  • jiff

    Adds support for the jiff crate. This exposes methods to Cookie to retrieve the Expires and Max-Age attributes with jiff specific types.

  • chrono

    Adds support for the chrono crate. This exposes methods to Cookie to retrieve the Expires and Max-Age attributes with chrono specific types.

  • time

    Adds support for the time crate. This exposes methods to Cookie to retrieve the Expires and Max-Age attributes with time specific types.

  • percent-encode

    Parse/serialize Cookies that are percent-encoded.

  • axum

    Adds integration with the axum crate.
    Implements IntoResponse and IntoResponseParts for Cookie and CookieJar.
    Implements FromRequestParts only for CookieJar

  • http

    Adds integration with the http crate. Create a CookieJar from a HeaderMap. Write a CookieJar to a HeaderMap.

§Axum example

use axum::response::IntoResponse;
use cookie_monster::{Cookie, CookieJar, SameSite};

static COOKIE_NAME: &str = "session";

async fn handler(mut jar: CookieJar) -> impl IntoResponse {
    if let Some(cookie) = jar.get(COOKIE_NAME) {
        // Remove cookie
        println!("Removing cookie {cookie:?}");
        jar.remove(Cookie::named(COOKIE_NAME));
    } else {
        // Set cookie.
        let cookie = Cookie::build(COOKIE_NAME, "hello, world")
        .http_only()
        .same_site(SameSite::Strict);

        println!("Setting cookie {cookie:?}");
        jar.add(cookie);
    }
    // Return the jar so the cookies are updated
   jar
}

§Honorable mention

This crate takes a lot of inspiration from the cookie crate.

Structs§

Cookie
An HTTP Cookie.
CookieBuilder
A builder struct for building a Cookie.
CookieJar
A generic CookieJar for cookie management. Can be used to read update or delete cookies from a user session.

Enums§

Error
All errors that can be returned while parsing or serializing cookies.
Expires
The Expires attribute.
SameSite
The SameSite attribute.