wclient 0.1.7

wclient is a lightweight HTTP client library written in Rust.
Documentation
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
// Copyright 2021 Juan A. Cáceres (cacexp@gmail.com)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use chrono::Utc;
use chrono::NaiveDate;
use std::time::UNIX_EPOCH;
use chrono::DateTime;
use std::str::FromStr;
use std::io::{Error, ErrorKind};
use std::time::{Duration, SystemTime};
use std::ops::Add;
use std::collections::{HashMap, HashSet};
use std::cmp::{PartialEq, Eq};
use std::hash::{Hash, Hasher};
use lazy_static::lazy_static;
use regex::Regex;

pub(crate) const COOKIE: &str = "cookie";
pub(crate) const COOKIE_EXPIRES: &str = "expires";
pub(crate) const COOKIE_MAX_AGE: &str = "max-age";
pub(crate) const COOKIE_DOMAIN: &str = "domain";
pub(crate) const COOKIE_PATH: &str = "path";
pub(crate) const COOKIE_SAME_SITE: &str = "samesite";
pub(crate) const COOKIE_SAME_SITE_STRICT: &str = "strict";
pub(crate) const COOKIE_SAME_SITE_LAX: &str = "lax";
pub(crate) const COOKIE_SAME_SITE_NONE: &str = "none";
pub(crate) const COOKIE_SECURE: &str = "secure";
pub(crate) const COOKIE_HTTP_ONLY: &str = "httponly";

/// Enum with `SameSite` possible values for `Set-Cookie` attribute
#[derive(Debug,Copy,Clone,PartialEq)]
pub enum SameSiteValue {Strict, Lax, None}

impl FromStr for SameSiteValue {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        return match s {
            COOKIE_SAME_SITE_STRICT => Ok(SameSiteValue::Strict),
            COOKIE_SAME_SITE_LAX => Ok(SameSiteValue::Lax),
            COOKIE_SAME_SITE_NONE => Ok(SameSiteValue::None),
            _ => Err(
                Error::new(ErrorKind::InvalidData,
                           format!("Invalid SameSite cookie directive value: {}", s)))
        }

    }
}

/// Represents a cookie created from `Set-Cookie` response header. 
/// 
/// A `Cookie` can be parsed from the `Set-Cookie` value from an HTTP `Response` using the trait `FromStr`:
///
///  `let cookie = Cookie::from_str("id=a3fWa; Expires=Wed, 21 Oct 2022 07:28:00 GMT");`
/// 
/// See [RFC6265 Set-Cookie](https://datatracker.ietf.org/doc/html/rfc6265#section-4.2) for more information.

#[derive(Debug,Clone)]
pub struct Cookie {
    /// Cookie name
    pub(crate) name: String,
    /// Cookie value
    pub (crate) value: String,
    /// Cookie domain, by default is the originating domain of the request
    pub(crate) domain: String,
    /// Cookie path, by default, it is the request's path
    pub(crate) path: String,
    /// When the Cookie expires, if None, it does not expire.
    /// This value is obtained from Max-Age and Expires attributes (Max-Age has precedence)
    pub(crate) expires: Option<SystemTime>,
    /// Cookie same site value (option)
    pub(crate) same_site: SameSiteValue,
    /// Cookie requires HTTPS
    pub(crate) secure: bool,
    /// Browsers does not allow Javascript access to this cookie
    pub(crate) http_only: bool,
    /// Other Set-Cookie extensions
    pub(crate) extensions: HashMap<String, String>    
}


impl Cookie {
    /// Constructor. It takes ownership params:
    ///
    /// * `name`: Cookie name
    /// * `value`: Cookie value, for binary data it is recommended [Base64](https://en.wikipedia.org/wiki/Base64) encoding.
    /// * `domain`: Cookie domain, sets hosts (domain and subdomains) to which the cookie will be sent, in includes subdomains.
    /// If not present in `Set-Cookie` header, it is taken from the HTTP request `Host` header
    /// * `path`: Cookie path, paths (same path or children) to which the cookie will be sent
    /// If not present in `Set-Cookie` header, it is taken from the HTTP request path

    pub fn new (name: String, value: String, domain: String, path: String) -> Cookie {
        Cookie {
            name,
            value,
            domain,
            path,
            expires: None,
            same_site: SameSiteValue::Lax,
            secure: false,
            http_only: false,
            extensions: HashMap::new()            
        }
    }

    /// Cookie name
    pub fn name(& self) -> &str {
        self.name.as_str()
    }

    /// Cookie value
    pub fn value(& self) -> &str {
        self.value.as_str()
    }

    /// Cookie domain: hosts to which the cookie will be sent
    pub fn domain(& self) -> &str {
        self.domain.as_str()
    }

    /// Cookie path
    pub fn path(& self) -> &str {
        self.path.as_str()
    }
    /// When the Cookie expires, if `None`, it does not expire.
    /// This value is obtained from `Max-Age` and `Expires` attributes (Max-Age has precedence)
    pub fn expires(& self) -> Option<SystemTime> {
        self.expires.clone()
    }

    /// Cookie `Same-Site` value (optional)
    pub fn same_site(& self) -> SameSiteValue {
        self.same_site
    }
    /// Cookie requires HTTPS
    pub fn secure(& self) -> bool {
        self.secure
    }
    /// Cookie requires HTTP only
    pub fn http_only(& self) -> bool {
        self.http_only
    }

    /// Cookie extendions
    pub fn extensions(&self) -> &HashMap<String, String> {
        &self.extensions
    }

    /// Checks if the request path match the cookie path. 
    /// 
    /// Using [RFC6265 Section 5.1.4](https://datatracker.ietf.org/doc/html/rfc6265#section-5.1.4) Algorithm.    
    pub fn path_match(&self, request_path: &str) -> bool {
        
        let cookie_path = self.path();

        let cookie_path_len = cookie_path.len();
        let request_path_len = request_path.len();
 
       
        if !request_path.starts_with(cookie_path) {  // A. cookie path is a prefix of request path
            return false;
        }
    
        return request_path_len ==  cookie_path_len // 1. They are identical, or
            // 2. A and cookie path ends with an slash
            || cookie_path.chars().nth(cookie_path_len - 1).unwrap() == '/' 
            // 3. A and the first char of request path that is not incled in request path is an slash
            || request_path.chars().nth(cookie_path_len).unwrap() == '/'; 
    }

    /// Checks if the request domain match the cookie domain. 
    /// 
    /// Using [RFC6265 Section 4.1.1.3](https://datatracker.ietf.org/doc/html/rfc6265#section-4.1.2.3).
    pub fn domain_match(&self, request_domain: &str) -> bool {
        let cookie_domain = self.domain();
        
        if let Some(index) = request_domain.rfind(cookie_domain) {
            if index == 0 { // same domain
                return true;
            }
            // The cookie domain is a subdomain of request domain, acccept
            return request_domain.chars().nth(index-1).unwrap() == '.';
        }
         
        return false;
    }

    /// Checks if the cookie can be used on this request
    pub fn request_match(&self, request_domain: &str, request_path: &str, secure: bool) -> bool {

        // Match Secure restrictions 

        if self.secure && !secure {
            return false;
        }        
    
        // Strict behaviour: it is only same-site if the domain is the same

        if self.same_site == SameSiteValue::Strict && self.domain != request_domain {
            return false;
        }

        // Lax behaviour: allow cross-site from subdomain to father domain
        if self.same_site() == SameSiteValue::Lax && !self.domain_match(request_domain) {
            return false;
        }

        // None: allow all cookies transfer but only it HTTPS is in use
        if self.same_site == SameSiteValue::None && ! self.secure {
            return false;
        }

        // PATH filtering

        return self.path_match(request_path);      
    }

     /// Parses a cookie value and modifiers from a 'Set-Cookie'header
    pub fn parse(s: &str, domain: &str, path: &str) ->  Result<Cookie, Error> {
    let mut components = s.split(';');
 
        return if let Some(slice) = components.next() {
            let (key, value) = parse_cookie_value(slice)?;
            let mut cookie = Cookie::new(key, value, String::from(domain), String::from(path));
            while let Some(param) = components.next() {
                let directive = CookieDirective::from_str(param)?;
                match directive {
                    CookieDirective::Expires(date) => {
                        if cookie.expires().is_none() {  // Max-Age already parsed, it has precedence
                            cookie.expires = Some(date);
                        }
                    },
                    CookieDirective::MaxAge(seconds) => {
                         cookie.expires = Some(SystemTime::now().add(seconds));
                    },
                    CookieDirective::Domain(url) => {   // starting dot is ignored                      
                       cookie.domain = if let Some(stripped) = url.as_str().strip_prefix(".") {
                           String::from(stripped)
                       } else {
                           url
                       }    
                    },
                    CookieDirective::Path(path) => cookie.path = path,
                    CookieDirective::SameSite(val) => cookie.same_site = val,
                    CookieDirective::Secure => cookie.secure = true,
                    CookieDirective::HttpOnly => cookie.http_only = true,
                    CookieDirective::Extension(name, value) => {
                        let _res = cookie.extensions.insert(name, value);
                    }
                }
            }         
            Ok(cookie)
        } else {
            if CookieDirective::from_str(s).is_ok() {
                return Err(Error::new(ErrorKind::InvalidData, "Cookie has not got name/value"));
            };

            let (key, value) = parse_cookie_value(s)?;            
            Ok(Cookie::new(key, value, String::from(domain),  String::from(path)))
        }
    }
}

impl PartialEq for Cookie {
    fn eq(&self, other: &Self) -> bool {
        self.name == other.name 
    }    
}

impl Eq for Cookie{}

impl Hash for Cookie {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.name.hash(state);
        self.domain.hash(state);
    }
}


/// Helper function to parse the `Cookie` name and value
pub(crate) fn parse_cookie_value(cookie: &str) -> Result<(String, String), Error>{
    if let Some(index) = cookie.find('=') {
        let key = String::from(cookie[0..index].trim());
        let value = String::from(cookie[index + 1..].trim());
        return Ok((key, value))
    } else {
        Err(Error::new(ErrorKind::InvalidData,
                       format!("Malformed HTTP cookie: {}", cookie)))
    }
}

/// Helper enum to parse directives and set up the `Cookie` values
enum CookieDirective {
    Expires(SystemTime),
    MaxAge(Duration),
    Domain(String),
    Path(String),
    SameSite(SameSiteValue),
    Secure,
    HttpOnly,
    Extension(String, String)
}

// 
const DATE_FORMAT_850: &str= "(Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday|Mon|Tue|Wed|Thu|Fri|Sat|Sun), \
(0[1-9]|[123][0-9])-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-([0-9]{4}|[0-9]{2}) \
([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]) GMT";

// Regex for dates Sun, 06 Nov 1994 08:49:37 GMT
const DATE_FORMAT_1123: &str= "(Mon|Tue|Wed|Thu|Fri|Sat|Sun), \
(0[1-9]|[123][0-9]) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) ([0-9]{4}) \
([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]) GMT";


// Regex for dates Sun Nov 6 08:49:37 1994 
const DATE_FORMAT_ASCT: &str= "(Mon|Tue|Wed|Thu|Fri|Sat|Sun) \
(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[ ]{1,2}([1-9]|0[1-9]|[123][0-9]) \
([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]) ([0-9]{4})";

/// Parses RFC 850 dates, with extension. 
/// For example,  `Wed, 15-Nov-23 09:13:29 GMT` and  `Wed, 15-Nov-23 09:13:29 GMT` 
/// or `Sunday, 06-Nov-94 08:49:37 GMT` dates.
fn parse_rfc_850_date(date: &str) -> Result<SystemTime, Error> {
    lazy_static! {
        static ref RE: Regex = Regex::new(DATE_FORMAT_850).unwrap();
    }

    
    if let Some(captures) = RE.captures(date) {
        // Capture 0 is the full match and  1 is the day of the week name
        let day : u32 = captures.get(2).unwrap().as_str().parse().unwrap();
        let month = match captures.get(3).unwrap().as_str() {
            "Jan" => 1,
            "Feb" => 2,
            "Mar" => 3,
            "Apr" => 4,
            "May" => 5,
            "Jun" => 6,
            "Jul" => 7,
            "Aug" => 8,
            "Sep" => 9,
            "Oct" => 10,
            "Nov" => 11,
            "Dec" => 12,
            _ => return Err(Error::new(ErrorKind::InvalidData, "Invalid date"))
        };

        let mut year: i32 = captures.get(4).unwrap().as_str().parse().unwrap();
        // Fix millenium, for 2 digit year
        year+= if year < 70 {2000} else if year < 100 {1900} else {0};

        let hour : u32 = captures.get(5).unwrap().as_str().parse().unwrap();
        let min : u32 = captures.get(6).unwrap().as_str().parse().unwrap();
        let secs : u32 = captures.get(7).unwrap().as_str().parse().unwrap();

        let naive =
            NaiveDate::from_ymd(year, month, day)
            .and_hms(hour,min,secs);
        let time = DateTime::<Utc>::from_utc(naive, Utc);
        let millis = Duration::from_millis(time.timestamp_millis() as u64);
        let time = UNIX_EPOCH.clone().add(millis);

        return Ok(time);


    } else {
        return Err(Error::new(ErrorKind::InvalidData, "Invalid date"));
    }
}

/// Parses RFC 1123 dates. 
/// For example,  `Sun, 06 Nov 1994 08:49:37 GMT` date.
fn parse_rfc_1123_date(date: &str) -> Result<SystemTime, Error> {
    lazy_static! {
        static ref RE: Regex = Regex::new(DATE_FORMAT_1123).unwrap();
    }

    
    if let Some(captures) = RE.captures(date) {
        // Capture 0 is the full match and  1 is the day of the week name
        let day : u32 = captures.get(2).unwrap().as_str().parse().unwrap();
        let month = match captures.get(3).unwrap().as_str() {
            "Jan" => 1,
            "Feb" => 2,
            "Mar" => 3,
            "Apr" => 4,
            "May" => 5,
            "Jun" => 6,
            "Jul" => 7,
            "Aug" => 8,
            "Sep" => 9,
            "Oct" => 10,
            "Nov" => 11,
            "Dec" => 12,
            _ => return Err(Error::new(ErrorKind::InvalidData, "Invalid date"))
        };

        let year: i32 = captures.get(4).unwrap().as_str().parse().unwrap();

        let hour : u32 = captures.get(5).unwrap().as_str().parse().unwrap();
        let min : u32 = captures.get(6).unwrap().as_str().parse().unwrap();
        let secs : u32 = captures.get(7).unwrap().as_str().parse().unwrap();

        let naive =
            NaiveDate::from_ymd(year, month, day)
            .and_hms(hour,min,secs);
        let time = DateTime::<Utc>::from_utc(naive, Utc);
        let millis = Duration::from_millis(time.timestamp_millis() as u64);
        let time = UNIX_EPOCH.clone().add(millis);

        return Ok(time);


    } else {
        return Err(Error::new(ErrorKind::InvalidData, "Invalid date"));
    }
}

/// Parses Asct dates, with extension. 
/// For example,  `Sun Nov 6 08:49:37 1994` dates.
fn parse_asct_date(date: &str) -> Result<SystemTime, Error> {
    lazy_static! {
        static ref RE: Regex = Regex::new(DATE_FORMAT_ASCT).unwrap();
    }

    
    if let Some(captures) = RE.captures(date) {
        // Capture 0 is the full match and  1 is the day of the week name
        let month = match captures.get(2).unwrap().as_str() {
            "Jan" => 1,
            "Feb" => 2,
            "Mar" => 3,
            "Apr" => 4,
            "May" => 5,
            "Jun" => 6,
            "Jul" => 7,
            "Aug" => 8,
            "Sep" => 9,
            "Oct" => 10,
            "Nov" => 11,
            "Dec" => 12,
            _ => return Err(Error::new(ErrorKind::InvalidData, "Invalid date"))
        };

        let day : u32 = captures.get(3).unwrap().as_str().parse().unwrap();
        
        let hour : u32 = captures.get(4).unwrap().as_str().parse().unwrap();
        let min :  u32 = captures.get(5).unwrap().as_str().parse().unwrap();
        let secs : u32 = captures.get(6).unwrap().as_str().parse().unwrap();

        let year: i32 = captures.get(7).unwrap().as_str().parse().unwrap();
       
        let naive =
            NaiveDate::from_ymd(year, month, day)
            .and_hms(hour,min,secs);
        let time = DateTime::<Utc>::from_utc(naive, Utc);
        let millis = Duration::from_millis(time.timestamp_millis() as u64);
        let time = UNIX_EPOCH.clone().add(millis);

        return Ok(time);


    } else {
        return Err(Error::new(ErrorKind::InvalidData, "Invalid date"));
    }
}

/// Helper function to parse `CookieDirective`
impl FromStr for CookieDirective {
    
    type Err = Error;

    fn from_str(s: &str) -> Result<CookieDirective,Error> {
        if let Some(index) = s.find('=') { // Cookie param with value
            let key = s[0..index].trim().to_ascii_lowercase();
            let value = s[index + 1..].trim();
            return match key.as_str() {
                COOKIE_EXPIRES => {
                    let expires = parse_rfc_1123_date(value)
                        .or_else(|_| parse_rfc_850_date(value))
                        .or_else(|_| parse_asct_date(value))?; 

                    Ok(CookieDirective::Expires(expires))
                },
                COOKIE_MAX_AGE => {  // Max-age value in seconds
                    let digit = u64::from_str(value)
                        .or_else(|e|  {
                            Err(Error::new(ErrorKind::InvalidData, e))
                        })?;
                    Ok(CookieDirective::MaxAge(Duration::from_secs(digit)))
                },
                COOKIE_DOMAIN => {
                    Ok(CookieDirective::Domain(String::from(value)))
                },
                COOKIE_PATH => {
                    Ok(CookieDirective::Path(String::from(value)))
                }
                COOKIE_SAME_SITE => {
                    let lower_case = value.to_ascii_lowercase();
                    match SameSiteValue::from_str(lower_case.as_str()) {
                        Ok(site_value) => Ok(CookieDirective::SameSite(site_value)),
                        Err(e) => Err(e)
                    }
                },
                _ => Ok(CookieDirective::Extension(key, value.to_string()))
            }
        } else {
            match s.trim().to_ascii_lowercase().as_str() {
                COOKIE_SECURE => Ok(CookieDirective::Secure),
                COOKIE_HTTP_ONLY => Ok(CookieDirective::HttpOnly),
                _ => return Err(
                    Error::new(ErrorKind::InvalidData,
                            format!("Invalid HTTP cookie directive: {}", s)))
            }
        }
    }
}

/// Cookies repository trait. Keeps active cookies from a session.
pub trait CookieJar {
    /// Adds a cookie to the jar, if 'value' has no 'domain' member, 'request_domain' is used
    fn cookie(&mut self, value: Cookie, request_domain: &str);

    /// Gets the active cookie name/value list for the given domain (expired are deleted)
    fn active_cookies(&mut self, request_domain: &str, request_path: &str, secure: bool) -> Vec<(String, String)>;
 }


 pub struct MemCookieJar {
    // Hash set of cookies by target domain
    cookies: HashSet<Cookie>
 }

 impl MemCookieJar {
     pub fn new() -> MemCookieJar{
        MemCookieJar {
            cookies: HashSet::new()
        }
     }
 }

 
 impl CookieJar for MemCookieJar {
    fn cookie(&mut self, value: Cookie, request_domain: &str) {

        if !value.domain_match(request_domain) {
            return; // Discard different domain
        } 
        let now =  SystemTime::now();

        if let Some(expires) = value.expires() {
            if expires < now {
                return; // Discard expired 
            }
        }
       
        self.cookies.insert(value);
    }

    fn active_cookies(&mut self, request_domain: &str, request_path: &str, secure: bool) -> Vec<(String, String)> {
        
        let mut result = Vec::new();

        // First clean expired cookies
        let now =  SystemTime::now();

        self.cookies.retain( |c| {
            if let Some(time) = c.expires {
                return time < now;
            }
            return true;
        });
                
        for cookie in self.cookies.iter() {
            if cookie.request_match(request_domain, request_path, secure) {
                result.push((cookie.name.clone(), cookie.value.clone()));
            }
        }

        return result;
    }
 }

#[cfg(test)]
mod cookie_test;