Skip to content

joeydewaal/cookie-monster

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

87 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
}

Prefix cookies

Cookie::host and Cookie::secure build cookies using the __Host- and __Secure- name prefixes (RFC 6265bis §4.1.3). They set the attributes the prefix requires as defaults (which you can override to build non-standard cookies) and apply the prefix to the name on serialization. Parsing strips a recognized prefix from the name, so a prefixed cookie is looked up in a CookieJar by its logical (unprefixed) name.

use cookie_monster::Cookie;

let cookie = Cookie::host("id", "abc").build();
assert_eq!(cookie.name(), "id");
assert_eq!(cookie.serialize().as_deref(), Ok("__Host-id=abc; Path=/; Secure"));

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

11 stars

Watchers

1 watching

Forks

Packages

 
 
 

Contributors

Languages