Skip to main content

Crate set_cookie_parser

Crate set_cookie_parser 

Source
Expand description

Parse a Set-Cookie header value into a structured Cookie, 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);
assert_eq!(c.same_site.as_deref(), Some("Lax"));

// A single 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!(split_cookies_string(header), ["a=1; Expires=Wed, 09 Jun 2021 10:18:14 GMT", "b=2"]);
assert_eq!(parse_all(header).len(), 2);

Structs§

Cookie
A parsed cookie from a Set-Cookie header.

Functions§

parse
Parse a single Set-Cookie header value into a Cookie, URI-decoding the value.
parse_all
Split a combined Set-Cookie header and parse each cookie (URI-decoding values).
parse_all_with
Split a combined Set-Cookie header and parse each cookie, controlling URI-decoding.
parse_with
Parse a single Set-Cookie header value, controlling whether the cookie value is URI-decoded (decodeURIComponent). On a decode error the raw value is kept.
split_cookies_string
Split a combined Set-Cookie header string into individual cookie strings.