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 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
use crate::QuartzResult;
use chrono::prelude::*;
use hyper::http::uri::Scheme;
use std::{
collections::HashSet,
convert::Infallible,
hash::Hash,
ops::{Deref, DerefMut},
path::{Path, PathBuf},
str::FromStr,
};
pub enum Field {
Domain,
Subdomains,
Path,
Secure,
ExpiresAt,
Name,
Value,
}
#[derive(Debug, Clone)]
pub struct CookieError;
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Domain(String);
impl Deref for Domain {
type Target = String;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for Domain {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl From<&str> for Domain {
fn from(value: &str) -> Self {
Self::new(value)
}
}
impl Domain {
pub fn new<T>(s: T) -> Self
where
T: Into<String>,
{
let s = Self::canonicalize(&s.into());
Self(s)
}
/// Standardize representation of a domain name string `value`.
///
/// # Examples
///
/// ```
/// use quartz_cli::cookie::Domain;
///
/// assert_eq!(Domain::canonicalize("www.example.com"), "www.example.com");
/// assert_eq!(Domain::canonicalize("www.EXAMPLE.com"), "www.example.com");
/// assert_eq!(Domain::canonicalize("www.example.com\n"), "www.example.com");
/// assert_eq!(Domain::canonicalize(" www.example.com"), "www.example.com");
/// assert_eq!(Domain::canonicalize("www....example..com"), "www.example.com");
/// ```
// TODO: Allow non-ASCII characters (punycode)
pub fn canonicalize(value: &str) -> String {
let value = value.to_ascii_lowercase();
let value = value.trim();
let mut res = String::new();
let chars = value.chars();
let mut last = '*';
for ch in chars {
if ch == '.' && last == '.' {
continue;
}
last = ch;
res.push(ch);
}
res
}
/// Whether this [`Domain`] can set cookies for
/// `other` according to [RFC 6265](https://datatracker.ietf.org/doc/html/rfc6265).
///
/// # Examples
///
/// ```
/// use quartz_cli::cookie::Domain;
///
/// assert!(Domain::new(".example.com").matches("www.example.com"));
/// assert!(Domain::new(".example.com").matches("example.com"));
/// assert!(Domain::new("example.com").matches("www.example.com"));
/// assert!(Domain::new("www.example.com").matches("example.com"));
/// assert!(Domain::new("sub.sub.sub.example.com").matches("example.com"));
///
/// assert_eq!(Domain::new("example.com").matches("anotherexample.com"), false);
/// assert_eq!(Domain::new("www.example.com").matches("www2.example.com"), false);
/// assert_eq!(Domain::new("www.example.com").matches("www.example.com.au"), false);
/// ```
#[must_use]
pub fn matches<T>(&self, other: T) -> bool
where
T: Into<Domain>,
{
let other: Domain = other.into();
if **self == *other {
return true;
}
let this_segments: Vec<&str> = self.as_segments().collect();
let other_segments = other.as_segments();
for (idx, other_seg) in other_segments.enumerate() {
if this_segments.len() <= idx {
break;
}
if other_seg != this_segments[idx] {
return false;
}
}
true
}
/// Transforms a string into domain segments from top-level.
///
/// # Examples
///
/// ```
/// use quartz_cli::cookie::Domain;
///
/// let domain = Domain::new("www.example.com");
///
/// let expected = vec!["com", "example", "www"];
/// let result = domain.as_segments().collect::<Vec<&str>>();
/// assert_eq!(expected, result);
pub fn as_segments(&self) -> impl Iterator<Item = &str> {
self.split('.').filter(|s| !s.is_empty()).rev()
}
}
#[derive(Default)]
pub struct CookieBuilder {
domain: Option<String>,
subdomains: bool,
path: Option<String>,
secure: bool,
expires_at: i64,
name: Option<String>,
value: Option<String>,
}
impl CookieBuilder {
pub fn domain<T>(&mut self, s: T) -> &mut Self
where
T: Into<String>,
{
self.domain = Some(s.into());
self
}
pub fn subdomains(&mut self, v: bool) -> &mut Self {
self.subdomains = v;
self
}
pub fn path<T>(&mut self, s: T) -> &mut Self
where
T: Into<String>,
{
self.path = Some(s.into());
self
}
pub fn secure(&mut self, v: bool) -> &mut Self {
self.secure = v;
self
}
pub fn expires_at(&mut self, v: i64) -> &mut Self {
self.expires_at = v;
self
}
pub fn name<T>(&mut self, s: T) -> &mut Self
where
T: Into<String>,
{
self.name = Some(s.into());
self
}
pub fn value<T>(&mut self, s: T) -> &mut Self
where
T: Into<String>,
{
self.value = Some(s.into());
self
}
/// Generate [`Cookie`] from this [`CookieBuilder`] components.
///
/// # Errors
///
/// This function will return an error if builder has any invalid cookie component, such as
/// missing `domain`, `name`, or `value`.
pub fn build(self) -> QuartzResult<Cookie, CookieError> {
let domain = Domain::new(self.domain.ok_or(CookieError)?);
let name = self.name.ok_or(CookieError)?;
let value = self.value.ok_or(CookieError)?;
Ok(Cookie {
domain,
subdomains: self.subdomains,
path: PathAttr::from(self.path.unwrap_or_default().as_str()),
secure: self.secure,
expires_at: self.expires_at,
name,
value,
})
}
}
#[derive(Debug, Clone)]
pub struct Cookie {
domain: Domain,
subdomains: bool,
path: PathAttr,
secure: bool,
expires_at: i64,
name: String,
value: String,
}
impl Eq for Cookie {}
impl PartialEq for Cookie {
fn eq(&self, other: &Self) -> bool {
self.domain == other.domain && self.name == other.name
}
}
impl Hash for Cookie {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.domain.hash(state);
self.name.hash(state);
}
}
impl ToString for Cookie {
/// Converts a given [`Cookie`] into a Netspace HTTP Cookie file line.
///
///# Examples
///
/// ```
/// use quartz_cli::cookie::Cookie;
///
/// let mut cookie = Cookie::builder();
/// cookie
/// .domain("httpbin.org")
/// .subdomains(true)
/// .name("mysecret")
/// .value("supersecretkey")
/// .path("/somepath");
///
/// let cookie = cookie.build().unwrap();
///
/// assert_eq!(cookie.to_string(),
/// "httpbin.org\tTRUE\t/somepath\tFALSE\t0\tmysecret\tsupersecretkey");
/// ```
fn to_string(&self) -> String {
format!(
"{}\t{}\t{}\t{}\t{}\t{}\t{}",
*self.domain,
self.subdomains.to_string().to_uppercase(),
self.path.to_string(),
self.secure.to_string().to_uppercase(),
self.expires_at,
self.name,
self.value,
)
}
}
impl FromStr for Cookie {
type Err = CookieError;
/// Parses a string `s` to return a [`Cookie`].
///
/// If parsing succeeds, return the value inside [`Ok`], otherwise
/// when the string is ill-formatted return an error specific to the
/// inside [`Err`]. The error type is specific to the implementation of the trait.
///
/// # Examples
///
/// ```
/// use std::str::FromStr;
/// use quartz_cli::cookie::Cookie;
///
/// let s = "httpbin.org\tFALSE\t/somepath\tTRUE\t0\tmycookie\tsecret";
/// let cookie = Cookie::from_str(s).unwrap();
///
/// assert_eq!(**cookie.domain(), "httpbin.org");
/// assert_eq!(cookie.subdomains(), false);
/// assert_eq!(cookie.path().to_string(), "/somepath");
/// assert_eq!(cookie.secure(), true);
/// assert_eq!(cookie.name(), "mycookie");
/// assert_eq!(cookie.value(), "secret");
/// ```
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut cookie = Cookie::builder();
let line: Vec<&str> = s.splitn(7, '\t').collect();
if line.len() != 7 {
return Err(CookieError);
}
cookie
.domain(line[Field::Domain as usize])
.subdomains(line[Field::Subdomains as usize] == "TRUE")
.path(line[Field::Path as usize])
.secure(line[Field::Secure as usize] == "TRUE")
.name(line[Field::Name as usize])
.value(line[Field::Value as usize]);
if let Ok(v) = line[4].parse() {
cookie.expires_at(v);
}
cookie.build()
}
}
impl Cookie {
pub fn builder() -> CookieBuilder {
CookieBuilder::default()
}
pub fn matches<T>(&self, req: hyper::Request<T>) -> bool {
if !self.domain().matches(req.uri().host().unwrap_or_default()) {
return false;
}
if self.secure() {
let scheme = req.uri().scheme().unwrap_or(&Scheme::HTTP);
if scheme == &Scheme::HTTP {
return false;
}
}
if !self.path().matches(req.uri().path()) {
return false;
}
true
}
/// Whether this cookie is expired.
pub fn expired(&self) -> bool {
self.expires_at != 0 && Utc::now().timestamp_micros() > self.expires_at
}
pub fn domain(&self) -> &Domain {
&self.domain
}
pub fn subdomains(&self) -> bool {
self.subdomains
}
pub fn path(&self) -> &PathAttr {
&self.path
}
pub fn secure(&self) -> bool {
self.secure
}
pub fn expires_at(&self) -> i64 {
self.expires_at
}
pub fn name(&self) -> &str {
self.name.as_ref()
}
pub fn value(&self) -> &str {
self.value.as_ref()
}
}
#[derive(Default)]
pub struct CookieJar {
data: HashSet<Cookie>,
pub path: PathBuf,
}
impl Deref for CookieJar {
type Target = HashSet<Cookie>;
fn deref(&self) -> &Self::Target {
&self.data
}
}
impl DerefMut for CookieJar {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.data
}
}
impl ToString for CookieJar {
fn to_string(&self) -> String {
let mut jar = String::new();
for cookie in self.iter() {
jar.push_str(&cookie.to_string());
jar.push('\n');
}
jar
}
}
impl CookieJar {
fn pair(v: &str) -> Option<(&str, &str)> {
v.trim().split_once('=')
}
/// Insert new [`Cookie`] from Set-Cookie `input` from `domain`.
pub fn set(&mut self, domain: &str, input: &'_ str) -> Cookie {
let mut cookie = Cookie::builder();
cookie.domain(domain);
let (pair, settings) = input.split_once(';').unwrap_or((input, ""));
let (key, value) = Self::pair(pair).unwrap_or_else(|| panic!("malformed cookie: {}", pair));
cookie.name(key);
cookie.value(value);
for v in settings.split(';') {
let (key, value) = Self::pair(v).unwrap_or((v, ""));
match key.to_lowercase().as_str() {
"domain" => cookie.domain(value),
"path" => cookie.path(value),
"secure" => cookie.secure(true),
"max-age" => {
cookie.expires_at(value.parse::<i64>().unwrap() + Utc::now().timestamp_micros())
}
"expires" => cookie.expires_at(
DateTime::parse_from_rfc2822(value)
.unwrap()
.timestamp_micros(),
),
_ => &mut cookie,
};
}
let cookie = cookie.build().unwrap();
// Removing existing cookie ensures it is possible to
// overwrite its value.
if self.contains(&cookie) {
self.remove(&cookie);
}
// To remove a cookie, the server returns a Set-Cookie header
// with an expiration date in the past.
if !cookie.expired() {
self.insert(cookie.clone());
}
cookie
}
pub fn find_by_name(&self, s: &str) -> Vec<&Cookie> {
self.iter().filter(|c| c.name() == s).collect()
}
}
impl CookieJar {
pub const FILENAME: &'static str = "cookies";
/// Read [`CookieJar`] struct from Netscape HTTP Cookie file.
/// Empty, malformed, or commented (starting with "#") lines will be skipped.
///
/// Expired cookies are also ignored berfore loaded in-memory.
///
/// # Errors
///
/// This function will return an error if the file does not exist.
pub fn read(path: &Path) -> QuartzResult<Self> {
let mut cookies = Self::default();
let file = std::fs::read_to_string(path)?;
let lines = file.lines();
for line in lines {
if line.is_empty() || line.starts_with('#') {
continue;
}
if let Ok(cookie) = Cookie::from_str(line) {
if !cookie.expired() {
cookies.insert(cookie);
}
}
}
cookies.path = path.to_path_buf();
Ok(cookies)
}
/// Write cookie jar contents to environment cookie jar in Netspace HTTP Cookie file format.
pub fn write(&self) -> std::io::Result<()> {
self.write_at(&self.path)
}
/// Write cookie jar contents to `path` in Netspace HTTP Cookie file format.
pub fn write_at(&self, path: &Path) -> std::io::Result<()> {
std::fs::write(path, self.to_string())
}
}
#[derive(Debug, Default, Clone)]
pub struct PathAttr(Vec<String>);
impl Deref for PathAttr {
type Target = Vec<String>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for PathAttr {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl FromStr for PathAttr {
type Err = Infallible;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self::from(s))
}
}
impl From<&str> for PathAttr {
/// Parses a string `s` to return a cookie Path attribute.
///
/// # Examples
///
/// ```
/// use quartz_cli::cookie::PathAttr;
/// use std::str::FromStr;
///
/// assert_eq!(PathAttr::from_str("/").unwrap().len(), 0);
/// assert_eq!(PathAttr::from_str("invalid").unwrap().len(), 0);
/// assert_eq!(PathAttr::from_str("http://www.example.com").unwrap().len(), 0);
/// assert_eq!(*PathAttr::from_str("/somepath").unwrap(), vec!["somepath".to_string()]);
/// assert_eq!(
/// *PathAttr::from_str("http://www.example.com/somepath").unwrap(),
/// vec!["somepath".to_string()]
/// );
/// ```
fn from(value: &str) -> Self {
if let Ok(uri) = hyper::Uri::from_str(value) {
let path = uri
.path()
.split('/')
.filter(|v| !v.is_empty())
.map(String::from);
return Self(path.collect::<Vec<String>>());
}
// If the uri-path is empty or if the first character of the uri-
// path is not a %x2F ("/") character, output %x2F ("/") and skip
// the remaining steps.
if !value.starts_with('/') {
return Self::default();
}
let path: Vec<String> = value
.split('/')
.filter(|v| !v.is_empty())
.map(String::from)
.collect();
Self(path)
}
}
impl ToString for PathAttr {
/// Converts this into a Path attribute-value string.
///
/// # Examples
///
/// ```
/// use quartz_cli::cookie::PathAttr;
///
/// ```
fn to_string(&self) -> String {
let mut s = String::from("/");
s.push_str(&self.join("/"));
s
}
}
impl PathAttr {
/// Whether this cookie-path matches `other` request-path.
///
/// See [RFC 6265](https://datatracker.ietf.org/doc/html/rfc6265#section-1.3).
///
/// # Examples
///
/// ```
/// use quartz_cli::cookie::PathAttr;
///
/// assert!(PathAttr::from("/").matches("/"));
/// assert!(PathAttr::from("/").matches("/some/nested/path"));
/// assert!(PathAttr::from("/some").matches("/some/"));
/// assert!(PathAttr::from("/some").matches("/some/nested/path"));
///
/// assert_eq!(PathAttr::from("/somepath").matches("/some"), false);
/// assert_eq!(PathAttr::from("/somepath").matches("/"), false);
/// assert_eq!(PathAttr::from("/some/nested/path").matches("/"), false);
/// ```
#[must_use]
pub fn matches<T>(&self, other: T) -> bool
where
T: Into<PathAttr>,
{
if self.is_empty() {
return true;
}
let other: PathAttr = other.into();
if self.len() > other.len() {
return false;
}
for (idx, p) in self.iter().enumerate() {
if p != &other[idx] {
return false;
}
}
true
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn jar_set_overwrite() {
let mut jar = CookieJar::default();
jar.set("example.com", "foo=bar");
jar.set("example.com", "foo=baz");
let found = jar.find_by_name("foo");
assert_eq!(found.len(), 1);
assert_eq!(found[0].value(), "baz");
}
#[test]
fn jar_set_same_name_different_domain() {
let mut jar = CookieJar::default();
jar.set("example.com", "mycookie=true");
jar.set("httpbin.org", "mycookie=false");
let cookies = jar.find_by_name("mycookie");
assert_eq!(cookies.len(), 2);
let cookie = cookies
.iter()
.find(|c| c.domain().matches("example.com"))
.expect("did not find cookie");
assert_eq!(cookie.value(), "true");
let cookie = cookies
.iter()
.find(|c| c.domain().matches("httpbin.org"))
.expect("did not find cookie");
assert_eq!(cookie.value(), "false");
}
#[test]
fn jar_set_remove() {
let mut jar = CookieJar::default();
let foo = jar.set("httpbin.org", "foo=bar");
let baz = jar.set("httpbin.org", "baz=baz");
assert_eq!(jar.len(), 2);
assert!(jar.contains(&foo));
jar.set("httpbin.org", "foo=; Expires=Sun, 06 Nov 1994 08:49:37 GMT");
assert_eq!(jar.len(), 1);
assert!(!jar.contains(&foo));
jar.set(
"httpbin.org",
"baz=bar; Expires=Sun, 06 Nov 1994 08:49:37 GMT",
);
assert_eq!(jar.len(), 0);
assert!(!jar.contains(&baz));
}
}