hpx 2.5.7

High Performance HTTP Client
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
//! RFC 7838 Alternative Services (`Alt-Svc`) header parser.
//!
//! Parses HTTP `Alt-Svc` response headers into [`AltSvc`] entries that
//! describe alternative ways to reach the origin (e.g., HTTP/3 endpoints).
//!
//! # Format (RFC 7838 §3)
//!
//! ```text
//! Alt-Svc = clear / 1#alt-value
//! alt-value = protocol-id "=" alt-authority *( OWS ";" OWS parameter )
//! protocol-id = token
//! alt-authority = quoted-string
//! parameter = token "=" ( token / quoted-string )
//! clear = "clear"
//! ```
//!
//! The `alt-authority` contains a `host:port` pair, or just `:port` (meaning
//! same host as the origin). If the port is omitted, 443 is assumed.

use http::HeaderValue;

/// A single alternative service entry parsed from an `Alt-Svc` header.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct AltSvc {
    /// Protocol identifier (e.g., `"h3"`, `"h2"`).
    pub protocol: String,
    /// Hostname of the alternative service (empty string = same host as origin).
    pub host: String,
    /// Port number of the alternative service.
    pub port: u16,
    /// Maximum age in seconds (defaults to 86400 = 24 hours).
    pub max_age: u32,
    /// Whether the entry persists across network changes.
    pub persist: bool,
}

/// Parse an `Alt-Svc` header value.
///
/// Returns `None` if the header value cannot be parsed (no valid entries).
/// Returns `Some(vec![])` if the value is `"clear"` (case-insensitive),
/// signalling that all alternative services should be invalidated.
/// Returns `Some(vec![...])` with one or more [`AltSvc`] entries on success.
pub(crate) fn parse_alt_svc(header: &HeaderValue) -> Option<Vec<AltSvc>> {
    let value = header.to_str().ok()?;
    let value = value.trim();

    // "clear" directive (case-insensitive) — invalidate all cached entries.
    if value.eq_ignore_ascii_case("clear") {
        return Some(Vec::new());
    }

    let mut entries = Vec::new();
    for part in split_entries(value) {
        let part = part.trim();
        if part.is_empty() {
            continue;
        }
        if let Some(entry) = parse_alt_value(part) {
            entries.push(entry);
        }
    }

    if entries.is_empty() {
        None
    } else {
        Some(entries)
    }
}

// ---------------------------------------------------------------------------
// Alt-Svc cache
// ---------------------------------------------------------------------------

/// An entry in the Alt-Svc cache with insertion time for TTL tracking.
#[derive(Debug, Clone)]
struct AltSvcEntry {
    alt_svc: AltSvc,
    inserted_at: std::time::Instant,
}

/// A per-authority cache for `Alt-Svc` entries.
///
/// The key is a `(host, port)` tuple representing the authority.
/// Entries are evicted based on their `max_age` when retrieved.
pub(crate) struct AltSvcCache {
    entries: tokio::sync::RwLock<std::collections::HashMap<(String, u16), Vec<AltSvcEntry>>>,
}

impl AltSvcCache {
    /// Create an empty cache.
    pub(crate) fn new() -> Self {
        Self {
            entries: tokio::sync::RwLock::new(std::collections::HashMap::new()),
        }
    }

    /// Insert entries for an authority.
    ///
    /// If `entries` is empty (clear signal), remove all entries for that
    /// authority.
    pub(crate) async fn insert(&self, authority: (String, u16), entries: Vec<AltSvc>) {
        if entries.is_empty() {
            self.entries.write().await.remove(&authority);
            return;
        }
        let now = std::time::Instant::now();
        let alt_svc_entries: Vec<AltSvcEntry> = entries
            .into_iter()
            .map(|alt_svc| AltSvcEntry {
                alt_svc,
                inserted_at: now,
            })
            .collect();
        self.entries
            .write()
            .await
            .insert(authority, alt_svc_entries);
    }

    /// Returns non-expired entries for an authority.
    ///
    /// Entries where `inserted_at + Duration::from_secs(max_age) < now` are
    /// expired and filtered out. If all entries have expired, returns `None`.
    pub(crate) async fn get(&self, authority: &(String, u16)) -> Option<Vec<AltSvc>> {
        let guard = self.entries.read().await;
        let cached = guard.get(authority)?;
        let now = std::time::Instant::now();
        let alive: Vec<AltSvc> = cached
            .iter()
            .filter(|e| {
                let max_age = std::time::Duration::from_secs(u64::from(e.alt_svc.max_age));
                e.inserted_at + max_age >= now
            })
            .map(|e| e.alt_svc.clone())
            .collect();
        if alive.is_empty() { None } else { Some(alive) }
    }

    /// Remove all entries for an authority.
    #[allow(dead_code)] // HTTP/3 alt-svc invalidation API; reserved for future use.
    pub(crate) async fn invalidate(&self, authority: &(String, u16)) {
        self.entries.write().await.remove(authority);
    }
}

// ---------------------------------------------------------------------------
// H3 failure tracker (circuit breaker)
// ---------------------------------------------------------------------------

/// Tracks QUIC handshake failures per-authority to implement a circuit
/// breaker pattern for HTTP/3 (`h3`) connections.
///
/// When a QUIC connection to an authority `(host, port)` fails (e.g.,
/// handshake timeout, unreachable port), the failure is recorded with a
/// cooldown period. Subsequent requests to the same authority skip h3
/// and fall back to h2/h1 until the cooldown expires.
///
/// The cooldown defaults to 60 seconds. Failed authorities are
/// automatically cleared from the tracker after the cooldown expires,
/// allowing h3 retries on the next request.
///
/// # Thread safety
///
/// `H3FailureTracker` is wrapped in `Arc` and shared across `HttpClient`
/// clones. The internal map uses `tokio::sync::RwLock` for concurrent
/// read/write access.
pub(crate) struct H3FailureTracker {
    failures: tokio::sync::RwLock<std::collections::HashMap<(String, u16), std::time::Instant>>,
    cooldown: std::time::Duration,
}

impl H3FailureTracker {
    /// Create a new failure tracker with the given cooldown duration.
    pub(crate) fn new(cooldown: std::time::Duration) -> Self {
        Self {
            failures: tokio::sync::RwLock::new(std::collections::HashMap::new()),
            cooldown,
        }
    }

    /// Returns `true` if the authority has a recent failure and is still
    /// within the cooldown period.
    pub(crate) async fn is_blocked(&self, authority: &(String, u16)) -> bool {
        let guard = self.failures.read().await;
        match guard.get(authority) {
            Some(failure_time) => {
                let elapsed = failure_time.elapsed();
                elapsed < self.cooldown
            }
            None => false,
        }
    }

    /// Records a failure for the given authority.
    ///
    /// The authority is blocked from h3 attempts for the duration of the
    /// cooldown period.
    pub(crate) async fn record_failure(&self, authority: (String, u16)) {
        self.failures
            .write()
            .await
            .insert(authority, std::time::Instant::now());
    }

    /// Clears the failure record for the given authority.
    ///
    /// Called when a successful h3 connection is established, resetting
    /// the circuit breaker.
    pub(crate) async fn clear(&self, authority: &(String, u16)) {
        self.failures.write().await.remove(authority);
    }
}

// ---------------------------------------------------------------------------
// Internal helpers
// ---------------------------------------------------------------------------

/// Split a comma-separated list, respecting quoted strings (so commas inside
/// `"..."` are not treated as delimiters).
fn split_entries(input: &str) -> Vec<&str> {
    split_quoted(input, ',')
}

/// Split a semicolon-separated list, respecting quoted strings.
fn split_params(input: &str) -> Vec<&str> {
    split_quoted(input, ';')
}

/// Split `input` by `delimiter`, ignoring delimiters that appear inside
/// quoted strings (between `"` characters).
fn split_quoted(input: &str, delimiter: char) -> Vec<&str> {
    let mut parts = Vec::new();
    let mut start = 0;
    let mut in_quotes = false;
    let mut escaped = false;

    for (i, ch) in input.char_indices() {
        if escaped {
            escaped = false;
            continue;
        }
        match ch {
            '"' => in_quotes = !in_quotes,
            '\\' => escaped = true,
            c if c == delimiter && !in_quotes => {
                parts.push(&input[start..i]);
                start = i + 1;
            }
            _ => {}
        }
    }
    if start < input.len() {
        parts.push(&input[start..]);
    }
    parts
}

/// Parse a single `alt-value` entry:
/// `protocol-id "=" alt-authority *( ";" parameter )`
fn parse_alt_value(input: &str) -> Option<AltSvc> {
    let input = input.trim();

    // 1. protocol-id "="
    let eq_pos = input.find('=')?;
    let protocol = input[..eq_pos].trim();
    if !is_token(protocol) {
        return None;
    }

    let rest = input[eq_pos + 1..].trim();

    // 2. alt-authority (quoted string)
    let (authority, rest) = parse_quoted_string(rest)?;
    let (host, port) = parse_authority(&authority)?;

    // 3. Optional parameters
    let mut max_age: u32 = 86400;
    let mut persist = false;

    let rest = rest.trim();
    for param_str in split_params(rest) {
        let param_str = param_str.trim();
        if param_str.is_empty() {
            continue;
        }

        let param_eq = match param_str.find('=') {
            Some(p) => p,
            None => continue,
        };
        let name = param_str[..param_eq].trim();
        let value_part = param_str[param_eq + 1..].trim();

        let value = if value_part.starts_with('"') {
            match parse_quoted_string(value_part) {
                Some((v, _)) => v,
                None => continue,
            }
        } else if is_token(value_part) {
            value_part.to_string()
        } else {
            continue;
        };

        match name {
            "ma" => {
                if let Ok(v) = value.parse::<u32>() {
                    max_age = v;
                }
            }
            "persist" => {
                persist = value == "1";
            }
            _ => {
                // Unknown parameters are ignored per RFC 7838.
            }
        }
    }

    Some(AltSvc {
        protocol: protocol.to_string(),
        host,
        port,
        max_age,
        persist,
    })
}

/// Parse a quoted string, returning the unescaped content and the remainder
/// of the input after the closing quote.
///
/// Returns `None` if the input does not start with `"` or if the quoted
/// string is malformed (e.g., missing closing quote, invalid escape).
fn parse_quoted_string(input: &str) -> Option<(String, &str)> {
    let input = input.trim();
    let mut chars = input.char_indices();

    // Expect opening DQUOTE.
    match chars.next() {
        Some((_, '"')) => {}
        _ => return None,
    }

    let mut result = String::new();
    let mut escaped = false;

    for (pos, ch) in chars {
        if escaped {
            // quoted-pair: only DQUOTE and backslash are valid after escape.
            match ch {
                '"' | '\\' => result.push(ch),
                _ => return None,
            }
            escaped = false;
        } else if ch == '\\' {
            escaped = true;
        } else if ch == '"' {
            // Closing DQUOTE — return content and remainder.
            let rest = &input[pos + ch.len_utf8()..];
            return Some((result, rest));
        } else {
            // qdtext: any visible ASCII character.
            result.push(ch);
        }
    }

    // Reached end of input without finding closing quote.
    None
}

/// Parse the `alt-authority` content (already unquoted) into a `(host, port)` pair.
///
/// Supported formats:
/// - `":443"` → empty host (same as origin), port 443
/// - `"example.com:443"` → host="example.com", port=443
/// - `"example.com"` → host="example.com", port=443 (default)
fn parse_authority(authority: &str) -> Option<(String, u16)> {
    if authority.is_empty() {
        return None;
    }

    // ":port" — same host, explicit port.
    if let Some(port_str) = authority.strip_prefix(':') {
        let port = port_str.parse::<u16>().ok()?;
        return Some((String::new(), port));
    }

    // "host:port" or "host"
    if let Some(colon_pos) = authority.rfind(':') {
        let host = &authority[..colon_pos];
        let port_str = &authority[colon_pos + 1..];
        let port = port_str.parse::<u16>().ok()?;
        if host.is_empty() {
            return None;
        }
        Some((host.to_string(), port))
    } else {
        // Host only, default port.
        Some((authority.to_string(), 443))
    }
}

/// Check whether `s` is a valid HTTP `token` per RFC 7230 §3.2.6.
///
/// A token is a non-empty sequence of characters from the `tchar` set:
/// `!#$%&'*+-.^_`|~` plus digits and letters.
fn is_token(s: &str) -> bool {
    if s.is_empty() {
        return false;
    }
    s.chars().all(|c| {
        c.is_ascii_alphanumeric()
            || matches!(
                c,
                '!' | '#'
                    | '$'
                    | '%'
                    | '&'
                    | '\''
                    | '*'
                    | '+'
                    | '-'
                    | '.'
                    | '^'
                    | '_'
                    | '`'
                    | '|'
                    | '~'
            )
    })
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use http::HeaderValue;

    use super::*;

    // Helper to create a HeaderValue from a static string.
    fn hv(s: &'static str) -> HeaderValue {
        HeaderValue::from_static(s)
    }

    #[test]
    fn alt_svc_parse_valid() {
        let entries = parse_alt_svc(&hv(r#"h3=":443""#)).unwrap();
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].protocol, "h3");
        assert_eq!(entries[0].host, "");
        assert_eq!(entries[0].port, 443);
        assert_eq!(entries[0].max_age, 86400);
        assert!(!entries[0].persist);
    }

    #[test]
    fn alt_svc_parse_clear() {
        let entries = parse_alt_svc(&hv("clear")).unwrap();
        assert!(entries.is_empty());
    }

    #[test]
    fn alt_svc_parse_malformed() {
        assert!(parse_alt_svc(&hv("garbage")).is_none());
    }

    #[test]
    fn alt_svc_parse_multiple() {
        let entries = parse_alt_svc(&hv(r#"h3=":443", h2="alt.example.com:8443""#)).unwrap();
        assert_eq!(entries.len(), 2);

        assert_eq!(entries[0].protocol, "h3");
        assert_eq!(entries[0].host, "");
        assert_eq!(entries[0].port, 443);

        assert_eq!(entries[1].protocol, "h2");
        assert_eq!(entries[1].host, "alt.example.com");
        assert_eq!(entries[1].port, 8443);
    }

    #[test]
    fn alt_svc_parse_with_ma() {
        let entries = parse_alt_svc(&hv(r#"h3=":443"; ma=3600"#)).unwrap();
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].max_age, 3600);
    }

    #[test]
    fn alt_svc_parse_with_persist() {
        let entries = parse_alt_svc(&hv(r#"h3=":443"; persist=1"#)).unwrap();
        assert_eq!(entries.len(), 1);
        assert!(entries[0].persist);
    }

    #[test]
    fn alt_svc_parse_with_host() {
        let entries = parse_alt_svc(&hv(r#"h3="alt.example.com:443""#)).unwrap();
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].host, "alt.example.com");
        assert_eq!(entries[0].port, 443);
    }

    #[test]
    fn alt_svc_parse_case_insensitive_clear() {
        let entries = parse_alt_svc(&hv("CLEAR")).unwrap();
        assert!(entries.is_empty());
    }

    // ------------------------------------------------------------------
    // AltSvcCache tests
    // ------------------------------------------------------------------

    #[tokio::test]
    async fn alt_svc_cache_insert_lookup() {
        let cache = AltSvcCache::new();
        let authority = ("example.com".to_string(), 443);
        let entries = vec![AltSvc {
            protocol: "h3".to_string(),
            host: String::new(),
            port: 443,
            max_age: 86400,
            persist: false,
        }];
        cache.insert(authority.clone(), entries).await;
        let result = cache.get(&authority).await.unwrap();
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].protocol, "h3");
        assert_eq!(result[0].host, "");
        assert_eq!(result[0].port, 443);
    }

    #[tokio::test]
    async fn alt_svc_cache_expiry() {
        let cache = AltSvcCache::new();
        let authority = ("example.com".to_string(), 443);
        let entries = vec![AltSvc {
            protocol: "h3".to_string(),
            host: String::new(),
            port: 443,
            max_age: 0,
            persist: false,
        }];
        cache.insert(authority.clone(), entries).await;
        let result = cache.get(&authority).await;
        assert!(result.is_none());
    }

    #[tokio::test]
    async fn alt_svc_cache_invalidation() {
        let cache = AltSvcCache::new();
        let authority = ("example.com".to_string(), 443);
        let entries = vec![AltSvc {
            protocol: "h3".to_string(),
            host: String::new(),
            port: 443,
            max_age: 86400,
            persist: false,
        }];
        cache.insert(authority.clone(), entries).await;
        assert!(cache.get(&authority).await.is_some());
        cache.invalidate(&authority).await;
        assert!(cache.get(&authority).await.is_none());
    }

    // ------------------------------------------------------------------
    // H3FailureTracker tests
    // ------------------------------------------------------------------

    #[tokio::test]
    async fn h3_failure_tracker_is_blocked_after_failure() {
        let tracker = H3FailureTracker::new(std::time::Duration::from_secs(60));
        let authority = ("example.com".to_string(), 443);
        // Initially not blocked.
        assert!(!tracker.is_blocked(&authority).await);
        // Record a failure.
        tracker.record_failure(authority.clone()).await;
        // Now blocked.
        assert!(tracker.is_blocked(&authority).await);
    }

    #[tokio::test]
    async fn h3_failure_tracker_clear() {
        let tracker = H3FailureTracker::new(std::time::Duration::from_secs(60));
        let authority = ("example.com".to_string(), 443);
        tracker.record_failure(authority.clone()).await;
        assert!(tracker.is_blocked(&authority).await);
        tracker.clear(&authority).await;
        assert!(!tracker.is_blocked(&authority).await);
    }

    #[tokio::test]
    async fn h3_failure_tracker_cooldown_expires() {
        let tracker = H3FailureTracker::new(std::time::Duration::from_millis(100));
        let authority = ("example.com".to_string(), 443);
        tracker.record_failure(authority.clone()).await;
        assert!(tracker.is_blocked(&authority).await);
        // Wait for cooldown to expire.
        tokio::time::sleep(std::time::Duration::from_millis(200)).await;
        assert!(!tracker.is_blocked(&authority).await);
    }

    #[tokio::test]
    async fn h3_failure_tracker_different_authorities() {
        let tracker = H3FailureTracker::new(std::time::Duration::from_secs(60));
        let a1 = ("example.com".to_string(), 443);
        let a2 = ("example.com".to_string(), 8443);
        tracker.record_failure(a1.clone()).await;
        assert!(tracker.is_blocked(&a1).await);
        assert!(!tracker.is_blocked(&a2).await);
    }

    #[tokio::test]
    async fn alt_svc_cache_clear_signal() {
        let cache = AltSvcCache::new();
        let authority = ("example.com".to_string(), 443);
        let entries = vec![AltSvc {
            protocol: "h3".to_string(),
            host: String::new(),
            port: 443,
            max_age: 86400,
            persist: false,
        }];
        cache.insert(authority.clone(), entries).await;
        assert!(cache.get(&authority).await.is_some());
        // Insert empty vec (clear signal).
        cache.insert(authority.clone(), Vec::new()).await;
        assert!(cache.get(&authority).await.is_none());
    }
}