oxihttp-core 0.1.1

OxiHTTP core types: error and http crate re-exports.
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
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
//! Cookie parsing and management for the OxiHTTP stack.
//!
//! Provides a `Cookie` struct for individual cookie values and a `CookieJar`
//! for managing cookies across multiple requests and responses.

use std::fmt;
use std::time::Duration;

/// Represents a single HTTP cookie.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Cookie {
    pub name: String,
    pub value: String,
    pub domain: Option<String>,
    pub path: Option<String>,
    /// Max-Age attribute stored as a Duration.
    pub max_age: Option<Duration>,
    pub secure: bool,
    pub http_only: bool,
    pub same_site: Option<SameSite>,
    /// Computed absolute expiry time from max_age, set at insertion.
    pub expires_at: Option<std::time::Instant>,
}

/// The SameSite cookie attribute.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SameSite {
    /// Cookies are sent with same-site requests only.
    Strict,
    /// Cookies are sent with same-site requests and top-level navigations.
    Lax,
    /// Cookies are sent with all requests (requires Secure).
    None,
}

impl Cookie {
    /// Create a new cookie with the given name and value.
    pub fn new(name: impl Into<String>, value: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            value: value.into(),
            domain: None,
            path: None,
            max_age: None,
            secure: false,
            http_only: false,
            same_site: None,
            expires_at: None,
        }
    }

    /// The cookie name.
    pub fn name(&self) -> &str {
        &self.name
    }

    /// The cookie value.
    pub fn value(&self) -> &str {
        &self.value
    }

    /// The domain attribute.
    pub fn domain(&self) -> Option<&str> {
        self.domain.as_deref()
    }

    /// The path attribute.
    pub fn path(&self) -> Option<&str> {
        self.path.as_deref()
    }

    /// The max-age attribute as a `Duration`.
    pub fn max_age(&self) -> Option<Duration> {
        self.max_age
    }

    /// Whether the Secure flag is set.
    pub fn is_secure(&self) -> bool {
        self.secure
    }

    /// Whether the HttpOnly flag is set.
    pub fn is_http_only(&self) -> bool {
        self.http_only
    }

    /// The SameSite attribute.
    pub fn same_site(&self) -> Option<SameSite> {
        self.same_site
    }

    /// Set the domain attribute.
    pub fn set_domain(mut self, domain: impl Into<String>) -> Self {
        self.domain = Some(domain.into());
        self
    }

    /// Set the path attribute.
    pub fn set_path(mut self, path: impl Into<String>) -> Self {
        self.path = Some(path.into());
        self
    }

    /// Set the max-age attribute in seconds.
    pub fn set_max_age(mut self, seconds: u64) -> Self {
        self.max_age = Some(Duration::from_secs(seconds));
        self
    }

    /// Set the Secure flag.
    pub fn set_secure(mut self, secure: bool) -> Self {
        self.secure = secure;
        self
    }

    /// Set the HttpOnly flag.
    pub fn set_http_only(mut self, http_only: bool) -> Self {
        self.http_only = http_only;
        self
    }

    /// Set the SameSite attribute.
    pub fn set_same_site(mut self, same_site: SameSite) -> Self {
        self.same_site = Some(same_site);
        self
    }

    /// Parse a cookie from a `Set-Cookie` header value (RFC 6265).
    /// `expires_at` is left as `None` here; it is computed at insertion time by `CookieJar`.
    pub fn parse_set_cookie(header: &str) -> Option<Self> {
        let mut parts = header.split(';');
        let first = parts.next()?.trim();
        let (name, value) = first.split_once('=')?;
        let name = name.trim();
        let value = value.trim();

        if name.is_empty() {
            return None;
        }

        let mut cookie = Cookie::new(name, value);

        for attr in parts {
            let attr = attr.trim();
            if attr.is_empty() {
                continue;
            }
            if let Some((key, val)) = attr.split_once('=') {
                let key = key.trim().to_lowercase();
                let val = val.trim();
                match key.as_str() {
                    "domain" => cookie.domain = Some(val.to_string()),
                    "path" => cookie.path = Some(val.to_string()),
                    "max-age" => {
                        cookie.max_age = val.parse::<u64>().ok().map(Duration::from_secs);
                    }
                    "samesite" => {
                        cookie.same_site = match val.to_lowercase().as_str() {
                            "strict" => Some(SameSite::Strict),
                            "lax" => Some(SameSite::Lax),
                            "none" => Some(SameSite::None),
                            _ => None,
                        };
                    }
                    _ => {}
                }
            } else {
                match attr.to_lowercase().as_str() {
                    "secure" => cookie.secure = true,
                    "httponly" => cookie.http_only = true,
                    _ => {}
                }
            }
        }

        Some(cookie)
    }

    /// Serialize the cookie for use in a `Cookie` request header.
    pub fn to_cookie_header(&self) -> String {
        format!("{}={}", self.name, self.value)
    }
}

impl fmt::Display for Cookie {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}={}", self.name, self.value)?;
        if let Some(ref domain) = self.domain {
            write!(f, "; Domain={domain}")?;
        }
        if let Some(ref path) = self.path {
            write!(f, "; Path={path}")?;
        }
        if let Some(max_age) = self.max_age {
            write!(f, "; Max-Age={}", max_age.as_secs())?;
        }
        if self.secure {
            write!(f, "; Secure")?;
        }
        if self.http_only {
            write!(f, "; HttpOnly")?;
        }
        if let Some(same_site) = self.same_site {
            match same_site {
                SameSite::Strict => write!(f, "; SameSite=Strict")?,
                SameSite::Lax => write!(f, "; SameSite=Lax")?,
                SameSite::None => write!(f, "; SameSite=None")?,
            }
        }
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// RFC 6265 helpers
// ---------------------------------------------------------------------------

/// RFC 6265 §5.1.3 domain-match
fn domain_match(cookie_domain: &str, request_host: &str) -> bool {
    let cd = cookie_domain.to_lowercase();
    let rh_full = request_host.to_lowercase();
    // Strip port from request_host if present
    let rh = rh_full.split(':').next().unwrap_or(&rh_full);
    // Remove leading dot from cookie domain
    let cd = cd.strip_prefix('.').unwrap_or(&cd);
    // Exact match
    if rh == cd {
        return true;
    }
    // Suffix match: host ends with "." + cookie_domain, and not an IP
    if rh.ends_with(&format!(".{cd}")) {
        // Ensure cookie_domain is not an IP literal
        return cd.parse::<std::net::IpAddr>().is_err();
    }
    false
}

/// RFC 6265 §5.1.4 path-match
fn path_match(cookie_path: &str, request_path: &str) -> bool {
    if request_path == cookie_path {
        return true;
    }
    if let Some(remaining) = request_path.strip_prefix(cookie_path) {
        // Next char must be '/' or cookie_path ends with '/'
        return remaining.starts_with('/') || cookie_path.ends_with('/');
    }
    false
}

// ---------------------------------------------------------------------------
// CookieJar
// ---------------------------------------------------------------------------

/// A jar for collecting and managing cookies across requests and responses.
#[derive(Debug, Clone, Default)]
pub struct CookieJar {
    pub cookies: Vec<Cookie>,
}

impl CookieJar {
    /// Create an empty cookie jar.
    pub fn new() -> Self {
        Self::default()
    }

    /// Add or replace a cookie in the jar (keyed by name only, for backward compat).
    pub fn insert(&mut self, cookie: Cookie) {
        self.cookies.retain(|c| c.name != cookie.name);
        self.cookies.push(cookie);
    }

    /// Get a cookie by name (first match).
    pub fn get(&self, name: &str) -> Option<&Cookie> {
        self.cookies.iter().find(|c| c.name == name)
    }

    /// Remove a cookie by name. Returns the removed cookie if it existed.
    pub fn remove(&mut self, name: &str) -> Option<Cookie> {
        if let Some(pos) = self.cookies.iter().position(|c| c.name == name) {
            Some(self.cookies.remove(pos))
        } else {
            None
        }
    }

    /// Iterate over all cookies.
    pub fn iter(&self) -> impl Iterator<Item = &Cookie> {
        self.cookies.iter()
    }

    /// The number of cookies in the jar.
    pub fn len(&self) -> usize {
        self.cookies.len()
    }

    /// Returns `true` if the jar is empty.
    pub fn is_empty(&self) -> bool {
        self.cookies.is_empty()
    }

    /// Build a `Cookie` header value from all cookies in the jar.
    pub fn to_cookie_header(&self) -> String {
        self.cookies
            .iter()
            .map(|c| c.to_cookie_header())
            .collect::<Vec<_>>()
            .join("; ")
    }

    /// Parse cookies from multiple `Set-Cookie` header values and add them to the jar.
    pub fn add_from_set_cookie_headers<'a, I: IntoIterator<Item = &'a str>>(&mut self, headers: I) {
        for header in headers {
            if let Some(cookie) = Cookie::parse_set_cookie(header) {
                self.insert(cookie);
            }
        }
    }

    /// Insert a cookie with URL context for default domain/path assignment and expiry computation.
    pub fn insert_for_url(&mut self, mut cookie: Cookie, request_url: &http::Uri) {
        // Default domain = request host when cookie has no Domain attr
        if cookie.domain.is_none() {
            if let Some(host) = request_url.host() {
                cookie.domain = Some(host.split(':').next().unwrap_or(host).to_lowercase());
            }
        } else {
            // Normalize: remove leading dot
            if let Some(d) = &cookie.domain {
                cookie.domain = Some(d.trim_start_matches('.').to_lowercase());
            }
        }
        // Default path = up to last '/' in request path
        if cookie.path.is_none() {
            let req_path = request_url.path();
            let default_path = if let Some(pos) = req_path.rfind('/') {
                if pos == 0 {
                    "/"
                } else {
                    &req_path[..pos]
                }
            } else {
                "/"
            };
            cookie.path = Some(default_path.to_string());
        }
        // Compute expires_at from max_age
        cookie.expires_at = cookie.max_age.map(|dur| std::time::Instant::now() + dur);

        // Dedup by (name, domain, path)
        let name = cookie.name.clone();
        let domain = cookie.domain.clone();
        let path = cookie.path.clone();
        self.cookies
            .retain(|c| !(c.name == name && c.domain == domain && c.path == path));
        self.cookies.push(cookie);
    }

    /// Returns cookies matching the given URL per RFC 6265 §5.4.
    /// Filters by domain-match, path-match, secure, and expiry.
    /// Result is sorted by path length descending.
    pub fn cookies_for_url(&self, url: &http::Uri) -> Vec<&Cookie> {
        let now = std::time::Instant::now();
        let host = url.host().unwrap_or("");
        let path = url.path();
        let is_secure = url.scheme_str() == Some("https");

        let mut matched: Vec<&Cookie> = self
            .cookies
            .iter()
            .filter(|c| {
                // Expiry check
                if let Some(exp) = c.expires_at {
                    if exp <= now {
                        return false;
                    }
                }
                // Secure flag: secure cookies only for https
                if c.secure && !is_secure {
                    return false;
                }
                // Domain match
                let cookie_domain = c.domain.as_deref().unwrap_or("");
                if !cookie_domain.is_empty() && !domain_match(cookie_domain, host) {
                    return false;
                }
                // Path match
                let cookie_path = c.path.as_deref().unwrap_or("/");
                if !path_match(cookie_path, path) {
                    return false;
                }
                true
            })
            .collect();

        // Sort by path length descending (longer path = more specific = first)
        matched.sort_by(|a, b| {
            let a_len = a.path.as_deref().unwrap_or("/").len();
            let b_len = b.path.as_deref().unwrap_or("/").len();
            b_len.cmp(&a_len)
        });
        matched
    }

    /// Build a `Cookie` header value for the given URL, or None if no cookies match.
    pub fn to_cookie_header_for_url(&self, url: &http::Uri) -> Option<String> {
        let matched = self.cookies_for_url(url);
        if matched.is_empty() {
            return None;
        }
        Some(
            matched
                .iter()
                .map(|c| format!("{}={}", c.name, c.value))
                .collect::<Vec<_>>()
                .join("; "),
        )
    }

    /// Parse all `Set-Cookie` headers from an `http::HeaderMap` and insert them
    /// with URL context for default domain/path and expiry computation.
    pub fn add_from_response_headers(&mut self, headers: &http::HeaderMap, url: &http::Uri) {
        for value in headers.get_all(http::header::SET_COOKIE) {
            if let Ok(s) = value.to_str() {
                if let Some(cookie) = Cookie::parse_set_cookie(s) {
                    self.insert_for_url(cookie, url);
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_simple_cookie() {
        let cookie = Cookie::parse_set_cookie("session=abc123").expect("parse cookie");
        assert_eq!(cookie.name(), "session");
        assert_eq!(cookie.value(), "abc123");
    }

    #[test]
    fn test_parse_full_cookie() {
        let cookie = Cookie::parse_set_cookie(
            "id=a3fWa; Domain=.example.com; Path=/; Max-Age=3600; Secure; HttpOnly; SameSite=Lax",
        )
        .expect("parse full cookie");
        assert_eq!(cookie.name(), "id");
        assert_eq!(cookie.value(), "a3fWa");
        assert_eq!(cookie.domain(), Some(".example.com"));
        assert_eq!(cookie.path(), Some("/"));
        assert_eq!(cookie.max_age(), Some(Duration::from_secs(3600)));
        assert!(cookie.is_secure());
        assert!(cookie.is_http_only());
        assert_eq!(cookie.same_site(), Some(SameSite::Lax));
    }

    #[test]
    fn test_cookie_display() {
        let cookie = Cookie::new("session", "abc")
            .set_domain(".example.com")
            .set_path("/")
            .set_secure(true)
            .set_http_only(true);
        let s = cookie.to_string();
        assert!(s.contains("session=abc"));
        assert!(s.contains("Domain=.example.com"));
        assert!(s.contains("Secure"));
        assert!(s.contains("HttpOnly"));
    }

    #[test]
    fn test_cookie_jar_operations() {
        let mut jar = CookieJar::new();
        assert!(jar.is_empty());

        jar.insert(Cookie::new("a", "1"));
        jar.insert(Cookie::new("b", "2"));
        assert_eq!(jar.len(), 2);

        assert_eq!(jar.get("a").map(|c| c.value()), Some("1"));
        jar.remove("a");
        assert_eq!(jar.len(), 1);
        assert!(jar.get("a").is_none());
    }

    #[test]
    fn test_cookie_jar_header() {
        let mut jar = CookieJar::new();
        jar.insert(Cookie::new("a", "1"));
        jar.insert(Cookie::new("b", "2"));
        let header = jar.to_cookie_header();
        // Order not guaranteed, but both must be present
        assert!(header.contains("a=1"));
        assert!(header.contains("b=2"));
    }

    #[test]
    fn test_add_from_set_cookie_headers() {
        let mut jar = CookieJar::new();
        jar.add_from_set_cookie_headers(vec!["session=abc; HttpOnly", "lang=en; Path=/"]);
        assert_eq!(jar.len(), 2);
        assert!(jar.get("session").expect("session").is_http_only());
        assert_eq!(jar.get("lang").expect("lang").path(), Some("/"));
    }

    #[test]
    fn test_empty_name_rejected() {
        let result = Cookie::parse_set_cookie("=value");
        assert!(result.is_none());
    }

    #[test]
    fn test_domain_match_exact() {
        assert!(domain_match("example.com", "example.com"));
        assert!(domain_match(".example.com", "example.com")); // leading dot stripped
        assert!(!domain_match("example.com", "other.com"));
    }

    #[test]
    fn test_domain_match_suffix() {
        assert!(domain_match("example.com", "sub.example.com"));
        assert!(domain_match(".example.com", "sub.example.com"));
        assert!(!domain_match("example.com", "notexample.com"));
    }

    #[test]
    fn test_path_match() {
        assert!(path_match("/", "/foo/bar"));
        assert!(path_match("/foo", "/foo/bar"));
        assert!(path_match("/foo/", "/foo/bar"));
        assert!(!path_match("/foo", "/foobar")); // must be boundary
        assert!(path_match("/foo", "/foo"));
    }

    #[test]
    fn test_insert_for_url_defaults() {
        use http::Uri;
        let url: Uri = "http://example.com/api/v1/items".parse().expect("uri");
        let cookie = Cookie::new("session", "abc");
        let mut jar = CookieJar::new();
        jar.insert_for_url(cookie, &url);
        assert_eq!(jar.cookies[0].domain.as_deref(), Some("example.com"));
        assert_eq!(jar.cookies[0].path.as_deref(), Some("/api/v1"));
    }

    #[test]
    fn test_cookies_for_url() {
        use http::Uri;
        let url: Uri = "http://example.com/api/items".parse().expect("uri");
        let mut jar = CookieJar::new();
        let c = Cookie::new("session", "abc");
        jar.insert_for_url(c, &url);

        let matches = jar.cookies_for_url(&url);
        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].name, "session");

        // Different domain — no match
        let other_url: Uri = "http://other.com/api/items".parse().expect("uri");
        let no_matches = jar.cookies_for_url(&other_url);
        assert!(no_matches.is_empty());
    }

    #[test]
    fn test_expired_cookie_not_returned() {
        use http::Uri;
        let url: Uri = "http://example.com/".parse().expect("uri");
        let mut c = Cookie::new("old", "val");
        c.max_age = Some(std::time::Duration::from_secs(0)); // immediately expired
        let mut jar = CookieJar::new();
        jar.insert_for_url(c, &url);
        // Manually set expires_at to the past
        if let Some(cookie) = jar.cookies.first_mut() {
            cookie.expires_at = Some(std::time::Instant::now() - std::time::Duration::from_secs(1));
        }
        assert!(jar.cookies_for_url(&url).is_empty());
    }

    #[test]
    fn test_secure_cookie_https_only() {
        use http::Uri;
        let https_url: Uri = "https://example.com/".parse().expect("uri");
        let http_url: Uri = "http://example.com/".parse().expect("uri");
        let mut c = Cookie::new("secure_token", "xyz");
        c.secure = true;
        let mut jar = CookieJar::new();
        jar.insert_for_url(c, &https_url);
        // Only returned for https
        assert_eq!(jar.cookies_for_url(&https_url).len(), 1);
        assert!(jar.cookies_for_url(&http_url).is_empty());
    }

    #[test]
    fn test_same_name_different_domain_coexist() {
        use http::Uri;
        let url_a: Uri = "http://a.example.com/".parse().expect("uri");
        let url_b: Uri = "http://b.example.com/".parse().expect("uri");
        let mut jar = CookieJar::new();
        jar.insert_for_url(Cookie::new("token", "aaa"), &url_a);
        jar.insert_for_url(Cookie::new("token", "bbb"), &url_b);
        assert_eq!(jar.cookies.len(), 2);
        assert_eq!(jar.cookies_for_url(&url_a)[0].value, "aaa");
        assert_eq!(jar.cookies_for_url(&url_b)[0].value, "bbb");
    }

    // -------------------------------------------------------------------------
    // Proptest: Cookie name/value round-trip stability
    // -------------------------------------------------------------------------

    use proptest::prelude::*;

    /// Generate strings that are safe cookie name tokens:
    /// non-empty, alphanumeric only (strict subset of RFC 7230 token chars).
    fn cookie_name_strategy() -> impl Strategy<Value = String> {
        proptest::string::string_regex("[a-zA-Z][a-zA-Z0-9]{0,31}").expect("valid regex")
    }

    /// Generate strings that are safe cookie values:
    /// alphanumeric only, may be empty, no `;` or `=` or whitespace.
    fn cookie_value_strategy() -> impl Strategy<Value = String> {
        proptest::string::string_regex("[a-zA-Z0-9]{0,64}").expect("valid regex")
    }

    proptest! {
        #[test]
        fn prop_cookie_round_trip(
            name in cookie_name_strategy(),
            value in cookie_value_strategy(),
        ) {
            let original = Cookie::new(name.clone(), value.clone());
            let serialized = original.to_string();
            let parsed = Cookie::parse_set_cookie(&serialized)
                .expect("round-trip parse must succeed for valid name/value");
            prop_assert_eq!(&parsed.name, &name);
            prop_assert_eq!(&parsed.value, &value);
        }
    }

    // Known special-case round trips (no whitespace, no control chars)
    #[test]
    fn test_cookie_round_trip_known_cases() {
        let cases = [
            ("session", "abc123"),
            ("LANG", "en"),
            ("x", ""),
            ("Token", "abcdefghijklmnopqrstuvwxyz"),
            ("A1B2", "Z9Y8X7"),
        ];
        for (name, value) in cases {
            let original = Cookie::new(name, value);
            let serialized = original.to_string();
            let parsed = Cookie::parse_set_cookie(&serialized)
                .unwrap_or_else(|| panic!("failed to parse round-trip for {name}={value}"));
            assert_eq!(parsed.name, name, "name mismatch for {name}");
            assert_eq!(parsed.value, value, "value mismatch for {name}={value}");
        }
    }
}