set-cookie-parser
Parse Set-Cookie response headers into structured cookies, and split a
comma-joined Set-Cookie string into individual cookies without choking on the
commas inside an Expires date. A faithful Rust port of the
set-cookie-parser npm package.
Zero dependencies and #![no_std].
use ;
let c = parse.unwrap;
assert_eq!;
assert_eq!;
assert_eq!;
assert!;
// One combined header with two cookies (note the comma inside Expires):
let header = "a=1; Expires=Wed, 09 Jun 2021 10:18:14 GMT, b=2";
assert_eq!;
Why set-cookie-parser?
Servers and proxies sometimes combine multiple Set-Cookie field values into one
comma-joined string — which is awkward to split because Expires dates contain
commas too (Wed, 09 Jun 2021 …). This ports the canonical JS algorithm that splits
correctly, and parses each cookie's name, value, and attributes.
[]
= "0.1"
API
| Item | Purpose |
|---|---|
parse(set_cookie) |
Parse one Set-Cookie value into a Cookie |
parse_with(set_cookie, decode_values) |
…controlling URI-decoding of the value |
split_cookies_string(combined) |
Split a comma-joined header into individual strings |
parse_all(combined) |
Split then parse each cookie |
Cookie |
name, value, expires, max_age, domain, path, secure, http_only, same_site, partitioned, other |
Notes
- Cookie values are URI-decoded by default (
decodeURIComponent); a malformed escape keeps the raw value. Passdecode_values = falseto disable. expiresis kept as the raw header string — parse it with a date crate (e.g.httpdate,time,chrono) if you need a timestamp.- Attributes beyond the well-known ones are collected into
Cookie::otheras(lower-cased key, value)pairs. Keys are lower-cased with Rust's Unicode lowercasing; for non-ASCII custom keys this can differ from JavaScript on context-sensitive cases (e.g. Greek final sigma). Standard ASCII attributes (Path,Secure, …) are unaffected.
Contributors ✨
This project follows the all-contributors specification. Contributions of any kind are welcome — code, docs, bug reports, ideas, reviews! See the emoji key for how each contribution is recognized, and open a PR or issue to get involved.
Thanks goes to these wonderful people:
License
Licensed under either of Apache-2.0 or MIT at your option.