liburlx 0.2.2

A memory-safe URL transfer library — idiomatic Rust reimplementation of libcurl
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
//! Alt-Svc header parsing (RFC 7838).
//!
//! Parses `Alt-Svc` response headers to discover alternative services
//! (e.g., HTTP/3 via QUIC). Currently provides parsing only; caching
//! and automatic upgrade are planned for future phases.

use std::collections::HashMap;
use std::time::{Duration, Instant};

/// A single alternative service entry from an `Alt-Svc` header.
///
/// Example: `h3=":443"; ma=2592000` produces an `AltSvc` with
/// `protocol_id = "h3"`, `host = ""`, `port = 443`, and
/// `max_age = Duration::from_secs(2592000)`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AltSvc {
    /// ALPN protocol identifier (e.g., "h3", "h2", "h3-29").
    pub protocol_id: String,
    /// Alternative hostname (empty string means same as origin).
    pub host: String,
    /// Alternative port number.
    pub port: u16,
    /// Maximum age before the entry expires.
    pub max_age: Duration,
}

/// Default max-age for Alt-Svc entries (24 hours per RFC 7838).
const DEFAULT_MAX_AGE_SECS: u64 = 86400;

/// Parse an `Alt-Svc` header value into a list of alternative service entries.
///
/// Handles the `clear` directive (returns empty vec) and multiple
/// comma-separated alternatives.
///
/// # Examples
///
/// ```
/// use liburlx::protocol::http::altsvc::parse_alt_svc;
///
/// let entries = parse_alt_svc(r#"h3=":443"; ma=2592000, h2=":443""#);
/// assert_eq!(entries.len(), 2);
/// assert_eq!(entries[0].protocol_id, "h3");
/// assert_eq!(entries[0].port, 443);
/// ```
#[must_use]
pub fn parse_alt_svc(value: &str) -> Vec<AltSvc> {
    let trimmed = value.trim();

    // "clear" means the server wants to invalidate all cached Alt-Svc entries
    if trimmed == "clear" {
        return Vec::new();
    }

    let mut results = Vec::new();

    for entry in split_entries(trimmed) {
        if let Some(alt) = parse_single_entry(entry.trim()) {
            results.push(alt);
        }
    }

    results
}

/// Split Alt-Svc header into individual entries, respecting quoted strings.
fn split_entries(value: &str) -> Vec<&str> {
    let mut entries = Vec::new();
    let mut start = 0;
    let mut in_quotes = false;

    for (i, ch) in value.char_indices() {
        match ch {
            '"' => in_quotes = !in_quotes,
            ',' if !in_quotes => {
                entries.push(&value[start..i]);
                start = i + 1;
            }
            _ => {}
        }
    }

    if start < value.len() {
        entries.push(&value[start..]);
    }

    entries
}

/// Parse a single Alt-Svc entry like `h3=":443"; ma=2592000`.
fn parse_single_entry(entry: &str) -> Option<AltSvc> {
    // Split on first '=' to get protocol_id and authority
    let (proto_id, rest) = entry.split_once('=')?;
    let protocol_id = proto_id.trim().to_string();

    // Parse the rest: "authority"; params...
    let mut parts = rest.splitn(2, ';');
    let authority_str = parts.next()?.trim().trim_matches('"');
    let params_str = parts.next().unwrap_or("");

    // Parse authority (host:port)
    let (host, port) = parse_authority(authority_str)?;

    // Parse parameters
    let max_age = parse_max_age(params_str);

    Some(AltSvc { protocol_id, host, port, max_age })
}

/// Parse an authority string like ":443" or "alt.example.com:443".
fn parse_authority(authority: &str) -> Option<(String, u16)> {
    if let Some(colon_pos) = authority.rfind(':') {
        let host = authority[..colon_pos].to_string();
        let port: u16 = authority[colon_pos + 1..].parse().ok()?;
        Some((host, port))
    } else {
        // No port — invalid
        None
    }
}

/// Parse the `ma=` parameter from Alt-Svc parameters.
fn parse_max_age(params: &str) -> Duration {
    for param in params.split(';') {
        let param = param.trim();
        if let Some(val) = param.strip_prefix("ma=") {
            if let Ok(secs) = val.trim().parse::<u64>() {
                return Duration::from_secs(secs);
            }
        }
    }
    Duration::from_secs(DEFAULT_MAX_AGE_SECS)
}

/// Parse a `Retry-After` response header value.
///
/// The value can be either:
/// - A number of seconds (e.g., `120`)
/// - An HTTP-date (e.g., `Fri, 31 Dec 1999 23:59:59 GMT`) — not yet supported
///
/// Returns `None` if the value cannot be parsed.
///
/// # Examples
///
/// ```
/// use liburlx::protocol::http::altsvc::parse_retry_after;
///
/// assert_eq!(parse_retry_after("120"), Some(std::time::Duration::from_secs(120)));
/// assert_eq!(parse_retry_after("0"), Some(std::time::Duration::from_secs(0)));
/// assert_eq!(parse_retry_after("not a number"), None);
/// ```
#[must_use]
pub fn parse_retry_after(value: &str) -> Option<Duration> {
    // Try parsing as seconds first (most common)
    if let Ok(secs) = value.trim().parse::<u64>() {
        return Some(Duration::from_secs(secs));
    }

    // HTTP-date parsing would go here in a future phase
    None
}

/// A cached Alt-Svc entry with expiry time.
#[derive(Debug, Clone)]
struct AltSvcEntry {
    /// The alternative service information.
    alt_svc: AltSvc,
    /// When this entry expires.
    expires_at: Instant,
}

/// An in-memory cache for Alt-Svc entries.
///
/// Stores alternative service entries per origin (scheme + host + port)
/// with TTL-based expiry. Used to remember that a server supports HTTP/3
/// or other alternative protocols.
#[derive(Debug)]
pub struct AltSvcCache {
    /// Cached entries keyed by origin (e.g., `"https://example.com:443"`).
    entries: HashMap<String, Vec<AltSvcEntry>>,
}

impl AltSvcCache {
    /// Create a new, empty Alt-Svc cache.
    #[must_use]
    pub fn new() -> Self {
        Self { entries: HashMap::new() }
    }

    /// Store Alt-Svc entries for an origin.
    ///
    /// The origin should be in the format "scheme://host:port".
    pub fn store(&mut self, origin: &str, services: &[AltSvc]) {
        let now = Instant::now();
        let entries: Vec<AltSvcEntry> = services
            .iter()
            .map(|svc| AltSvcEntry { alt_svc: svc.clone(), expires_at: now + svc.max_age })
            .collect();
        let _ = self.entries.insert(origin.to_string(), entries);
    }

    /// Clear all entries for an origin (used when server sends `Alt-Svc: clear`).
    pub fn clear_origin(&mut self, origin: &str) {
        let _ = self.entries.remove(origin);
    }

    /// Look up valid (non-expired) Alt-Svc entries for an origin.
    #[must_use]
    pub fn get(&self, origin: &str) -> Vec<&AltSvc> {
        let now = Instant::now();
        self.entries
            .get(origin)
            .map(|entries| {
                entries.iter().filter(|e| now < e.expires_at).map(|e| &e.alt_svc).collect()
            })
            .unwrap_or_default()
    }

    /// Look up a specific protocol (e.g., "h3") for an origin.
    #[must_use]
    pub fn get_protocol(&self, origin: &str, protocol_id: &str) -> Option<&AltSvc> {
        let now = Instant::now();
        self.entries.get(origin).and_then(|entries| {
            entries
                .iter()
                .find(|e| now < e.expires_at && e.alt_svc.protocol_id == protocol_id)
                .map(|e| &e.alt_svc)
        })
    }

    /// Remove expired entries from the cache.
    pub fn purge_expired(&mut self) {
        let now = Instant::now();
        self.entries.retain(|_, entries| {
            entries.retain(|e| now < e.expires_at);
            !entries.is_empty()
        });
    }

    /// Clear all entries from the cache.
    pub fn clear(&mut self) {
        self.entries.clear();
    }

    /// Returns the number of origins in the cache.
    #[must_use]
    pub fn len(&self) -> usize {
        self.entries.len()
    }

    /// Returns true if the cache is empty.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }
}

impl Default for AltSvcCache {
    fn default() -> Self {
        Self::new()
    }
}

impl Clone for AltSvcCache {
    fn clone(&self) -> Self {
        Self { entries: self.entries.clone() }
    }
}

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

    #[test]
    fn parse_clear() {
        let result = parse_alt_svc("clear");
        assert!(result.is_empty());
    }

    #[test]
    fn parse_single_h3() {
        let result = parse_alt_svc(r#"h3=":443""#);
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].protocol_id, "h3");
        assert_eq!(result[0].host, "");
        assert_eq!(result[0].port, 443);
        assert_eq!(result[0].max_age, Duration::from_secs(DEFAULT_MAX_AGE_SECS));
    }

    #[test]
    fn parse_with_max_age() {
        let result = parse_alt_svc(r#"h3=":443"; ma=2592000"#);
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].protocol_id, "h3");
        assert_eq!(result[0].port, 443);
        assert_eq!(result[0].max_age, Duration::from_secs(2_592_000));
    }

    #[test]
    fn parse_multiple_entries() {
        let result = parse_alt_svc(r#"h3=":443"; ma=2592000, h2=":443""#);
        assert_eq!(result.len(), 2);
        assert_eq!(result[0].protocol_id, "h3");
        assert_eq!(result[1].protocol_id, "h2");
    }

    #[test]
    fn parse_with_host() {
        let result = parse_alt_svc(r#"h3="alt.example.com:8443""#);
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].host, "alt.example.com");
        assert_eq!(result[0].port, 8443);
    }

    #[test]
    fn parse_versioned_protocol() {
        let result = parse_alt_svc(r#"h3-29=":443""#);
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].protocol_id, "h3-29");
    }

    #[test]
    fn parse_empty_string() {
        let result = parse_alt_svc("");
        assert!(result.is_empty());
    }

    #[test]
    fn parse_whitespace() {
        let result = parse_alt_svc("   ");
        assert!(result.is_empty());
    }

    #[test]
    fn parse_invalid_port() {
        let result = parse_alt_svc(r#"h3=":notaport""#);
        assert!(result.is_empty());
    }

    #[test]
    fn parse_no_port() {
        let result = parse_alt_svc(r#"h3="noport""#);
        assert!(result.is_empty());
    }

    #[test]
    fn retry_after_seconds() {
        assert_eq!(parse_retry_after("120"), Some(Duration::from_secs(120)));
    }

    #[test]
    fn retry_after_zero() {
        assert_eq!(parse_retry_after("0"), Some(Duration::from_secs(0)));
    }

    #[test]
    fn retry_after_with_whitespace() {
        assert_eq!(parse_retry_after("  60  "), Some(Duration::from_secs(60)));
    }

    #[test]
    fn retry_after_invalid() {
        assert_eq!(parse_retry_after("not a number"), None);
    }

    #[test]
    fn retry_after_http_date_not_supported() {
        // HTTP-date is not yet supported
        assert_eq!(parse_retry_after("Fri, 31 Dec 1999 23:59:59 GMT"), None);
    }

    #[test]
    fn split_entries_basic() {
        let entries = split_entries(r#"h3=":443", h2=":443""#);
        assert_eq!(entries.len(), 2);
    }

    #[test]
    fn split_entries_with_quoted_comma() {
        // Comma inside quotes should not split
        let entries = split_entries(r#"h3="host,name:443""#);
        assert_eq!(entries.len(), 1);
    }

    #[test]
    fn parse_authority_basic() {
        let (host, port) = parse_authority(":443").unwrap();
        assert_eq!(host, "");
        assert_eq!(port, 443);
    }

    #[test]
    fn parse_authority_with_host() {
        let (host, port) = parse_authority("example.com:8080").unwrap();
        assert_eq!(host, "example.com");
        assert_eq!(port, 8080);
    }

    // --- AltSvcCache tests ---

    #[test]
    fn cache_new_is_empty() {
        let cache = AltSvcCache::new();
        assert!(cache.is_empty());
        assert_eq!(cache.len(), 0);
    }

    #[test]
    fn cache_store_and_get() {
        let mut cache = AltSvcCache::new();
        let services = vec![AltSvc {
            protocol_id: "h3".to_string(),
            host: String::new(),
            port: 443,
            max_age: Duration::from_secs(3600),
        }];
        cache.store("https://example.com:443", &services);

        let result = cache.get("https://example.com:443");
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].protocol_id, "h3");
        assert_eq!(result[0].port, 443);
    }

    #[test]
    fn cache_get_missing_returns_empty() {
        let cache = AltSvcCache::new();
        assert!(cache.get("https://example.com:443").is_empty());
    }

    #[test]
    fn cache_get_protocol() {
        let mut cache = AltSvcCache::new();
        let services = vec![
            AltSvc {
                protocol_id: "h3".to_string(),
                host: String::new(),
                port: 443,
                max_age: Duration::from_secs(3600),
            },
            AltSvc {
                protocol_id: "h2".to_string(),
                host: String::new(),
                port: 443,
                max_age: Duration::from_secs(3600),
            },
        ];
        cache.store("https://example.com:443", &services);

        assert!(cache.get_protocol("https://example.com:443", "h3").is_some());
        assert!(cache.get_protocol("https://example.com:443", "h2").is_some());
        assert!(cache.get_protocol("https://example.com:443", "h1").is_none());
    }

    #[test]
    fn cache_expired_entries_not_returned() {
        let mut cache = AltSvcCache::new();
        let services = vec![AltSvc {
            protocol_id: "h3".to_string(),
            host: String::new(),
            port: 443,
            max_age: Duration::ZERO, // Immediately expired
        }];
        cache.store("https://example.com:443", &services);

        assert!(cache.get("https://example.com:443").is_empty());
        assert!(cache.get_protocol("https://example.com:443", "h3").is_none());
    }

    #[test]
    fn cache_clear_origin() {
        let mut cache = AltSvcCache::new();
        let services = vec![AltSvc {
            protocol_id: "h3".to_string(),
            host: String::new(),
            port: 443,
            max_age: Duration::from_secs(3600),
        }];
        cache.store("https://example.com:443", &services);
        assert_eq!(cache.len(), 1);

        cache.clear_origin("https://example.com:443");
        assert!(cache.is_empty());
    }

    #[test]
    fn cache_clear_all() {
        let mut cache = AltSvcCache::new();
        cache.store(
            "https://a.com:443",
            &[AltSvc {
                protocol_id: "h3".to_string(),
                host: String::new(),
                port: 443,
                max_age: Duration::from_secs(3600),
            }],
        );
        cache.store(
            "https://b.com:443",
            &[AltSvc {
                protocol_id: "h3".to_string(),
                host: String::new(),
                port: 443,
                max_age: Duration::from_secs(3600),
            }],
        );
        assert_eq!(cache.len(), 2);

        cache.clear();
        assert!(cache.is_empty());
    }

    #[test]
    fn cache_purge_expired() {
        let mut cache = AltSvcCache::new();
        cache.store(
            "https://expired.com:443",
            &[AltSvc {
                protocol_id: "h3".to_string(),
                host: String::new(),
                port: 443,
                max_age: Duration::ZERO,
            }],
        );
        cache.store(
            "https://valid.com:443",
            &[AltSvc {
                protocol_id: "h3".to_string(),
                host: String::new(),
                port: 443,
                max_age: Duration::from_secs(3600),
            }],
        );
        assert_eq!(cache.len(), 2);

        cache.purge_expired();
        assert_eq!(cache.len(), 1);
        assert!(!cache.get("https://valid.com:443").is_empty());
    }

    #[test]
    fn cache_default_is_empty() {
        let cache = AltSvcCache::default();
        assert!(cache.is_empty());
    }

    #[test]
    fn cache_clone() {
        let mut cache = AltSvcCache::new();
        cache.store(
            "https://example.com:443",
            &[AltSvc {
                protocol_id: "h3".to_string(),
                host: String::new(),
                port: 443,
                max_age: Duration::from_secs(3600),
            }],
        );
        let cloned = cache.clone();
        assert_eq!(cloned.len(), 1);
    }

    // --- Alt-Svc h3 upgrade tests ---

    #[test]
    fn cache_h3_upgrade_from_header() {
        // Simulate receiving an Alt-Svc header and checking for h3 upgrade
        let entries = parse_alt_svc(r#"h3=":443"; ma=86400"#);
        assert_eq!(entries.len(), 1);

        let mut cache = AltSvcCache::new();
        cache.store("https://example.com:443", &entries);

        // Should find h3 for subsequent requests
        let h3 = cache.get_protocol("https://example.com:443", "h3");
        assert!(h3.is_some());
        assert_eq!(h3.unwrap().port, 443);
    }

    #[test]
    fn cache_h3_upgrade_different_port() {
        // Server advertises h3 on a different port
        let entries = parse_alt_svc(r#"h3=":8443"; ma=86400"#);
        let mut cache = AltSvcCache::new();
        cache.store("https://example.com:443", &entries);

        let h3 = cache.get_protocol("https://example.com:443", "h3");
        assert!(h3.is_some());
        assert_eq!(h3.unwrap().port, 8443);
    }

    #[test]
    fn cache_h3_upgrade_not_available_for_http() {
        // HTTP (not HTTPS) — h3 should not be cached for HTTP origins
        let entries = parse_alt_svc(r#"h3=":443"; ma=86400"#);
        let mut cache = AltSvcCache::new();
        cache.store("http://example.com:80", &entries);

        // Query for the HTTPS origin should not find it
        assert!(cache.get_protocol("https://example.com:443", "h3").is_none());
    }

    #[test]
    fn cache_clear_disables_h3_upgrade() {
        let entries = parse_alt_svc(r#"h3=":443"; ma=86400"#);
        let mut cache = AltSvcCache::new();
        cache.store("https://example.com:443", &entries);
        assert!(cache.get_protocol("https://example.com:443", "h3").is_some());

        // Server sends Alt-Svc: clear
        let clear_entries = parse_alt_svc("clear");
        assert!(clear_entries.is_empty());
        cache.clear_origin("https://example.com:443");
        assert!(cache.get_protocol("https://example.com:443", "h3").is_none());
    }

    #[test]
    fn cache_h3_upgrade_with_h2_fallback() {
        // Server advertises both h3 and h2
        let entries = parse_alt_svc(r#"h3=":443"; ma=86400, h2=":443"; ma=86400"#);
        let mut cache = AltSvcCache::new();
        cache.store("https://example.com:443", &entries);

        // h3 should be preferred
        assert!(cache.get_protocol("https://example.com:443", "h3").is_some());
        assert!(cache.get_protocol("https://example.com:443", "h2").is_some());
    }
}