htrio 0.2.0

A reqwest-compatible native HTTP/1, HTTP/2 and HTTP/3 client with Alt-Svc discovery
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
#![cfg(feature = "http3")]
//! HTTP/3 connection preparation and pooled request streams.
//!
//! The connector chooses direct UDP or SOCKS5 UDP without changing the
//! origin used for TLS and HTTP authority. Pools distinguish origins, network
//! destinations, and proxy credentials. Drivers retain their Quinn endpoints
//! and proxy tunnels for the lifetime of the connection.

pub(crate) mod connect;
pub(crate) mod dns;
mod pool;
pub(crate) mod session;
#[cfg(feature = "socks")]
mod socks;

use crate::error::BoxError;
use connect::H3Connector;
use http::uri::Authority;
use http::Uri;
use log::trace;
use pool::Pool;
use std::time::Duration;

pub(crate) use pool::{can_send_early, PoolClient};

/// Destination and handshake policy retained for one request hop and its retries.
#[derive(Clone)]
pub(crate) struct Route {
    pub(crate) authority: Authority,
    pub(crate) requires_ech: bool,
}
impl From<&crate::alt_svc::Alternative> for Route {
    fn from(alternative: &crate::alt_svc::Alternative) -> Self {
        Self {
            authority: alternative.authority.clone(),
            requires_ech: alternative.requires_ech,
        }
    }
}

#[derive(Clone)]
pub(crate) struct H3Client {
    pool: Pool,
    connector: H3Connector,
    health: std::sync::Arc<std::sync::Mutex<RouteHealth>>,
}

#[derive(Default)]
struct RouteHealth {
    generation: u64,
    coalescing_rejected: std::collections::HashSet<pool::Key>,
    probes: std::collections::HashSet<pool::Key>,
    failed: std::collections::HashMap<pool::Key, (u32, std::time::Instant)>,
}

pub(crate) struct RouteProbe {
    health: std::sync::Arc<std::sync::Mutex<RouteHealth>>,
    key: pool::Key,
    generation: u64,
}
impl Drop for RouteProbe {
    fn drop(&mut self) {
        let mut health = self.health.lock().unwrap();
        if health.generation == self.generation {
            health.probes.remove(&self.key);
        }
    }
}

impl H3Client {
    pub fn new(connector: H3Connector, pool_timeout: Option<Duration>) -> Self {
        Self {
            pool: Pool::new(pool_timeout),
            connector,
            health: Default::default(),
        }
    }

    // Establishing a connection cannot consume the request body. The transport
    // may fall back to TCP after this step fails, including for streaming POSTs.
    pub(crate) async fn prepare(
        &mut self,
        origin: &Uri,
        alternative: Option<Route>,
        proxy: Option<&crate::proxy::Intercepted>,
        allow_early: bool,
        deadline: Option<tokio::time::Instant>,
    ) -> Result<PoolClient, BoxError> {
        let alternative = if alternative.is_none() {
            self.discover(origin, proxy)
                .await
                .into_iter()
                .next()
                .as_ref()
                .map(Route::from)
        } else {
            alternative
        };
        let requires_ech = alternative
            .as_ref()
            .is_some_and(|alternative| alternative.requires_ech);
        let mut key = pool::extract_domain(origin)?;
        if let Some(alternative) = alternative {
            key.2 = alternative.authority;
        }
        key.3 = proxy.map(pool::ProxyKey::new);
        if let Some(mut client) = self.pool.try_pool(&key) {
            if (!requires_ech || client.info.uses_ech())
                && (allow_early || client.info.early.accepted().await)
            {
                trace!("getting HTTP/3 client from pool with key {key:?}");
                return Ok(client);
            }
            self.pool.remove(&key);
        }
        if let Some(client) = self.try_coalesce(origin, &key, requires_ech) {
            return Ok(client);
        }
        let lock = match self.pool.connecting(&key) {
            pool::Connecting::InProgress(waiter) => {
                let client = waiter
                    .receive()
                    .await
                    .ok_or("failed to establish HTTP/3 connection")?;
                if requires_ech && !client.info.uses_ech() {
                    return Err("selected ECH route requires a new encrypted handshake".into());
                }
                return Ok(client);
            }
            pool::Connecting::Acquired(lock) => lock,
        };
        let started = std::time::Instant::now();
        let generation = self.health.lock().unwrap().generation;
        let direct = proxy.is_none();
        let destination = pool::domain_as_uri(key.clone());
        let established = self
            .connector
            .connect(
                destination,
                Route {
                    authority: key.2.clone(),
                    requires_ech,
                },
                proxy,
                allow_early,
                deadline,
            )
            .await?;
        {
            let mut health = self.health.lock().unwrap();
            if health.generation == generation {
                health.failed.remove(&key);
            }
        }
        Ok(self.pool.new_connection(
            lock,
            established,
            direct,
            started.elapsed(),
            self.connector.tls_info(),
        ))
    }

    fn try_coalesce(
        &self,
        origin: &Uri,
        key: &pool::Key,
        requires_ech: bool,
    ) -> Option<PoolClient> {
        if requires_ech || !self.connector.coalescing_enabled() {
            return None;
        }
        let may_coalesce = {
            let health = self.health.lock().unwrap();
            health.coalescing_rejected.len() < 256 && !health.coalescing_rejected.contains(key)
        };
        if may_coalesce {
            for (previous, client) in self.pool.coalescing_candidates(key) {
                if self.connector.can_coalesce(origin, &key.2, &client.info) {
                    if let Some(client) = self.pool.alias(key.clone(), &previous) {
                        return Some(client);
                    }
                }
            }
        }
        None
    }

    pub(crate) async fn discover(
        &self,
        origin: &Uri,
        proxy: Option<&crate::proxy::Intercepted>,
    ) -> Vec<crate::alt_svc::Alternative> {
        if proxy.is_some_and(|proxy| proxy.uri().scheme_str() == Some("socks5h")) {
            return Vec::new();
        }
        let Ok(Ok(endpoints)) =
            tokio::time::timeout(Duration::from_millis(200), self.connector.discover(origin)).await
        else {
            return Vec::new();
        };
        endpoints
            .into_iter()
            .filter_map(|endpoint| {
                format!("{}:{}", endpoint.target, endpoint.port)
                    .parse()
                    .ok()
                    .map(|authority| {
                        crate::alt_svc::Alternative::service(
                            authority,
                            endpoint.ttl,
                            endpoint.ech_config_list.is_some(),
                        )
                    })
            })
            .collect()
    }

    pub(crate) fn network_generation(&mut self, generation: u64) -> bool {
        let is_newer = |current: u64| {
            let distance = generation.wrapping_sub(current);
            distance != 0 && distance < (1_u64 << 63)
        };
        if !is_newer(self.health.lock().unwrap().generation) {
            return false;
        }
        // Resolver hooks are application code and may inspect client statistics.
        // Never invoke them while holding a pool or route-health lock.
        self.connector.clear_dns_cache();
        let mut health = self.health.lock().unwrap();
        // A concurrent notification may have already installed a newer network.
        if is_newer(health.generation) {
            health.generation = generation;
            health.failed.clear();
            health.coalescing_rejected.clear();
            health.probes.clear();
            self.pool.clear();
            self.connector.network_changed();
            return true;
        }
        false
    }

    pub(crate) fn is_broken(
        &self,
        origin: &Uri,
        alternative: &Authority,
        proxy: Option<&crate::proxy::Intercepted>,
    ) -> bool {
        let Ok(mut key) = pool::extract_domain(origin) else {
            return false;
        };
        key.2 = alternative.clone();
        key.3 = proxy.map(pool::ProxyKey::new);
        self.health
            .lock()
            .unwrap()
            .failed
            .get(&key)
            .is_some_and(|(_, until)| *until > std::time::Instant::now())
    }

    // Call only after the same origin is reachable through the selected TCP
    // route. An offline network must not poison otherwise healthy QUIC routes.
    pub(crate) fn confirm_broken(
        &self,
        origin: &Uri,
        alternative: Authority,
        proxy: Option<&crate::proxy::Intercepted>,
    ) {
        let Ok(mut key) = pool::extract_domain(origin) else {
            return;
        };
        key.2 = alternative;
        key.3 = proxy.map(pool::ProxyKey::new);
        let mut health = self.health.lock().unwrap();
        if health.failed.len() >= 256 {
            health
                .failed
                .retain(|_, (_, until)| *until > std::time::Instant::now());
        }
        if health.failed.len() >= 256 {
            return;
        }
        let failures = health
            .failed
            .get(&key)
            .map_or(1, |(count, _)| count.saturating_add(1));
        let delay = Duration::from_secs((30_u64 << failures.min(7)).min(3600));
        health
            .failed
            .insert(key, (failures, std::time::Instant::now() + delay));
    }

    pub(crate) fn pool_stats(&self) -> crate::http3::PoolStats {
        let mut stats = self.pool.stats();
        let health = self.health.lock().unwrap();
        stats.probing = health.probes.len();
        stats.broken_routes = health
            .failed
            .values()
            .filter(|(_, until)| *until > std::time::Instant::now())
            .count();
        stats
    }

    pub(crate) fn rebind(&self, socket: std::net::UdpSocket) -> Result<(), BoxError> {
        self.connector.rebind(socket)
    }

    pub(crate) fn probe(
        &self,
        origin: &Uri,
        alternative: Authority,
        proxy: Option<&crate::proxy::Intercepted>,
        generation: u64,
    ) -> Option<RouteProbe> {
        let mut key = pool::extract_domain(origin).ok()?;
        key.2 = alternative;
        key.3 = proxy.map(pool::ProxyKey::new);
        let mut health = self.health.lock().unwrap();
        if health.generation != generation
            || health.probes.len() >= 64
            || !health.probes.insert(key.clone())
        {
            return None;
        }
        Some(RouteProbe {
            health: self.health.clone(),
            key,
            generation,
        })
    }

    pub(crate) fn retire_connection(&self, connection: &PoolClient) {
        self.pool.remove(&connection.key);
    }

    pub(crate) fn reject_connection(&self, connection: &PoolClient) {
        let mut health = self.health.lock().unwrap();
        if health.coalescing_rejected.len() < 256 {
            health.coalescing_rejected.insert(connection.key.clone());
        }
        self.pool.remove(&connection.key);
    }

    pub(crate) fn reject(
        &self,
        origin: &Uri,
        alternative: Authority,
        proxy: Option<&crate::proxy::Intercepted>,
    ) {
        if let Ok(mut key) = pool::extract_domain(origin) {
            key.2 = alternative.clone();
            key.3 = proxy.map(pool::ProxyKey::new);
            let mut health = self.health.lock().unwrap();
            if health.coalescing_rejected.len() < 256 {
                health.coalescing_rejected.insert(key);
            }
        }
        self.remove(origin, alternative, proxy);
    }

    pub(crate) fn remove(
        &self,
        origin: &Uri,
        alternative: Authority,
        proxy: Option<&crate::proxy::Intercepted>,
    ) {
        if let Ok(mut key) = pool::extract_domain(origin) {
            key.2 = alternative;
            key.3 = proxy.map(pool::ProxyKey::new);
            self.pool.remove(&key);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::atomic::{AtomicUsize, Ordering};
    use std::sync::Arc;

    #[test]
    fn stale_network_notifications_cannot_restore_an_old_generation() {
        struct Resolver(Arc<AtomicUsize>);
        impl crate::dns::Resolve for Resolver {
            fn resolve(&self, _: crate::dns::Name) -> crate::dns::Resolving {
                unreachable!("network invalidation does not resolve addresses")
            }
            fn clear_cache(&self) {
                self.0.fetch_add(1, Ordering::SeqCst);
            }
        }
        let calls = Arc::new(AtomicUsize::new(0));
        let tls = rustls::ClientConfig::builder_with_provider(Arc::new(
            rustls::crypto::aws_lc_rs::default_provider(),
        ))
        .with_protocol_versions(&[&rustls::version::TLS13])
        .unwrap()
        .with_root_certificates(rustls::RootCertStore::empty())
        .with_no_client_auth();
        let connector = H3Connector::new(
            crate::dns::DynResolver::new(Arc::new(Resolver(calls.clone()))),
            tls,
            None,
            Default::default(),
            Default::default(),
        )
        .unwrap();
        let mut client = H3Client::new(connector, None);
        assert!(client.network_generation(2));
        assert!(!client.network_generation(1));
        assert!(!client.network_generation(2));
        assert_eq!(calls.load(Ordering::SeqCst), 1);
        client.health.lock().unwrap().generation = u64::MAX;
        assert!(client.network_generation(0));
        assert!(!client.network_generation(u64::MAX));
        assert_eq!(calls.load(Ordering::SeqCst), 2);
    }
}