
Cookie Library
cookie-rs is a flexible library for working with HTTP cookies. It allows you to create, parse, and manage cookies.
Features
- Create cookies with various attributes (e.g.,
Domain,Path,Secure,HttpOnly). - Parse cookies from HTTP headers in lenient or strict mode.
- Manage cookies using
CookieJar, which tracks additions and removals. - Support for
SameSiteattribute. - Automatic percent-encoding and decoding of cookie values (enabled by default).
- Errors are handled gracefully through
ParseError.
Quick Start
To use this library, add it to your dependencies:
[dependencies]
...
+ cookie-rs = "0.5.0"
Create a Cookie
use *;
let cookie = builder
.domain
.path
.secure
.http_only
.same_site
.build;
println!;
Output:
session=abc123; Domain=example.com; HttpOnly; Path=/; SameSite=Lax; Secure
Parse a Cookie
use Cookie;
let cookie = parse.unwrap;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
Percent-Encoding
Cookie values are automatically percent-encoded when serialized and decoded when parsed. This behavior is enabled by default and can be disabled via Cargo features.
use *;
let cookie = new;
assert_eq!;
let parsed = parse.unwrap;
assert_eq!;
To disable encoding:
[]
= { = "0.5.0", = false }
Manage Cookies with CookieJar
use ;
let mut jar = default;
jar.add;
if let Some = jar.get
jar.remove;
assert!;
Parse a Cookie Request Header
CookieJar::parse accepts the Cookie request header format (name=value pairs separated by ; ).
use CookieJar;
let jar = parse.unwrap;
assert!;
assert!;
Generate Set-Cookie Response Headers
use ;
let mut jar = default;
jar.add;
jar.add;
for header in jar.as_header_values
Output:
Set-Cookie: name1=value1
Set-Cookie: name2=value2
Owned Cookies
To detach a cookie from the lifetime of the source string use into_owned:
use *;