set-cookie-parser 0.1.0

Parse Set-Cookie response headers into structured cookies, and split a comma-joined Set-Cookie header without choking on commas in Expires dates. A faithful port of the set-cookie-parser npm package. Zero dependencies, no_std.
Documentation
  • Coverage
  • 100%
    18 out of 18 items documented4 out of 7 items with examples
  • Size
  • Source code size: 35.47 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 243.88 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 2s Average build duration of successful builds.
  • all releases: 2s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • trananhtung/set-cookie-parser
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • trananhtung

set-cookie-parser

Crates.io Documentation CI License

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 set_cookie_parser::{parse, parse_all, split_cookies_string};

let c = parse("sid=abc123; Path=/; HttpOnly; SameSite=Lax").unwrap();
assert_eq!(c.name, "sid");
assert_eq!(c.value, "abc123");
assert_eq!(c.path.as_deref(), Some("/"));
assert!(c.http_only);

// 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!(parse_all(header).len(), 2);

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.

[dependencies]
set-cookie-parser = "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. Pass decode_values = false to disable.
  • expires is 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::other as (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.

License

Licensed under either of Apache-2.0 or MIT at your option.