ri-cookie-header-string 0.1.0

A library for parsing HTTP Cookie header strings into structured cookie objects.
Documentation
  • Coverage
  • 50%
    2 out of 4 items documented1 out of 4 items with examples
  • Size
  • Source code size: 23.25 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 2.35 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 26s Average build duration of successful builds.
  • all releases: 29s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Repository
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • FahriDevZ

ri-cookie-header-string

A Rust library for parsing HTTP Cookie header strings into structured cookie objects.

This crate provides an extension trait for the [cookie] crate that enables advanced parsing of cookie header strings (as received in HTTP Cookie headers) into a collection of [Cookie] objects.

⚠️ Note: This is a non-standard, security-focused parser. It provides advanced heuristics for handling unquoted cookie values that may contain semicolons, useful for real-world edge cases in non-standard cookie implementations. For standard RFC 6265-compliant parsing, use the built-in SplitCookies iterator from the cookie crate instead.

Features

  • Smart semicolon handling: Distinguishes between semicolons that separate cookies and semicolons that are part of cookie values, providing more accurate parsing than the standard SplitCookies iterator
  • Iterator-based parsing: Lazy parsing that returns an iterator over parsed cookies
  • Error handling: Returns Result<Cookie, ParseError> for each cookie, allowing graceful handling of malformed entries
  • Percent-encoding support: Recommended to enable the percent-encode feature for proper handling of percent-encoded cookie values

Installation

Add this to your Cargo.toml:

[dependencies]
ri-cookie-header-string = "0.1"
cookie = "0.18"

It's recommended to enable the percent-encode feature:

[dependencies]
ri-cookie-header-string = { version = "0.1", features = ["percent-encode"] }
cookie = "0.18"

Usage

Basic Usage

use ri_cookie_header_string::CookieHeaderStringExt;
use cookie::Cookie;

let cookie_header = "name=value; name2=value2; name3=value3";
let cookies: Vec<_> = Cookie::header_string_parse(cookie_header)
    .filter_map(|result| result.ok())
    .collect();

assert_eq!(cookies.len(), 3);

Handling Semicolons in Cookie Values

The library intelligently handles semicolons that appear within cookie values, providing more accurate parsing than the built-in SplitCookies iterator for non-standard cookie implementations:

use ri_cookie_header_string::CookieHeaderStringExt;
use cookie::Cookie;

// Semicolon inside unquoted value is preserved correctly
let cookie_header = "session=abc;123; other=value";
let cookies: Vec<_> = Cookie::header_string_parse(cookie_header)
    .filter_map(|result| result.ok())
    .collect();

assert_eq!(cookies.len(), 2);
assert_eq!(cookies[0].value(), "abc;123");
assert_eq!(cookies[1].value(), "value");

Error Handling

Since parsing returns Result<Cookie, ParseError>, you can handle errors gracefully:

use ri_cookie_header_string::CookieHeaderStringExt;
use cookie::Cookie;

let cookie_header = "valid=value; invalid";
for result in Cookie::header_string_parse(cookie_header) {
    match result {
        Ok(cookie) => println!("Parsed: {}={}", cookie.name(), cookie.value()),
        Err(e) => eprintln!("Parse error: {:?}", e),
    }
}