7 releases

0.2.3 Mar 3, 2025
0.2.2 Feb 12, 2025
0.1.1 Jan 29, 2025

#3 in #web-cookies

Download history 120/week @ 2025-01-01 147/week @ 2025-01-08 13/week @ 2025-01-15 245/week @ 2025-01-29 167/week @ 2025-02-05 186/week @ 2025-02-12 37/week @ 2025-02-19 139/week @ 2025-02-26 68/week @ 2025-03-05 15/week @ 2025-03-12

278 downloads per month

MIT license

11KB
142 lines

Axum Cookie Middleware

This library provides a middleware layer for integrating cookie management into Axum applications. It allows parsing cookies from incoming requests, managing cookies and setting Set-Cookie headers in HTTP responses.

Features

  • Middleware integration with Axum.
  • Flexible cookie parsing (strict or lenient mode).
  • Thread-safe cookie management.
  • Automatic handling of Set-Cookie headers.

Usage

Add the library to your Cargo.toml:

[dependencies]
...
+ axum-cookie = "0.2.3"

Example: Basic Integration

use axum::{Router, routing::get};
use axum_cookie::prelude::*;

async fn handler(cookie: CookieManager) -> &'static str {
    // Retrieve a cookie
    if let Some(cookie) = cookie.get("session") {
        println!("Cookie value: {}", cookie.value());
    }

    // Add a cookie
    cookie.add(Cookie::new("session", "abc123"));
    "Hello, cookies!"
}

let app: Router<()> = Router::new()
    .route("/", get(handler))
    .layer(CookieLayer::default());
use axum::{Router, routing::get};
use axum_cookie::CookieLayer;

let app: Router<()> = Router::new()
    .route("/", get(|| async { "Strict mode enabled" }))
    .layer(CookieLayer::strict());

API

CookieManager

  • CookieManager::new - Creates a new cookie manager with a specified CookieJar.
  • CookieManager::add - Adds a cookie to the jar.
  • CookieManager::remove - Removes a cookie by its name.
  • CookieManager::get - Retrieves a cookie by its name.
  • CookieManager::cookie - Returns all cookies in the jar.
  • CookieManager::as_header_value - Generates Set-Cookie header value for all cookies in the jar.

CookieLayer

  • CookieLayer::default - Creates a layer with lenient cookie parsing.
  • CookieLayer::strict - Creates a layer with strict cookie parsing.

CookieMiddleware

  • Handles parsing cookies from requests.
  • Adds Set-Cookie headers to responses.

Contributing

Contributions are welcome! Please open an issue or submit a pull request on GitHub.

License

This project is licensed under the MIT License.

Dependencies

~2MB
~30K SLoC