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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
use std::marker::PhantomData;
use std::borrow::{Borrow, BorrowMut, Cow};
use crate::{CookieJar, Cookie};
/// A child jar that automatically [prefixes](Prefix) cookies.
///
/// Obtained via [`CookieJar::prefixed()`] and [`CookieJar::prefixed_mut()`].
///
/// This jar implements the [HTTP RFC6265 draft] "cookie prefixes" extension by
/// automatically adding and removing a specified [`Prefix`] from cookies that
/// are added and retrieved from this jar, respectively. Additionally, upon
/// being added to this jar, cookies are automatically made to
/// [conform](Prefix::conform()) to the corresponding prefix's specifications.
///
/// **Note:** Cookie prefixes are specified in an HTTP draft! Their meaning and
/// definition are subject to change.
///
/// [HTTP RFC6265 draft]:
/// https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis#name-cookie-name-prefixes
pub struct PrefixedJar<P: Prefix, J> {
parent: J,
_prefix: PhantomData<fn() -> P>,
}
/// The [`"__Host-"`] cookie [`Prefix`].
///
/// See [`Prefix`] and [`PrefixedJar`] for usage details.
///
/// [`"__Host-"`]:
/// https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis#name-the-__host-prefix
pub struct Host;
/// The [`"__Secure-"`] cookie [`Prefix`].
///
/// See [`Prefix`] and [`PrefixedJar`] for usage details.
///
/// [`"__Secure-"`]:
/// https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis#name-the-__secure-prefix
pub struct Secure;
/// Trait identifying [HTTP RFC6265 draft] cookie prefixes.
///
/// A [`Prefix`] can be applied to cookies via a child [`PrefixedJar`], itself
/// obtainable via [`CookieJar::prefixed()`] and [`CookieJar::prefixed_mut()`].
/// Cookies added/retrieved to/from these child jars have the corresponding
/// [prefix](Prefix::conform()) automatically prepended/removed as needed.
/// Additionally, added cookies are automatically make to
/// [conform](Prefix::conform()).
///
/// **Note:** Cookie prefixes are specified in an HTTP draft! Their meaning and
/// definition are subject to change.
///
/// [HTTP RFC6265 draft]:
/// https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis#name-cookie-name-prefixes
pub trait Prefix: private::Sealed {
/// The prefix string to prepend.
///
/// See [`Host::PREFIX`] and [`Secure::PREFIX`] for specifics.
const PREFIX: &'static str;
/// Alias to [`Host`].
#[allow(non_upper_case_globals)]
const Host: Host = Host;
/// Alias to [`Secure`].
#[allow(non_upper_case_globals)]
const Secure: Secure = Secure;
/// Modify `cookie` so it conforms to the requirements of `self`.
///
/// See [`Host::conform()`] and [`Secure::conform()`] for specifics.
//
// This is the only required method. Everything else is shared across
// implementations via the default implementations below and should not be
// implemented.
fn conform(cookie: Cookie<'_>) -> Cookie<'_>;
/// Returns a string with `name` prefixed with `self`.
#[doc(hidden)]
#[inline(always)]
fn prefixed_name(name: &str) -> String {
format!("{}{}", Self::PREFIX, name)
}
/// Prefix `cookie`'s name with `Self`.
#[doc(hidden)]
fn prefix(mut cookie: Cookie<'_>) -> Cookie<'_> {
use crate::CookieStr;
cookie.name = CookieStr::Concrete(match cookie.name {
CookieStr::Concrete(Cow::Owned(mut string)) => {
string.insert_str(0, Self::PREFIX);
string.into()
}
_ => Self::prefixed_name(cookie.name()).into(),
});
cookie
}
/// Remove the prefix `Self` from `cookie`'s name and return it.
///
/// If the prefix isn't in `cookie`, the cookie is returned unmodified. This
/// method is expected to be called only when `cookie`'s name is known to
/// contain the prefix.
#[doc(hidden)]
fn clip(mut cookie: Cookie<'_>) -> Cookie<'_> {
use std::borrow::Cow::*;
use crate::CookieStr::*;
if !cookie.name().starts_with(Self::PREFIX) {
return cookie;
}
let len = Self::PREFIX.len();
cookie.name = match cookie.name {
Indexed(i, j) => Indexed(i + len, j),
Concrete(Borrowed(v)) => Concrete(Borrowed(&v[len..])),
Concrete(Owned(v)) => Concrete(Owned(v[len..].to_string())),
};
cookie
}
/// Prefix and _conform_ `cookie`: prefix `cookie` with `Self` and make it
/// conform to the required specification by modifying it.
#[inline]
#[doc(hidden)]
fn apply(cookie: Cookie<'_>) -> Cookie<'_> {
Self::conform(Self::prefix(cookie))
}
}
impl<P: Prefix, J> PrefixedJar<P, J> {
#[inline(always)]
pub(crate) fn new(parent: J) -> Self {
Self { parent, _prefix: PhantomData }
}
}
impl<P: Prefix, J: Borrow<CookieJar>> PrefixedJar<P, J> {
/// Fetches the `Cookie` inside this jar with the prefix `P` and removes the
/// prefix before returning it. If the cookie isn't found, returns `None`.
///
/// See [`CookieJar::prefixed()`] for more examples.
///
/// # Example
///
/// ```rust
/// use cookie::CookieJar;
/// use cookie::prefix::{Host, Secure};
///
/// // Add a `Host` prefixed cookie.
/// let mut jar = CookieJar::new();
/// jar.prefixed_mut(Host).add(("h0st", "value"));
/// assert_eq!(jar.prefixed(Host).get("h0st").unwrap().name(), "h0st");
/// assert_eq!(jar.prefixed(Host).get("h0st").unwrap().value(), "value");
/// ```
pub fn get(&self, name: &str) -> Option<Cookie<'static>> {
self.parent.borrow()
.get(&P::prefixed_name(name))
.map(|c| P::clip(c.clone()))
}
}
impl<P: Prefix, J: BorrowMut<CookieJar>> PrefixedJar<P, J> {
/// Adds `cookie` to the parent jar. The cookie's name is prefixed with `P`,
/// and the cookie's attributes are made to [`conform`](Prefix::conform()).
///
/// See [`CookieJar::prefixed_mut()`] for more examples.
///
/// # Example
///
/// ```rust
/// use cookie::{Cookie, CookieJar};
/// use cookie::prefix::{Host, Secure};
///
/// // Add a `Host` prefixed cookie.
/// let mut jar = CookieJar::new();
/// jar.prefixed_mut(Secure).add(Cookie::build(("name", "value")).secure(false));
/// assert_eq!(jar.prefixed(Secure).get("name").unwrap().value(), "value");
/// assert_eq!(jar.prefixed(Secure).get("name").unwrap().secure(), Some(true));
/// ```
pub fn add<C: Into<Cookie<'static>>>(&mut self, cookie: C) {
self.parent.borrow_mut().add(P::apply(cookie.into()));
}
/// Adds `cookie` to the parent jar. The cookie's name is prefixed with `P`,
/// and the cookie's attributes are made to [`conform`](Prefix::conform()).
///
/// Adding an original cookie does not affect the [`CookieJar::delta()`]
/// computation. This method is intended to be used to seed the cookie jar
/// with cookies. For accurate `delta` computations, this method should not
/// be called after calling `remove`.
///
/// # Example
///
/// ```rust
/// use cookie::{Cookie, CookieJar};
/// use cookie::prefix::{Host, Secure};
///
/// // Add a `Host` prefixed cookie.
/// let mut jar = CookieJar::new();
/// jar.prefixed_mut(Secure).add_original(("name", "value"));
/// assert_eq!(jar.iter().count(), 1);
/// assert_eq!(jar.delta().count(), 0);
/// ```
pub fn add_original<C: Into<Cookie<'static>>>(&mut self, cookie: C) {
self.parent.borrow_mut().add_original(P::apply(cookie.into()));
}
/// Removes `cookie` from the parent jar.
///
/// The cookie's name is prefixed with `P`, and the cookie's attributes are
/// made to [`conform`](Prefix::conform()) before attempting to remove the
/// cookie. For correct removal, the passed in `cookie` must contain the
/// same `path` and `domain` as the cookie that was initially set.
///
/// # Example
///
/// ```rust
/// use cookie::{Cookie, CookieJar};
/// use cookie::prefix::{Host, Secure};
///
/// let mut jar = CookieJar::new();
/// let mut prefixed_jar = jar.prefixed_mut(Host);
///
/// prefixed_jar.add(("name", "value"));
/// assert!(prefixed_jar.get("name").is_some());
///
/// prefixed_jar.remove("name");
/// assert!(prefixed_jar.get("name").is_none());
/// ```
pub fn remove<C: Into<Cookie<'static>>>(&mut self, cookie: C) {
self.parent.borrow_mut().remove(P::apply(cookie.into()));
}
}
impl Prefix for Host {
/// The [`"__Host-"` prefix] string.
///
/// [`"__Host-"` prefix]:
/// https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis#name-the-__host-prefix
const PREFIX: &'static str = "__Host-";
/// Modify `cookie` so it conforms to the prefix's requirements.
///
/// **Note: this method is called automatically by [`PrefixedJar`]. It _does
/// not need to_ and _should not_ be called manually under normal
/// circumstances.**
///
/// According to [RFC 6265bis-12 §4.1.3.2]:
///
/// ```text
/// If a cookie's name begins with a case-sensitive match for the string
/// __Host-, then the cookie will have been set with a Secure attribute,
/// a Path attribute with a value of /, and no Domain attribute.
/// ```
///
/// As such, to make a cookie conforn, this method:
///
/// * Sets [`secure`](Cookie::set_secure()) to `true`.
/// * Sets the [`path`](Cookie::set_path()) to `"/"`.
/// * Removes the [`domain`](Cookie::unset_domain()), if any.
///
/// [RFC 6265bis-12 §4.1.3.2]:
/// https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis#name-the-__host-prefix
///
/// # Example
///
/// ```rust
/// use cookie::{CookieJar, Cookie, prefix::Host};
///
/// // A cookie with some non-conformant properties.
/// let cookie = Cookie::build(("name", "some-value"))
/// .secure(false)
/// .path("/foo/bar")
/// .domain("rocket.rs")
/// .http_only(true);
///
/// // Add the cookie to the jar.
/// let mut jar = CookieJar::new();
/// jar.prefixed_mut(Host).add(cookie);
///
/// // Fetch the cookie: notice it's been made to conform.
/// let cookie = jar.prefixed(Host).get("name").unwrap();
/// assert_eq!(cookie.name(), "name");
/// assert_eq!(cookie.value(), "some-value");
/// assert_eq!(cookie.secure(), Some(true));
/// assert_eq!(cookie.path(), Some("/"));
/// assert_eq!(cookie.domain(), None);
/// assert_eq!(cookie.http_only(), Some(true));
/// ```
fn conform(mut cookie: Cookie<'_>) -> Cookie<'_> {
cookie.set_secure(true);
cookie.set_path("/");
cookie.unset_domain();
cookie
}
}
impl Prefix for Secure {
/// The [`"__Secure-"` prefix] string.
///
/// [`"__Secure-"` prefix]:
/// https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis#name-the-__secure-prefix
const PREFIX: &'static str = "__Secure-";
/// Modify `cookie` so it conforms to the prefix's requirements.
///
/// **Note: this method is called automatically by [`PrefixedJar`]. It _does
/// not need to_ and _should not_ be called manually under normal
/// circumstances.**
///
/// According to [RFC 6265bis-12 §4.1.3.1]:
///
/// ```text
/// If a cookie's name begins with a case-sensitive match for the string
/// __Secure-, then the cookie will have been set with a Secure
/// attribute.
/// ```
///
/// As such, to make a cookie conforn, this method:
///
/// * Sets [`secure`](Cookie::set_secure()) to `true`.
///
/// [RFC 6265bis-12 §4.1.3.1]:
/// https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis#name-the-__secure-prefix
///
/// # Example
///
/// ```rust
/// use cookie::{CookieJar, Cookie, prefix::Secure};
///
/// // A cookie with some non-conformant properties.
/// let cookie = Cookie::build(("name", "some-value"))
/// .secure(false)
/// .path("/guide")
/// .domain("rocket.rs")
/// .http_only(true);
///
/// // Add the cookie to the jar.
/// let mut jar = CookieJar::new();
/// jar.prefixed_mut(Secure).add(cookie);
///
/// // Fetch the cookie: notice it's been made to conform.
/// let cookie = jar.prefixed(Secure).get("name").unwrap();
/// assert_eq!(cookie.name(), "name");
/// assert_eq!(cookie.value(), "some-value");
/// assert_eq!(cookie.secure(), Some(true));
/// assert_eq!(cookie.path(), Some("/guide"));
/// assert_eq!(cookie.domain(), Some("rocket.rs"));
/// assert_eq!(cookie.http_only(), Some(true));
/// ```
fn conform(mut cookie: Cookie<'_>) -> Cookie<'_> {
cookie.set_secure(true);
cookie
}
}
mod private {
pub trait Sealed {}
impl Sealed for super::Host {}
impl Sealed for super::Secure {}
}