Biscotti
A crate to handle HTTP cookies in a Rust server.
Overview
You can use biscotti to handle cookies in your server.
It has support for:
- Handling cookies attached to incoming requests, via
RequestCookies - Building cookies for outgoing responses, via
ResponseCookies - Encrypting, signing or encoding cookies, via
Processor
In particular:
- It can handle multiple request cookies with the same name
- It lets you add multiple cookies with the same name but different paths or domains
- Cookies are percent-encoded/decoded by default (but you can opt out)
- It has built-in support for rotating signing/encryption keys over time
Non-goals
biscotti is not designed to handle cookies on the client side.
It doesn't provide any logic to parse the Set-Cookie headers returned in a server response.
Quickstart
Incoming cookies
use ;
// Start by creating a `Processor` instance from a `Config`.
// It determines if (and which) cookies should be decrypted, verified or percent-decoded.
let processor: Processor = default.into;
// You can then use `RequestCookies::parse_header` to parse the `Cookie` header
// you received from the client.
let cookies = parse_header.unwrap;
// You can now access the cookies!
// You can access the first cookie with a given name...
assert_eq!;
// ...or opt to retrieve all values associated with that cookie name.
assert_eq!;
assert_eq!;
assert_eq!;
Outgoing cookies
use HashSet;
use ;
use SameSite;
// Start by creating a `ResponseCookies` instance to hold the cookies you want to send.
let mut cookies = new;
// You can add cookies to the `ResponseCookies` instance via the `insert` method.
cookies.insert;
cookies.insert;
// If you want to remove a cookie from the client's machine, you can use a `RemovalCookie`.
cookies.insert;
// You then convert obtain the respective `Set-Cookie` header values.
// A processor is required: it determines if (and which) cookies should be encrypted,
// signed or percent-encoded.
let processor: Processor = default.into;
let header_values: = cookies.header_values.collect;
assert_eq!;
Credits
biscotti is heavily inspired by the cookie crate [Copyright (c) 2017 Sergio Benitez,
Copyright (c) 2014 Alex Crichton].
biscotti started as a cookie fork and it includes non-negligible portions of its
code.