1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//! An HTTP Cookie crate.
//!
//! # Overview
//! Exposes types like [`Cookie`] and [`CookieJar`] for working with HTTP cookies. This crate
//! focuses on server side applications. The main goals are simplicity and ease of use.
//!
//! # Prefix cookies
//! [`Cookie::host`] and [`Cookie::secure`] build cookies that use the `__Host-` and `__Secure-`
//! name prefixes ([RFC 6265bis ยง4.1.3](https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis#section-4.1.3)).
//! They set the attributes the prefix requires as defaults (which you may override) and apply
//! the prefix to the name on serialization. Parsing strips a recognized prefix from the name, so
//! a prefixed cookie is looked up in a [`CookieJar`] by its logical (unprefixed) name.
//!
//! ```rust
//! use cookie_monster::Cookie;
//!
//! let cookie = Cookie::host("id", "abc").build();
//! assert_eq!(cookie.serialize().as_deref(), Ok("__Host-id=abc; Path=/; Secure"));
//! ```
//!
//! # Usage
//! Add cookie-monster in your Cargo.toml:
//! ```toml
//![dependencies]
//!cookie-monster = "0.1"
//!```
//!
//! # Features
//! * `jiff`
//!
//! Adds support for the [jiff](https://docs.rs/jiff/latest/jiff/) crate.
//! This exposes methods to [`Cookie`] to retrieve the `Expires` and `Max-Age` attributes with jiff
//! specific types.
//!
//! * `chrono`
//!
//! Adds support for the [chrono](https://docs.rs/chrono/latest/chrono/) crate.
//! This exposes methods to [`Cookie`] to retrieve the `Expires` and `Max-Age` attributes with
//! chrono specific types.
//!
//! * `time`
//!
//! Adds support for the [time](https://docs.rs/time/latest/time/index.html) crate.
//! This exposes methods to [`Cookie`] to retrieve the `Expires` and `Max-Age` attributes with time
//! specific types.
//!
//! * `percent-encode`
//!
//! Parse/serialize [`Cookie`]s that are percent-encoded.
//!
//! * `axum`
//!
//! Adds integration with the [axum](https://docs.rs/axum/latest/axum/) crate.
//! Implements [`IntoResponse`](https://docs.rs/axum/latest/axum/response/trait.IntoResponse.html) and
//! [`IntoResponseParts`](https://docs.rs/axum/latest/axum/response/trait.IntoResponseParts.html)
//! for [`Cookie`] and [`CookieJar`].
//! Implements [`FromRequestParts`](https://docs.rs/axum/latest/axum/extract/trait.FromRequestParts.html) only for [`CookieJar`]
//!
//! * `http`
//!
//! Adds integration with the [http](https://docs.rs/http/latest/http/) crate.
//! Create a [`CookieJar`] from a [`HeaderMap`](https://docs.rs/http/latest/http/header/struct.HeaderMap.html).
//! Write a [`CookieJar`] to a [`HeaderMap`](https://docs.rs/http/latest/http/header/struct.HeaderMap.html).
//!
//!
//! # Axum example
//!
//! ```rust
//! # #[cfg(feature="axum")]
//! # {
//! use axum::response::IntoResponse;
//! use cookie_monster::{Cookie, CookieJar, SameSite};
//!
//! static COOKIE_NAME: &str = "session";
//!
//! async fn handler(mut jar: CookieJar) -> impl IntoResponse {
//! if let Some(cookie) = jar.get(COOKIE_NAME) {
//! // Remove cookie
//! println!("Removing cookie {cookie:?}");
//! jar.remove(Cookie::named(COOKIE_NAME));
//! } else {
//! // Set cookie.
//! let cookie = Cookie::build(COOKIE_NAME, "hello, world")
//! .http_only()
//! .same_site(SameSite::Strict);
//!
//! println!("Setting cookie {cookie:?}");
//! jar.add(cookie);
//! }
//! // Return the jar so the cookies are updated
//! jar
//! }
//! # }
//! ```
//!
//!
//! ### Honorable mention
//! This crate takes a lot of inspiration from the [cookie](https://crates.io/crates/cookie) crate.
pub use ;
pub use Error;
pub type Result<T, E = Error> = Result;
pub use CookieJar;