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
SplitCookiesiterator - 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-encodefeature for proper handling of percent-encoded cookie values
Installation
Add this to your Cargo.toml:
[]
= "0.1"
= "0.18"
It's recommended to enable the percent-encode feature:
[]
= { = "0.1", = ["percent-encode"] }
= "0.18"
Usage
Basic Usage
use CookieHeaderStringExt;
use Cookie;
let cookie_header = "name=value; name2=value2; name3=value3";
let cookies: = header_string_parse
.filter_map
.collect;
assert_eq!;
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 CookieHeaderStringExt;
use Cookie;
// Semicolon inside unquoted value is preserved correctly
let cookie_header = "session=abc;123; other=value";
let cookies: = header_string_parse
.filter_map
.collect;
assert_eq!;
assert_eq!;
assert_eq!;
Error Handling
Since parsing returns Result<Cookie, ParseError>, you can handle errors gracefully:
use CookieHeaderStringExt;
use Cookie;
let cookie_header = "valid=value; invalid";
for result in header_string_parse