Expand description
A library for parsing HTTP Cookie header strings into structured cookie objects.
This crate provides extension traits for parsing cookie header strings into structured cookie objects.
It supports multiple cookie implementations including the cookie crate and optionally [reqwest].
Note: This is a non-standard, security-focused parser. Unlike the standard SplitCookies iterator
and RFC 6265 compliance, this library provides smarter parsing for unquoted cookie values that may contain
semicolons. This is useful for handling cookie values that aren’t properly quoted or encoded in non-standard
cookie implementations, providing additional safety when parsing untrusted cookie headers.
§Features
- Advanced semicolon handling: Distinguishes between semicolons that are cookie separators and semicolons that appear within unquoted cookie values
- Iterator-based parsing: Lazy evaluation returns an iterator over parsed cookies
- Error handling: Returns
Result<Cookie, ParseError>for each cookie, allowing graceful handling of malformed entries - Percent-encoding support: Enable the
percent-encodefeature to decode percent-encoded cookie values (e.g.,%20for space) - Multiple cookie implementations: Support for
cookiecrate and optionallyreqwestvia feature flag
§When to Use This Library
Use this library when:
- Parsing non-standard cookie headers with unquoted values containing semicolons
- You need safety when handling untrusted cookie input with unusual formatting
- Your application requires advanced heuristics to detect cookie boundaries
- You work with different cookie implementations across your project
Note: For standard RFC 6265-compliant cookie parsing, the built-in cookie crate
provides SplitCookies which is more performant and spec-compliant.
§Usage
Add this to your Cargo.toml:
[dependencies]
ri-cookie-header-string = "0.2"
cookie = "0.18"It’s recommended to enable the percent-encode feature:
[dependencies]
ri-cookie-header-string = { version = "0.2", features = ["percent-encode"] }
cookie = "0.18"For reqwest support, enable the reqwest feature:
[dependencies]
ri-cookie-header-string = { version = "0.2", features = ["reqwest"] }
reqwest = { version = "0.12", features = ["cookies"] }§Examples
Basic usage with cookie crate:
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 unquoted cookie values:
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");Structs§
- Header
String Cookies - Iterator over cookies in a header string.
Traits§
- Cookie
Builder - Internal trait for abstracting cookie construction across different cookie implementations.
- Cookie
Header String Ext