eggfetch-core 0.1.6

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
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
//! Request preparation: header/body/version/limits normalization and pool
//! admission without transport I/O.
//!
//! Preparation owns effective decompression/body limits, proxy resolution
//! decisions, resolved-target compatibility checks, logical pool-key
//! construction/acquisition, write-timeout body wrapping, content length,
//! user-agent/accept-encoding, H2 forbidden-header policy, request
//! target/size validation, and remaining-total/deadline computation. It must
//! not perform transport I/O.

use std::sync::Arc;
use std::time::Duration;

use crate::body::RequestBody;
use crate::client::ClientInner;
use crate::error::{Error, Result};
use crate::headers::Headers;
use crate::pool::{OriginKey, PoolGuard};
#[cfg(feature = "proxy")]
use crate::proxy::{Proxy, ProxyConfig};
#[cfg(feature = "proxy")]
use crate::request::ProxyOverride;
use crate::request::Request;
use crate::stream::write_timeout_stream;
use crate::timeout::{Timeout, TimeoutPhase};

/// Transport-ready state after request policy and body/header normalization.
///
/// Built once in the preparation phase. Transport dispatch then selects an
/// execution path without reimplementing content-length, version, or header
/// policy. The body is owned so one-shot streams are moved, never cloned,
/// to satisfy the abstraction.
pub(super) struct PreparedRequest {
    pub(super) method: http::Method,
    pub(super) url: url::Url,
    pub(super) uri: http::Uri,
    pub(super) headers: Headers,
    pub(super) body: RequestBody,
    pub(super) version: http::Version,
    pub(super) transport_hints: crate::request::TransportHints,
    pub(super) proxied_target: Option<crate::request::ResolvedTarget>,
    #[cfg(feature = "proxy")]
    pub(super) effective_proxy: Option<ProxyConfig>,
    pub(super) decompression_enabled: bool,
    pub(super) max_decoded_body_size: Option<usize>,
    pub(super) max_decompression_ratio: Option<f64>,
    pub(super) timeout: Timeout,
    pub(super) remaining_total: Option<Duration>,
    pub(super) deadline: Option<std::time::Instant>,
    pub(super) failure_context: Option<Arc<crate::error::RequestFailureContext>>,
}

/// Apply `Content-Length` header to known-size request bodies when the
/// user has not provided one. For known-size bodies with a user-supplied
/// `Content-Length`, reject mismatches.
pub(crate) fn apply_content_length(headers: Headers, body: &RequestBody) -> Result<Headers> {
    let known =
        match body {
            RequestBody::Empty => Some(0u64),
            RequestBody::Bytes(b) => Some(u64::try_from(b.len()).map_err(|_| {
                Error::RequestBuild("request body length does not fit in u64".into())
            })?),
            RequestBody::Stream {
                length: Some(n), ..
            } => Some(u64::try_from(*n).map_err(|_| {
                Error::RequestBuild("request body length does not fit in u64".into())
            })?),
            RequestBody::Stream { length: None, .. } => None,
        };

    let supplied = headers.get("content-length").map(|value| {
        value
            .to_str()
            .map_err(|e| Error::InvalidHeaderValue(format!("invalid Content-Length: {e}")))?
            .parse::<u64>()
            .map_err(|e| Error::InvalidHeaderValue(format!("invalid Content-Length: {e}")))
    });
    let supplied = supplied.transpose()?;

    if let Some(known_len) = known {
        if let Some(user_len) = supplied {
            if user_len != known_len {
                return Err(Error::RequestBuild(format!(
                    "Content-Length mismatch: user supplied {user_len} but body is {known_len}"
                )));
            }
        } else {
            let mut h = headers;
            h.insert("content-length", &known_len.to_string())?;
            return Ok(h);
        }
    } else if supplied.is_some() {
        return Err(Error::RequestBuild(
            "Content-Length cannot be supplied for an unknown-length stream body".into(),
        ));
    }

    Ok(headers)
}

/// Resolve the effective proxy configuration for a request.
///
/// Applies the tri-state override model:
/// - `Inherit`: use client-level proxy
/// - `Direct`: direct, no proxy
/// - `Override(config)`: use request-level proxy
#[cfg(feature = "proxy")]
pub(super) fn resolve_proxy(
    inner: &ClientInner,
    url: &url::Url,
    proxy_override: &ProxyOverride,
) -> Option<ProxyConfig> {
    match proxy_override {
        ProxyOverride::Override(config) => Some(config.clone()),
        ProxyOverride::Direct => None,
        ProxyOverride::Inherit => {
            let candidates = inner
                .config
                .proxy
                .iter()
                .chain(inner.config.environment_proxies.iter());
            candidates
                .filter(|p| p.should_use_for_scheme(url.scheme()))
                .find(|p| p.no_proxy_rules().is_none_or(|np| !np.should_bypass(url)))
                .map(Proxy::config)
        }
    }
}

/// Validate a `target` extension value for request smuggling safety.
///
/// Rejects C0 control characters and DEL bytes. The target must also be
/// valid UTF-8 because it is converted to Hyper's text-based URI type before
/// dispatch; callers should percent-encode non-ASCII octets when necessary.
pub(crate) fn validate_target(target: &[u8]) -> Result<()> {
    if target.is_empty() {
        return Err(Error::RequestBuild(
            "target extension must not be empty".into(),
        ));
    }
    if target.iter().any(|&b| b < 0x20 || b == 0x7f) {
        return Err(Error::RequestBuild(
            "target extension contains forbidden characters (C0 controls/DEL; includes CR/LF/NUL)"
                .into(),
        ));
    }
    // Reject whitespace-only or leading/trailing whitespace targets that would
    // otherwise pass the C0 check (space 0x20) and produce a late `InvalidUrl`
    // or bypass proxy routing checks.
    if target.iter().all(|&b| b == b' ') {
        return Err(Error::RequestBuild(
            "target extension must not be whitespace-only".into(),
        ));
    }
    if target.first() == Some(&b' ') || target.last() == Some(&b' ') {
        return Err(Error::RequestBuild(
            "target extension must not contain leading or trailing whitespace".into(),
        ));
    }
    Ok(())
}

/// Build the HTTP request URI, applying `target` transport hint if present.
///
/// When `target` is set, the wire URI is overridden while the logical URL
/// is preserved for routing, Host header, cookies, and auth decisions.
pub(super) fn resolve_request_uri(
    url: &url::Url,
    transport_hints: &crate::request::TransportHints,
) -> Result<http::Uri> {
    let logical_uri: http::Uri = url
        .as_str()
        .parse()
        .map_err(|e| Error::InvalidUrl(format!("failed to convert url to URI: {e}")))?;
    if let Some(ref target) = transport_hints.target {
        validate_target(target)?;
        let target = std::str::from_utf8(target)
            .map_err(|_| Error::InvalidUrl("target extension is not valid UTF-8".into()))?;
        // Hyper's legacy client requires an absolute URI to select a
        // connector, then converts that URI to origin-form on the wire. Keep
        // the logical scheme/authority for connector routing and TLS while
        // replacing only the path-and-query portion with the caller's wire
        // target. This also preserves the `*` request target.
        let target_uri = target.parse::<http::Uri>().ok();
        let path_and_query = target_uri
            .as_ref()
            .and_then(http::Uri::path_and_query)
            .cloned()
            .or_else(|| target.parse().ok())
            .ok_or_else(|| Error::InvalidUrl("failed to convert target to URI".into()))?;
        let mut parts = logical_uri.into_parts();
        parts.path_and_query = Some(path_and_query);
        http::Uri::from_parts(parts)
            .map_err(|e| Error::InvalidUrl(format!("failed to convert target to URI: {e}")))
    } else {
        Ok(logical_uri)
    }
}

/// Preparation phase for single-request dispatch: normalize headers, body,
/// version, and proxy/pool/timeout state into a transport-ready form.
///
/// Centralizes content-length, user-agent, accept-encoding, H2 header, and
/// request-size policy so transport modules own only connection/protocol
/// work. The pool guard is acquired here and returned alongside the prepared
/// request; the caller attaches it to the response body after transport
/// completes.
///
/// # Errors
///
/// Returns an error for TLS misconfiguration, proxy origin resolution,
/// pool timeouts, content-length mismatches, or oversized requests.
#[allow(
    clippy::too_many_lines,
    reason = "preparation centralizes header/body/version/proxy/pool policy in one place so transports do not reimplement it"
)]
pub(super) async fn prepare_single_request(
    inner: &ClientInner,
    request: Request,
    timeout: &Timeout,
) -> Result<(PreparedRequest, PoolGuard)> {
    // Destructure exhaustively so new `RequestParts` fields fail to compile
    // here rather than being silently dropped before transport.
    let crate::request::RequestParts {
        method,
        url,
        headers,
        body,
        version,
        timeout: _request_timeout,
        redirect: _request_redirect,
        auth: _,
        auth_disabled: _,
        decompress: request_decompress,
        proxy_override,
        retry: _,
        transport_hints,
        proxied_target,
        failure_context,
        max_decoded_body_size: request_max_decoded_body_size,
        max_decompression_ratio: request_max_decompression_ratio,
    } = request.into_parts();

    #[cfg(any(feature = "http1", feature = "http2"))]
    if inner.lifecycle.invalid {
        return Err(Error::RequestBuild(
            "physical connection policy requires max_live > 0 and admission_timeout only with max_live"
                .into(),
        ));
    }

    if let Some(target) = &transport_hints.resolved_target {
        let expected_port = url.port_or_known_default().ok_or_else(|| {
            Error::InvalidResolvedTarget(
                "resolved destinations require an HTTP or HTTPS URL".into(),
            )
        })?;
        if target
            .addresses()
            .iter()
            .any(|address| address.port() != expected_port)
        {
            return Err(Error::InvalidResolvedTarget(format!(
                "all resolved destinations must use the URL's effective port {expected_port}"
            )));
        }
    }

    if let Some(target) = &proxied_target {
        let expected_port = url.port_or_known_default().ok_or_else(|| {
            Error::InvalidResolvedTarget("proxied destinations require an HTTP or HTTPS URL".into())
        })?;
        if target
            .addresses()
            .iter()
            .any(|address| address.port() != expected_port)
        {
            return Err(Error::InvalidResolvedTarget(format!(
                "all proxied destinations must use the URL's effective port {expected_port}"
            )));
        }
    }

    #[cfg(feature = "tls-rustls")]
    if url.scheme() == "https" {
        if let Some(error) = &inner.tls_config_error {
            return Err(Error::Tls(error.clone()));
        }
    }

    let decompression_enabled = request_decompress.unwrap_or(inner.config.automatic_decompression);
    let max_decoded_body_size =
        request_max_decoded_body_size.or(inner.config.max_decoded_body_size);
    let max_decompression_ratio =
        request_max_decompression_ratio.or(inner.config.max_decompression_ratio);

    let mut headers = headers;
    if decompression_enabled && !headers.contains("accept-encoding") {
        if let Some(value) = crate::compression::accept_encoding_value() {
            headers.insert("accept-encoding", value)?;
        }
    }

    #[cfg(feature = "proxy")]
    let effective_proxy = resolve_proxy(inner, &url, &proxy_override);
    #[cfg(not(feature = "proxy"))]
    {
        let _ = proxy_override;
    }

    if proxied_target.is_some() {
        #[cfg(feature = "proxy")]
        {
            let proxy = effective_proxy.as_ref().ok_or_else(|| {
                Error::Unsupported(
                    "caller-supplied proxied destinations require proxy routing".into(),
                )
            })?;
            if proxy.socks_remote_dns() {
                return Err(Error::Unsupported(
                    "proxied destinations are incompatible with SOCKS5H remote DNS".into(),
                ));
            }
            if url.scheme() == "http" && !proxy.is_socks() {
                return Err(Error::Unsupported(
                    "proxied destinations are unsupported for plaintext HTTP forward proxies"
                        .into(),
                ));
            }
        }
        #[cfg(not(feature = "proxy"))]
        {
            return Err(Error::Unsupported(
                "caller-supplied proxied destinations require proxy support".into(),
            ));
        }
    }

    if transport_hints.resolved_target.is_some() {
        #[cfg(feature = "proxy")]
        if effective_proxy.is_some() {
            return Err(Error::Unsupported(
                "caller-supplied resolved destinations require direct routing; disable the proxy explicitly".into(),
            ));
        }
        #[cfg(all(unix, any(feature = "http1", feature = "http2")))]
        if inner.uds_client.is_some() {
            return Err(Error::Unsupported(
                "caller-supplied resolved destinations are incompatible with Unix-domain routing"
                    .into(),
            ));
        }
    }

    if inner.config.dialer.is_some() {
        if transport_hints.resolved_target.is_some() {
            return Err(Error::Unsupported(
                "custom dialing is incompatible with caller-supplied resolved destinations".into(),
            ));
        }
        #[cfg(any(feature = "http1", feature = "http2"))]
        if inner.direct_connector_config.is_some() {
            return Err(Error::Unsupported(
                "custom dialing is incompatible with local-address or socket-option routing".into(),
            ));
        }
        if inner.config.uds_configured {
            return Err(Error::Unsupported(
                "custom dialing is incompatible with Unix-domain routing".into(),
            ));
        }
        #[cfg(feature = "http3")]
        if matches!(
            inner.config.http_version_policy,
            crate::HttpVersionPolicy::Http3Only
        ) {
            return Err(Error::Unsupported(
                "custom dialing is incompatible with HTTP/3".into(),
            ));
        }
    }

    #[cfg(feature = "proxy")]
    let origin = match effective_proxy {
        Some(ref proxy_config) => {
            let is_tunnel = url.scheme() == "https";
            OriginKey::from_url_with_proxy_scheme(
                url.scheme(),
                &url,
                proxy_config.host(),
                Some(proxy_config.port()?),
                Some(proxy_config.scheme()),
                is_tunnel,
            )
        }
        None => OriginKey::from_url(url.scheme(), &url),
    };
    #[cfg(not(feature = "proxy"))]
    let origin = OriginKey::from_url(url.scheme(), &url);

    let started = std::time::Instant::now();
    let pool_deadline = match (timeout.pool, timeout.total) {
        (Some(pool), Some(total)) if total < pool => Some((total, TimeoutPhase::Total)),
        (Some(pool), _) => Some((pool, TimeoutPhase::Pool)),
        (None, Some(total)) => Some((total, TimeoutPhase::Total)),
        (None, None) => None,
    };
    let guard = match pool_deadline {
        Some((duration, phase)) => {
            match tokio::time::timeout(duration, inner.pool.acquire(origin.as_ref())).await {
                Ok(guard) => guard?,
                Err(_) => {
                    return Err(Error::Timeout {
                        phase,
                        elapsed: started.elapsed(),
                    })
                }
            }
        }
        None => inner.pool.acquire(origin.as_ref()).await?,
    };

    let body = match (body, timeout.write) {
        (RequestBody::Stream { stream, length }, Some(write_dur)) => {
            let wrapped = write_timeout_stream(stream, write_dur);
            RequestBody::Stream {
                stream: wrapped,
                length,
            }
        }
        (b, _) => b,
    };

    let headers = apply_content_length(headers, &body)?;

    let mut headers = headers;
    if let Some(ref ua) = inner.config.user_agent {
        if !headers.contains("user-agent") {
            headers.insert(http::header::USER_AGENT.as_str(), ua.as_str())?;
        }
    }

    let enabler = crate::http_version::HttpVersionPolicyEnabler::from_policy(
        inner.config.http_version_policy,
    );
    // Only strip H2-forbidden headers when the request is known to use
    // HTTP/2 (explicit version or H2-only policy). For `Auto`, ALPN
    // negotiation is per-connection; stripping here would drop
    // `Connection: close` etc. on H1.1. Hyper will enforce H2 header
    // validity if `Auto` negotiates h2.
    if version == http::Version::HTTP_2 || (!enabler.enable_http1() && enabler.enable_http2()) {
        crate::h2_headers::strip_h2_forbidden_headers(&mut headers);
    }
    let request_uri = resolve_request_uri(&url, &transport_hints)?;
    headers.validate_request_size(&method, request_uri.to_string().as_bytes())?;

    let remaining_total = timeout
        .total
        .map(|total| total.saturating_sub(started.elapsed()));
    let deadline = timeout.total.map(|total| started + total);

    let prepared = PreparedRequest {
        method,
        url,
        uri: request_uri,
        headers,
        body,
        version,
        transport_hints,
        proxied_target,
        #[cfg(feature = "proxy")]
        effective_proxy,
        decompression_enabled,
        max_decoded_body_size,
        max_decompression_ratio,
        timeout: *timeout,
        remaining_total,
        deadline,
        failure_context,
    };
    Ok((prepared, guard))
}

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

    #[test]
    fn target_override_keeps_logical_authority_for_routing() {
        // The wire URI carries the caller's path while the logical URL
        // remains authoritative for routing/TLS/Host decisions upstream.
        let url = url::Url::parse("https://example.com/original").expect("valid URL");
        let hints = crate::request::TransportHints {
            target: Some(bytes::Bytes::from("/wire-path?q=1")),
            ..Default::default()
        };
        let uri = resolve_request_uri(&url, &hints).expect("target resolves");
        assert_eq!(
            uri.path_and_query().map(http::uri::PathAndQuery::as_str),
            Some("/wire-path?q=1")
        );
        assert_eq!(uri.host(), Some("example.com"));
    }

    #[test]
    fn validate_target_rejects_smuggling_vectors() {
        assert!(validate_target(b"/ok-path").is_ok());
        assert!(validate_target(b"").is_err());
        assert!(validate_target(b"/bad\r\n").is_err());
        assert!(validate_target(b"   ").is_err());
        assert!(validate_target(b" /leading").is_err());
    }
}