eggfetch-core 0.3.0

Async-first Rust HTTP client engine (tokio + hyper) with connection pooling, phase-aware timeouts, TLS, proxies, and compression
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
//! Private route identity and cached client acquisition.

#[cfg(feature = "advanced-routing")]
use std::sync::Arc;
#[cfg(all(
    feature = "proxy",
    any(feature = "transport-http1", feature = "transport-http2")
))]
use std::time::Duration;

#[cfg(feature = "tls-rustls")]
use super::configure_tls_alpn;
#[cfg(any(feature = "advanced-routing", feature = "proxy"))]
use super::{ClientConfig, Error, Pool};
use super::{ClientInner, Result};
#[cfg(any(
    feature = "transport-http1",
    feature = "transport-http2",
    feature = "advanced-routing",
    feature = "proxy"
))]
use crate::transport::hyper_client::{build_hyper_client, HyperClientPolicy};
#[cfg(any(
    feature = "transport-http1",
    feature = "transport-http2",
    feature = "advanced-routing"
))]
use crate::transport::lifecycle::LifecycleConfig;

/// Internal cache identity for one direct resolved-target route.
///
/// The type deliberately does not implement `Debug` or `Display`: route keys
/// carry connection identity only and must never render request state in
/// diagnostics. Compare with `==`/`!=` in tests.
///
/// Connection-scoped: normalized logical HTTP origin (scheme, host, effective
/// port as `scheme://host:port` with an always-explicit port and no
/// path/query/fragment), the full ordered physical address snapshot (order
/// controls attempt/failover order, so reordering fragments identity), and
/// the exact optional SNI override (no normalization: a redundant miss is
/// safe, a false hit is a security defect). TLS config/provider/trust
/// policy, HTTP version/ALPN policy, direct socket/local-address
/// configuration, connect timeout, lifecycle policy, canceled-request retry
/// behavior, and idle-pool policy are immutable at `ClientInner` scope after
/// `ClientBuilder::build` and therefore stay outside the per-route key.
/// Request-scoped state (total/read/write/pool deadlines, retry/redirect
/// state, body, cookies/auth headers, decompression limits, trace observers,
/// failure contexts) is never represented. If any currently client-wide
/// connection-affecting field becomes request-scoped, this key must be
/// expanded before the new variability may use the cache.
#[cfg(feature = "advanced-routing")]
#[derive(Clone, PartialEq, Eq, Hash)]
pub(crate) struct ResolvedRouteKey {
    origin: String,
    addresses: Arc<[std::net::SocketAddr]>,
    sni_hostname: Option<String>,
}

#[cfg(feature = "advanced-routing")]
impl ResolvedRouteKey {
    /// Canonical constructor from a native origin.
    pub(crate) fn from_origin(
        origin: &crate::http_origin::HttpOrigin,
        target: &crate::transport_hints::ResolvedTarget,
        sni_hostname: Option<&str>,
    ) -> Self {
        Self {
            origin: origin.origin_string(),
            addresses: target.addresses_shared(),
            sni_hostname: sni_hostname.map(str::to_owned),
        }
    }

    /// High-level adapter from `url::Url`; reduces to the same canonical
    /// `scheme://host:port` representation as [`Self::from_origin`].
    ///
    /// Test-only: production high-level routes convert via
    /// `HttpOrigin::from_url` + `from_origin` so pool and route identity
    /// share one canonical constructor.
    #[cfg(all(feature = "high-level-url", any(test, feature = "test-util")))]
    #[allow(
        dead_code,
        reason = "the high-level adapter is retained as the test-util entry point for the canonical origin form; the current tree has no caller for it, so the allow keeps the retained constructor warning-free"
    )]
    pub(crate) fn new(
        origin_url: &url::Url,
        target: &crate::transport_hints::ResolvedTarget,
        sni_hostname: Option<&str>,
    ) -> Self {
        match crate::http_origin::HttpOrigin::from_url(origin_url) {
            Some(origin) => Self::from_origin(&origin, target, sni_hostname),
            None => Self {
                // Fall back to the legacy serialization for non-HTTP(S) or
                // hostless URLs; such routes fail closed before I/O.
                origin: origin_url.origin().ascii_serialization(),
                addresses: target.addresses_shared(),
                sni_hostname: sni_hostname.map(str::to_owned),
            },
        }
    }
}

/// How a direct Hyper client binds its logical destination.
///
/// The resolved-target route pins the physical addresses and may additionally
/// override the TLS identity; the SNI route leaves DNS and TCP to the URL
/// host and only overrides the TLS identity. Everything else — TLS
/// materialization, connect timeout, metrics, idle-pool policy, and lifecycle
/// policy — is identical for both.
#[cfg(feature = "advanced-routing")]
#[derive(Debug, Clone, Copy)]
enum DirectRouteBinding<'a> {
    /// Bind a caller-supplied resolved address snapshot, with an optional SNI
    /// override on top of it.
    Resolved {
        /// The pinned destination addresses.
        target: &'a crate::transport_hints::ResolvedTarget,
        /// The TLS identity override, when the route carries one.
        sni_hostname: Option<&'a str>,
    },
    /// Bind a logical SNI hostname; DNS and TCP still resolve the URL host.
    Sni {
        /// The TLS identity override.
        sni_hostname: &'a str,
    },
}

/// Build a direct Hyper client for one cached route, without touching the
/// route cache.
///
/// CPU/local configuration only (connector + TLS policy + Hyper builder);
/// performs no network I/O, so callers may hold the route-cache mutex while
/// building, matching the documented route-cache locking contract.
/// Construction failures return before insert and never poison the cache.
#[cfg(feature = "advanced-routing")]
fn build_direct_client(
    config: &ClientConfig,
    direct_connector_config: Option<&crate::transport::direct_connector::DirectConnectorConfig>,
    transport_metrics: &Arc<crate::transport::metrics::TransportMetrics>,
    pool: &Pool,
    lifecycle: &Arc<LifecycleConfig>,
    binding: DirectRouteBinding<'_>,
) -> Result<crate::transport::TimeoutDirectClient> {
    let connect_timeout = config.timeout.as_ref().and_then(|t| t.connect);
    let enabler =
        crate::http_version::HttpVersionPolicyEnabler::from_policy(config.http_version_policy);
    #[cfg(feature = "tls-rustls")]
    let tls_connector = {
        let tls_config = config.tls_config.clone().unwrap_or_default();
        let rustls_config = tls_config
            .build_rustls_config()
            .map_err(|e| Error::Tls(format!("failed to build TLS config: {e}")))?;
        let rustls_config = configure_tls_alpn(rustls_config, enabler);
        Some(tokio_rustls::TlsConnector::from(Arc::new(rustls_config)))
    };
    #[cfg(not(feature = "tls-rustls"))]
    let tls_connector = None;

    let base_config = direct_connector_config.cloned().unwrap_or(
        crate::transport::direct_connector::DirectConnectorConfig {
            local_address: None,
            socket_options: Vec::new(),
        },
    );
    let mut connector = crate::transport::direct_connector::DirectConnector::with_metrics(
        base_config,
        tls_connector,
        transport_metrics.clone(),
    );
    let sni_hostname = match binding {
        DirectRouteBinding::Resolved {
            target,
            sni_hostname,
        } => {
            connector = connector.with_resolved_target(target);
            sni_hostname
        }
        DirectRouteBinding::Sni { sni_hostname } => Some(sni_hostname),
    };
    if let Some(sni_hostname) = sni_hostname {
        connector = connector.with_sni(sni_hostname.to_owned());
    }
    // Idle-connection lifetime comes from the pool configuration
    // (`Limits::keepalive_expiry`), matching the standard, direct,
    // and UDS paths. `Timeout.pool`/`total` are acquisition budgets
    // and must not close idle connections early. The effective
    // per-host idle cap is shared as well; see `Pool::max_idle_per_host`.
    let policy = HyperClientPolicy::new(
        config.retry_canceled_requests,
        pool.idle_timeout(),
        pool.max_idle_per_host(),
        enabler,
    );
    Ok(build_hyper_client(
        connector,
        &policy,
        connect_timeout,
        lifecycle,
    ))
}

impl ClientInner {
    /// Get or create a cached Hyper client for one direct resolved-target
    /// route.
    ///
    /// The cache key is the normalized logical origin plus the full ordered
    /// physical address snapshot plus the exact SNI override. Identical route
    /// identity reuses the configured Hyper client (and therefore Hyper-owned
    /// H1 keep-alive / H2 multiplexed connections); any change in those
    /// dimensions selects a different entry. Ordinary DNS/direct clients never
    /// share these entries.
    #[cfg(feature = "advanced-routing")]
    pub(crate) async fn resolved_client(
        &self,
        origin: &crate::http_origin::HttpOrigin,
        target: &crate::transport_hints::ResolvedTarget,
        sni_hostname: Option<&str>,
    ) -> Result<crate::transport::TimeoutDirectClient> {
        let key = ResolvedRouteKey::from_origin(origin, target, sni_hostname);
        let mut clients = self.resolved_clients.lock().await;
        if let Some(client) = clients.get(&key) {
            return Ok(client.clone());
        }
        let client = build_direct_client(
            &self.config,
            self.direct_connector_config.as_ref(),
            &self.transport_metrics,
            &self.pool,
            &self.lifecycle,
            DirectRouteBinding::Resolved {
                target,
                sni_hostname,
            },
        )?;
        clients.insert(key, client.clone());
        Ok(client)
    }

    /// Get or create a cached hyper client with TLS SNI hostname override.
    ///
    /// The returned client uses a [`DirectConnector`](crate::transport::direct_connector::DirectConnector)
    /// that separates DNS/TCP resolution (to the original URL host) from
    /// TLS negotiation (with the SNI hostname). Clients are cached by
    /// SNI hostname for connection reuse.
    #[cfg(feature = "advanced-routing")]
    pub(crate) async fn sni_client(
        &self,
        sni_hostname: &str,
    ) -> Result<crate::transport::TimeoutDirectClient> {
        let mut clients = self.sni_clients.lock().await;
        if let Some(client) = clients.get(sni_hostname) {
            return Ok(client.clone());
        }

        let client = build_direct_client(
            &self.config,
            self.direct_connector_config.as_ref(),
            &self.transport_metrics,
            &self.pool,
            &self.lifecycle,
            DirectRouteBinding::Sni { sni_hostname },
        )?;
        clients.insert(sni_hostname.to_owned(), client.clone());
        Ok(client)
    }

    /// Get or create a custom-dialer Hyper client with a fixed SNI override.
    #[cfg(feature = "advanced-routing")]
    pub(crate) async fn sni_custom_client(
        &self,
        sni_hostname: &str,
    ) -> Result<crate::transport::TimeoutCustomClient> {
        let mut clients = self.sni_custom_clients.lock().await;
        if let Some(client) = clients.get(sni_hostname) {
            return Ok(client.clone());
        }
        let dialer = self
            .config
            .dialer
            .clone()
            .ok_or_else(|| Error::Unsupported("custom dialer is not configured".into()))?;
        #[cfg(feature = "tls-rustls")]
        let tls_config = self
            .config
            .tls_config
            .clone()
            .unwrap_or_default()
            .build_rustls_config()
            .map_err(|error| Error::Tls(format!("failed to build TLS config: {error}")))?;
        #[cfg(not(feature = "tls-rustls"))]
        let tls_config = ();
        let connector = super::connectors::build_custom_connector(
            tls_config,
            dialer,
            crate::http_version::HttpVersionPolicyEnabler::from_policy(
                self.config.http_version_policy,
            ),
            Some(sni_hostname),
        )?;
        let policy = HyperClientPolicy::new(
            self.config.retry_canceled_requests,
            self.pool.idle_timeout(),
            self.pool.max_idle_per_host(),
            crate::http_version::HttpVersionPolicyEnabler::from_policy(
                self.config.http_version_policy,
            ),
        );
        let client = build_hyper_client(
            connector,
            &policy,
            self.config
                .timeout
                .as_ref()
                .and_then(|timeout| timeout.connect),
            &self.lifecycle,
        );
        clients.insert(sni_hostname.to_owned(), client.clone());
        Ok(client)
    }
}

#[cfg(feature = "proxy")]
impl ClientInner {
    pub(crate) async fn socks_client(
        &self,
        proxy: &crate::proxy::ProxyConfig,
        target: Option<&crate::transport_hints::ResolvedTarget>,
    ) -> Result<crate::transport::TimeoutSocksClient> {
        let key = crate::transport::socks::SocksRouteKey::from_proxy(proxy, target)?;
        let mut clients = self.socks_clients.lock().await;
        if let Some(client) = clients.get(&key) {
            return Ok(client.clone());
        }

        let enabler = crate::http_version::HttpVersionPolicyEnabler::from_policy(
            self.config.http_version_policy,
        );

        let tls_config = if let Some(config) = self.config.tls_config.as_ref() {
            config
                .build_rustls_config()
                .map_err(|e| Error::Tls(format!("failed to build SOCKS TLS config: {e}")))?
        } else {
            crate::tls::TlsConfig::default()
                .build_rustls_config()
                .map_err(|e| Error::Tls(format!("failed to build default SOCKS TLS config: {e}")))?
        };
        let tls_config = configure_tls_alpn(tls_config, enabler);
        let connector = crate::transport::socks::SocksConnector::new(
            proxy.clone(),
            Some(tokio_rustls::TlsConnector::from(Arc::new(tls_config))),
            None,
            target,
        );
        // SOCKS route clients share the resolved Hyper idle-pool policy
        // (timeout plus effective per-host cap) with every other persistent
        // Hyper family. `HyperClientPolicy::apply` installs the pool timer
        // the timeout requires.
        let policy = HyperClientPolicy::new(
            self.config.retry_canceled_requests,
            self.pool.idle_timeout(),
            self.pool.max_idle_per_host(),
            enabler,
        );
        let client = build_hyper_client(
            connector,
            &policy,
            self.config
                .timeout
                .as_ref()
                .and_then(|timeout| timeout.connect),
            &self.lifecycle,
        );
        clients.insert(key, client.clone());
        Ok(client)
    }

    /// Get or create a Hyper client for one HTTP forward-proxy route.
    ///
    /// The client is scoped to the destination origin rather than sharing a
    /// pool across arbitrary origins. Hyper still owns keep-alive handling,
    /// framing, response bodies, and stale-idle recovery for each entry.
    #[cfg(any(feature = "transport-http1", feature = "transport-http2"))]
    pub(crate) async fn forward_client(
        &self,
        proxy: &crate::proxy::ProxyConfig,
        origin: &url::Url,
        connect_timeout: Option<Duration>,
    ) -> Result<crate::transport::TimeoutForwardClient> {
        let proxy_tls_timeout = connect_timeout;
        let key = crate::transport::proxy::ForwardRouteKey::new(
            proxy,
            origin,
            connect_timeout,
            proxy_tls_timeout,
        );
        let mut clients = self.forward_clients.lock().await;
        if let Some(client) = clients.get(&key) {
            return Ok(client.clone());
        }

        let connector = crate::transport::proxy::ForwardProxyConnector::new(
            proxy.clone(),
            connect_timeout,
            proxy_tls_timeout,
            self.transport_metrics.clone(),
        );
        let policy = HyperClientPolicy::forward_route(
            self.config.retry_canceled_requests,
            self.pool.idle_timeout(),
            self.pool.max_idle_per_host(),
        );
        let client = build_hyper_client(connector, &policy, connect_timeout, &self.lifecycle);
        clients.insert(key, client.clone());
        Ok(client)
    }

    /// Get or create a Hyper client for one compatible HTTPS CONNECT route.
    /// Multi-address target snapshots remain on the legacy path so their
    /// typed 502/504 fallback semantics are not weakened.
    #[cfg(any(feature = "transport-http1", feature = "transport-http2"))]
    pub(crate) async fn connect_client(
        &self,
        proxy: &crate::proxy::ProxyConfig,
        origin: &url::Url,
        transport_hints: &crate::transport_hints::TransportHints,
        target: Option<std::net::SocketAddr>,
        connect_timeout: Option<Duration>,
    ) -> Result<crate::transport::TimeoutConnectClient> {
        let key = crate::transport::connect::ConnectRouteKey::new(
            proxy,
            origin,
            target,
            self.config.tls_config.as_ref(),
            transport_hints.sni_hostname.as_deref(),
            self.config.http_version_policy,
            connect_timeout,
            connect_timeout,
        );
        let mut clients = self.connect_clients.lock().await;
        if let Some(client) = clients.get(&key) {
            return Ok(client.clone());
        }
        let connector = crate::transport::connect::ConnectProxyConnector::new(
            origin.clone(),
            proxy.clone(),
            transport_hints,
            target,
            self.config.tls_config.clone(),
            connect_timeout,
            connect_timeout,
            connect_timeout,
            self.config.http_version_policy,
            self.transport_metrics.clone(),
        );
        let policy = HyperClientPolicy::new(
            self.config.retry_canceled_requests,
            self.pool.idle_timeout(),
            self.pool.max_idle_per_host(),
            crate::http_version::HttpVersionPolicyEnabler::from_policy(
                self.config.http_version_policy,
            ),
        );
        let client = build_hyper_client(connector, &policy, connect_timeout, &self.lifecycle);
        clients.insert(key, client.clone());
        Ok(client)
    }
}