Skip to content

joeydewaal/cookie-monster

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

80 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cookie-monster

CI/CD Current Crates.io Version Documentation

A Cookie library for parsing and managing HTTP Cookies.

Features

  • jiff, adds support for the jiff crate.
  • chrono, adds support for the chrono crate.
  • time, adds support for the time crate.
  • percent-encode, percent-encode/decode cookies.
  • axum, adds integration with the axum crate.
  • http, adds integration with the http crate.

Install

# Cargo.toml
[dependencies]
cookie-monster = "0.1"

# Integration with the `time` crate
cookie-monster = { version = "0.1", features = ["time"] }
# Integration with the`chrono` crate
cookie-monster = { version = "0.1", features = ["chrono"] }
# Integration with the `jiff` crate
cookie-monster = { version = "0.1", features = ["jiff"] }

# Adds support for percent-encoding/decoding cookies.
cookie-monster = { version = "0.1", features = ["percent-encoding"] }

# Integration with the `axum` crate.
cookie-monster = { version = "0.1", features = ["axum"] }

# Integration with the `http` crate.
cookie-monster = { version = "0.1", features = ["http"] }

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
}

Minimum Supported Rust Version (MSRV)

The cookie-monster crate has rust version 1.85 as MSRV.

Honorable mention

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

License

This project is licensed under the MIT license.

About

A Cookie library for parsing and managing HTTP Cookies.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages