Crate cookie_parser

Source
Expand description

A Rust library for parsing Cookie and Set-Cookie HTTP headers with support for all standard cookie attributes.

§Features

  • Parse Cookie header strings into structured data
  • Parse Set-Cookie header strings with full attribute support
  • Support for cookie extensions

§Usage

Install using cargo:

cargo add cookie_parser
use cookie_parser::{parse_cookie_string, CookiePair};

fn main() {
    let cookies = parse_cookie_string("session=abc123; user=john_doe")?;
    
    for cookie in cookies {
        println!("Name: {}, Value: {}", cookie.name, cookie.value);
    }
}
use cookie_parser::{parse_set_cookie, SetCookie};

fn main() {
    let set_cookie = parse_set_cookie(
        "session=abc123; HttpOnly; Secure; Path=/; Domain=example.com; Max-Age=3600"
    )?;
    
    println!("Cookie name: {}", set_cookie.pair.name);
    println!("Cookie value: {}", set_cookie.pair.value);
    println!("Is secure: {}", set_cookie.secure);
    println!("Is HTTP-only: {}", set_cookie.http_only);
    
    if let Some(domain) = set_cookie.domain {
        println!("Domain: {}", domain);
    }
}

§API Reference

§Structures

§CookiePair

Represents a name-value pair in a cookie.

pub struct CookiePair {
    pub name: String,
    pub value: String,
}
§SetCookie

Represents a complete Set-Cookie header with all possible attributes.

pub struct SetCookie {
    pub pair: CookiePair,
    pub secure: bool,
    pub http_only: bool,
    pub max_age: Option<String>,
    pub domain: Option<String>,
    pub expires: Option<String>,
    pub path: Option<String>,
    pub extensions: Vec<String>,
}

§Functions

pub fn parse_cookie_string(input: &str) -> Result<Vec<CookiePair>, CookieParseError>

Parses a Cookie header string into a vector of cookie pairs.

pub fn parse_set_cookie(input: &str) -> Result<SetCookie, CookieParseError>

Parses a Set-Cookie header string into a structured SetCookie object.

§Errors

The library uses a custom error type CookieParseError with the following variants:

  • ErrorCookieStringSyntax: Invalid cookie string syntax
  • ErrorCookieStringEmpty: Cookie string is empty

§License

MIT

Structs§

CookiePair
SetCookie
A structure that represents a set-cookie header contents and its attributes

Enums§

CookieParseError
An error that can occur when parsing a cookie/set-cookie string.
Rule

Functions§

parse_cookie_string
Parse a cookie string into a vector of cookie pairs.
parse_set_cookie
Parse a set-cookie string into a set-cookie structure.