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
//!
//! Cookies!
//! This module provides a simple interface for setting and receiving cookies.
use std::{
fmt,
ops::{Deref, DerefMut},
};
use crate::encoding::url;
/// Represents a Cookie
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct Cookie {
/// Cookie Key
pub name: String,
/// Cookie Value
pub value: String,
}
/// Represents a Set-Cookie header.
/// Has more information than a normal Cookie (e.g. max-age, domain, path, secure).
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct SetCookie {
/// Base Cookie
pub cookie: Cookie,
/// Cookie Max-Age.
/// Number of seconds until the cookie expires. A zero or negative number will expire the cookie immediately.
pub max_age: Option<u64>,
/// Cookie Domain
pub domain: Option<String>,
/// Cookie Path where the cookie is valid
pub path: Option<String>,
/// Cookie is secure
pub secure: bool,
}
/// A collection of Cookies.
#[derive(Debug, Clone)]
pub struct CookieJar(pub(crate) Vec<Cookie>);
impl Cookie {
/// Make a new Cookie from a name and a value.
/// ## Example
/// ```
/// # use afire::Cookie;
/// let cookie = Cookie::new("name", "value");
/// ```
pub fn new(name: impl AsRef<str>, value: impl AsRef<str>) -> Cookie {
Cookie {
name: name.as_ref().to_owned(),
value: value.as_ref().to_owned(),
}
}
/// Make a Vec of Cookies from a String.
/// Intended for making Cookie Vec from HTTP Headers.
pub fn from_string(cookie_string: &str) -> Vec<Cookie> {
let mut out = Vec::new();
for i in cookie_string.split(';') {
let (name, value) = match i.split_once('=') {
Some(i) => (i.0.trim(), i.1.trim()),
None => continue,
};
let name = url::decode(name).unwrap_or_else(|| name.to_owned());
let value = url::decode(value).unwrap_or_else(|| value.to_owned());
out.push(Cookie::new(name, value));
}
out
}
}
impl SetCookie {
/// Make a new SetCookie from a name and a value.
/// ## Example
/// ```
/// use afire::SetCookie;
/// let cookie = SetCookie::new("name", "value");
/// ```
pub fn new(name: impl AsRef<str>, value: impl AsRef<str>) -> SetCookie {
SetCookie {
cookie: Cookie::new(name, value),
max_age: None,
domain: None,
path: None,
secure: false,
}
}
/// Set the Max-Age field of a SetCookie.
/// This is the number of seconds the cookie should be valid for.
/// ## Example
/// ```
/// # use afire::SetCookie;
/// let mut cookie = SetCookie::new("name", "value")
/// .max_age(10 * 60);
///
/// assert_eq!(cookie.max_age, Some(10*60));
/// ```
pub fn max_age(self, max_age: u64) -> SetCookie {
SetCookie {
max_age: Some(max_age),
..self
}
}
/// Set the Domain field of a SetCookie.
/// ## Example
/// ```
/// # use afire::SetCookie;
/// let mut cookie = SetCookie::new("name", "value")
/// .domain("domain");
///
/// assert_eq!(cookie.domain, Some("domain".to_string()));
/// ```
pub fn domain(self, domain: impl AsRef<str>) -> SetCookie {
SetCookie {
domain: Some(domain.as_ref().to_owned()),
..self
}
}
/// Set the Path field of a SetCookie.
/// ## Example
/// ```
/// # use afire::SetCookie;
/// let mut cookie = SetCookie::new("name", "value")
/// .path("path");
///
/// assert_eq!(cookie.path, Some("path".to_string()));
/// ```
pub fn path(self, path: impl AsRef<str>) -> SetCookie {
SetCookie {
path: Some(path.as_ref().to_owned()),
..self
}
}
/// Set the Secure field of a SetCookie.
/// ## Example
/// ```
/// # use afire::SetCookie;
/// let mut cookie = SetCookie::new("name", "value")
/// .secure(true);
///
/// assert_eq!(cookie.secure, true);
/// ```
pub fn secure(self, secure: bool) -> SetCookie {
let mut new = self;
new.secure = secure;
new
}
}
impl CookieJar {
/// Create a new empty CookieJar.
pub fn new() -> CookieJar {
CookieJar(Vec::new())
}
/// Create a new CookieJar from a Vec of Cookies.
pub fn from_vec(cookies: Vec<Cookie>) -> CookieJar {
CookieJar(cookies)
}
/// Check if the cookie jar contains a cookie with the given name.
/// ## Example
/// ```rust
/// # use afire::cookie::CookieJar;
/// # fn test(jar: &CookieJar) {
/// if jar.has("Session") {
/// println!("Session cookie exists");
/// }
/// # }
pub fn has(&self, name: &str) -> bool {
self.iter().any(|i| i.name == name)
}
/// Adds a cookie to the jar with the given name and value.
/// See [`CookieJar::add_cookie`] for a version that takes a [`Cookie`] directly.
/// ## Example
/// ```rust
/// # use afire::cookie::CookieJar;
/// # fn test(jar: &mut CookieJar) {
/// jar.add("Session", "1234");
/// // ^^^^
/// // Very secure session ID
/// # }
pub fn add(&mut self, name: impl AsRef<str>, value: impl AsRef<str>) {
self.0.push(Cookie::new(name, value));
}
/// Gets the value of a cookie with the given name.
/// If the specified cookie does not exist, None is returned.
/// ## Example
/// ```rust
/// # use afire::cookie::CookieJar;
/// # fn test(jar: &CookieJar) {
/// if let Some(session) = jar.get("Session") {
/// println!("Session cookie value: {}", session);
/// }
/// # }
pub fn get(&self, name: &str) -> Option<&str> {
self.iter()
.find(|i| i.name == name)
.map(|x| x.value.as_str())
}
/// Gets a mutable reference to the value of a cookie with the given name.
/// If the specified cookie does not exist, None is returned.
/// See [`CookieJar::get`] for a non-mutable version.
/// ## Example
/// ```rust
/// # use afire::cookie::CookieJar;
/// # fn test(jar: &mut CookieJar) {
/// if let Some(session) = jar.get_mut("Session") {
/// *session = "new value".to_owned();
/// }
/// # }
pub fn get_mut(&mut self, name: &str) -> Option<&mut String> {
self.iter_mut()
.find(|i| i.name == name)
.map(|x| &mut x.value)
}
/// Adds the given cookie to the jar.
/// See [`CookieJar::add`] for a version that takes a name and value directly.
/// ## Example
/// ```rust
/// # use afire::cookie::{CookieJar, Cookie};
/// # fn test(jar: &mut CookieJar) {
/// jar.add_cookie(Cookie::new("Session", "1234"));
/// # }
pub fn add_cookie(&mut self, cookie: Cookie) {
self.0.push(cookie);
}
/// Gets a reference to the Cookie struct of a cookie with the given name.
/// If the specified cookie does not exist, None is returned.
/// ## Example
/// ```rust
/// # use afire::cookie::CookieJar;
/// # fn test(jar: &CookieJar) {
/// if let Some(session) = jar.get_cookie("Session") {
/// println!("Session cookie value: {}", session.value);
/// }
/// # }
pub fn get_cookie(&self, name: &str) -> Option<&Cookie> {
self.iter().find(|i| i.name == name)
}
/// Gets a mutable reference to the Cookie struct of a cookie with the given name.
/// If the specified cookie does not exist, None is returned.
/// See [`CookieJar::get_cookie`] for a non-mutable version.
/// ## Example
/// ```rust
/// # use afire::cookie::CookieJar;
/// # fn test(jar: &mut CookieJar) {
/// if let Some(session) = jar.get_cookie_mut("Session") {
/// session.value = "new value".to_owned();
/// }
/// # }
pub fn get_cookie_mut(&mut self, name: &str) -> Option<&mut Cookie> {
self.iter_mut().find(|i| i.name == name)
}
}
impl Default for CookieJar {
fn default() -> Self {
CookieJar::new()
}
}
impl Deref for CookieJar {
type Target = Vec<Cookie>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for CookieJar {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
// Impl ToString for Cookie
impl fmt::Display for Cookie {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}={}", self.name, self.value)
}
}
// Impl Display for SetCookie
impl fmt::Display for SetCookie {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut cookie_string = format!("{}={}; ", self.cookie.name, self.cookie.value);
// Add max_age
if self.max_age.is_some() {
cookie_string.push_str(&format!("Max-Age={}; ", self.max_age.unwrap()));
}
// Add domain
if self.domain.is_some() {
cookie_string.push_str(&format!("Domain={}; ", self.domain.as_ref().unwrap()));
}
// Add path
if self.path.is_some() {
cookie_string.push_str(&format!("Path={}; ", self.path.as_ref().unwrap()));
}
// Add secure
if self.secure {
cookie_string.push_str("Secure; ");
}
f.write_str(cookie_string.trim_end())
}
}
#[cfg(test)]
mod test {
use super::Cookie;
#[test]
fn test_cookie_parse() {
let cookie_string = "name=value; name2=value2; name3=value3";
let cookies = Cookie::from_string(cookie_string);
assert_eq!(cookies.len(), 3);
assert_eq!(cookies[0].name, "name");
assert_eq!(cookies[0].value, "value");
assert_eq!(cookies[1].name, "name2");
assert_eq!(cookies[1].value, "value2");
assert_eq!(cookies[2].name, "name3");
assert_eq!(cookies[2].value, "value3");
}
#[test]
fn test_ignore_cookie_parse() {
let cookie_string = "name=value; name2 value2; name3=value3;";
let cookies = Cookie::from_string(cookie_string);
assert_eq!(cookies.len(), 2);
assert_eq!(cookies[0].name, "name");
assert_eq!(cookies[0].value, "value");
assert_eq!(cookies[1].name, "name3");
assert_eq!(cookies[1].value, "value3");
}
}