cookie/same_site.rs
1//! This module contains types that represent cookie properties that are not yet
2//! standardized. That is, _draft_ features.
3
4use std::fmt;
5
6/// The `SameSite` cookie attribute.
7///
8/// A cookie with a `SameSite` attribute is imposed restrictions on when it is
9/// sent to the origin server in a cross-site request. If the `SameSite`
10/// attribute is "Strict", then the cookie is never sent in cross-site requests.
11/// If the `SameSite` attribute is "Lax", the cookie is only sent in cross-site
12/// requests with "safe" HTTP methods, i.e, `GET`, `HEAD`, `OPTIONS`, `TRACE`.
13/// If the `SameSite` attribute is "None", the cookie is sent in all cross-site
14/// requests if the "Secure" flag is also set, otherwise the cookie is ignored.
15/// This library automatically sets the "Secure" flag on cookies when
16/// `same_site` is set to `SameSite::None` as long as `secure` is not explicitly
17/// set to `false`.
18///
19/// If the `SameSite` attribute is not present (by not setting `SameSite`
20/// initally or passing `None` to [`Cookie::set_same_site()`]), then the cookie
21/// will be sent as normal.
22///
23/// **Note:** This cookie attribute is an [HTTP draft]! Its meaning and
24/// definition are subject to change.
25///
26/// [`Cookie::set_same_site()`]: crate::Cookie::set_same_site()
27/// [HTTP draft]: https://tools.ietf.org/html/draft-west-cookie-incrementalism-00
28#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
29pub enum SameSite {
30 /// The "Strict" `SameSite` attribute.
31 Strict,
32 /// The "Lax" `SameSite` attribute.
33 Lax,
34 /// The "None" `SameSite` attribute.
35 None
36}
37
38impl SameSite {
39 /// Returns `true` if `self` is `SameSite::Strict` and `false` otherwise.
40 ///
41 /// # Example
42 ///
43 /// ```rust
44 /// use cookie::SameSite;
45 ///
46 /// let strict = SameSite::Strict;
47 /// assert!(strict.is_strict());
48 /// assert!(!strict.is_lax());
49 /// assert!(!strict.is_none());
50 /// ```
51 #[inline]
52 pub fn is_strict(&self) -> bool {
53 match *self {
54 SameSite::Strict => true,
55 SameSite::Lax | SameSite::None => false,
56 }
57 }
58
59 /// Returns `true` if `self` is `SameSite::Lax` and `false` otherwise.
60 ///
61 /// # Example
62 ///
63 /// ```rust
64 /// use cookie::SameSite;
65 ///
66 /// let lax = SameSite::Lax;
67 /// assert!(lax.is_lax());
68 /// assert!(!lax.is_strict());
69 /// assert!(!lax.is_none());
70 /// ```
71 #[inline]
72 pub fn is_lax(&self) -> bool {
73 match *self {
74 SameSite::Lax => true,
75 SameSite::Strict | SameSite::None => false,
76 }
77 }
78
79 /// Returns `true` if `self` is `SameSite::None` and `false` otherwise.
80 ///
81 /// # Example
82 ///
83 /// ```rust
84 /// use cookie::SameSite;
85 ///
86 /// let none = SameSite::None;
87 /// assert!(none.is_none());
88 /// assert!(!none.is_lax());
89 /// assert!(!none.is_strict());
90 /// ```
91 #[inline]
92 pub fn is_none(&self) -> bool {
93 match *self {
94 SameSite::None => true,
95 SameSite::Lax | SameSite::Strict => false
96 }
97 }
98}
99
100impl fmt::Display for SameSite {
101 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
102 match *self {
103 SameSite::Strict => write!(f, "Strict"),
104 SameSite::Lax => write!(f, "Lax"),
105 SameSite::None => write!(f, "None"),
106 }
107 }
108}