Expand description
A 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. 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)
§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
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.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"§Examples
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 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.