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
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
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
//! 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(all(feature = "high-level-url", feature = "proxy"))]
use crate::request::ProxyOverride;
#[cfg(feature = "high-level-url")]
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.
///
/// High-level only: the native frame API bypasses URL policy and pool-key
/// construction goes through [`crate::http_origin::HttpOrigin`] directly.
#[cfg(feature = "high-level-url")]
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::transport_hints::TransportHints,
    pub(super) proxied_target: Option<crate::transport_hints::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)
        }
    }
}

/// Whether the built-in proxy routes would apply to a native origin.
///
/// Mirrors [`resolve_proxy`] with `Inherit` but uses native
/// scheme/host/explicit-port components so the native frame API never
/// reparses its URI through `url::Url`. Shares the exact
/// `should_use_for_scheme` + `should_bypass_components` evaluation with the
/// high-level path.
#[cfg(feature = "proxy")]
pub(super) fn native_would_use_proxy(
    inner: &ClientInner,
    origin: &crate::http_origin::HttpOrigin,
    explicit_port: Option<u16>,
) -> bool {
    let scheme = origin.scheme_str();
    let host = origin.host();
    inner
        .config
        .proxy
        .iter()
        .chain(inner.config.environment_proxies.iter())
        .filter(|p| p.should_use_for_scheme(scheme))
        .any(|p| {
            p.no_proxy_rules()
                .is_none_or(|np| !np.should_bypass_components(scheme, host, explicit_port))
        })
}

/// Validate a `target` extension value for request smuggling safety.
///
/// Rejects C0 control characters and DEL bytes, plus percent-encoded NUL
/// (`%00`, any case), which passes the UTF-8 check but decodes to NUL on
/// the wire. 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(),
        ));
    }
    // Encoded NUL would survive the byte check above and the UTF-8 check
    // at the call site; reject `%00` explicitly.
    if target.windows(3).any(|w| w == b"%00") {
        return Err(Error::RequestBuild(
            "target extension must not contain encoded NUL (%00)".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.
///
/// High-level only; the native path uses [`resolve_native_request_uri`].
#[cfg(feature = "high-level-url")]
pub(super) fn resolve_request_uri(
    url: &url::Url,
    transport_hints: &crate::transport_hints::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 = crate::transport_hints::target_extension_str(target)?;
        // 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)
    }
}

/// Build the native wire URI from an already-validated `http::Uri`.
///
/// Returns the original absolute URI when no `TransportHints::target`
/// override is present. When `target` is present, validates it with the
/// shared request-smuggling guard and replaces only `path_and_query` while
/// preserving the original scheme/authority used for connector routing and
/// TLS. Host-header policy is unchanged. Shares [`validate_target`] with
/// the high-level helper so security checks cannot diverge.
pub(super) fn resolve_native_request_uri(
    uri: http::Uri,
    transport_hints: &crate::transport_hints::TransportHints,
) -> Result<http::Uri> {
    if let Some(ref target) = transport_hints.target {
        validate_target(target)?;
        let target_str = crate::transport_hints::target_extension_str(target)?;
        // Same wire-target logic as the high-level helper: Hyper's legacy
        // client requires an absolute URI for connector selection, then
        // converts to origin-form on the wire. Preserve `*` support.
        let target_uri = target_str.parse::<http::Uri>().ok();
        let path_and_query = target_uri
            .as_ref()
            .and_then(http::Uri::path_and_query)
            .cloned()
            .or_else(|| target_str.parse().ok())
            .ok_or_else(|| Error::InvalidUrl("failed to convert target to URI".into()))?;
        let mut parts = 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(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.
///
/// High-level only; requires the `high-level-url` feature.
#[cfg(feature = "high-level-url")]
#[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,
        #[cfg(feature = "redirects")]
            redirect: _request_redirect,
        auth: _,
        auth_disabled: _,
        decompress: request_decompress,
        proxy_override,
        #[cfg(feature = "logical-retry")]
            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 = "transport-http1", feature = "transport-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 {
        crate::http_origin::validate_url_effective_port(
            crate::http_origin::EffectivePortSubject::ResolvedDestination,
            &url,
            target.addresses(),
        )?;
    }

    if let Some(target) = &proxied_target {
        crate::http_origin::validate_url_effective_port(
            crate::http_origin::EffectivePortSubject::ProxiedDestination,
            &url,
            target.addresses(),
        )?;
    }

    #[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(not(feature = "advanced-routing"))]
        {
            return Err(Error::Unsupported(
                "caller-supplied resolved destinations require the advanced-routing feature".into(),
            ));
        }
        #[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, feature = "advanced-routing"))]
        if inner.uds_client.is_some() {
            return Err(Error::Unsupported(
                "caller-supplied resolved destinations are incompatible with Unix-domain routing"
                    .into(),
            ));
        }
    }

    #[cfg(not(feature = "advanced-routing"))]
    if transport_hints.sni_hostname.is_some() {
        return Err(Error::Unsupported(
            "SNI override requires the advanced-routing feature".into(),
        ));
    }

    #[cfg(feature = "advanced-routing")]
    if inner.config.dialer.is_some() {
        let arms = super::custom_dialing::CustomDialingState {
            has_resolved_target: transport_hints.resolved_target.is_some(),
            has_local_address_or_socket_options: inner.direct_connector_config.is_some(),
            has_uds: inner.config.uds_configured,
            #[cfg(feature = "http3")]
            is_http3_only: matches!(
                inner.config.http_version_policy,
                crate::HttpVersionPolicy::Http3Only
            ),
            #[cfg(not(feature = "http3"))]
            is_http3_only: false,
            // The built-in-proxy arm belongs to the dispatch boundary, which
            // runs after this phase; see `pipeline::send_single_request`.
            has_built_in_proxy: None,
        };
        if let Some(conflict) = super::custom_dialing::custom_dialing_conflict(arms) {
            return Err(conflict.error());
        }
    }

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

    let started = std::time::Instant::now();
    // Total wins ties (matching the body-deadline boundary): with equal
    // pool/total, report `Total` at acquire rather than `Pool`.
    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_uri(&method, &request_uri)?;

    let remaining_total = timeout
        .total
        .map(|total| total.saturating_sub(started.elapsed()));
    // Skip dispatch with no remaining total budget instead of spending a pool
    // slot and TCP connect only to fail on the deadline (matches retry loop).
    if remaining_total.is_some_and(|remaining| remaining.is_zero()) {
        return Err(Error::Timeout {
            phase: crate::timeout::TimeoutPhase::Total,
            elapsed: started.elapsed(),
        });
    }
    let deadline = timeout
        .total
        .map(|total| crate::timeout::saturating_add_instant(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::*;

    #[cfg(feature = "high-level-url")]
    #[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::transport_hints::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 native_target_override_keeps_logical_authority() {
        let uri: http::Uri = "https://example.com/original".parse().expect("valid URI");
        let hints = crate::transport_hints::TransportHints {
            target: Some(bytes::Bytes::from("/wire-path?q=1")),
            ..Default::default()
        };
        let resolved = resolve_native_request_uri(uri.clone(), &hints).expect("target resolves");
        assert_eq!(
            resolved
                .path_and_query()
                .map(http::uri::PathAndQuery::as_str),
            Some("/wire-path?q=1")
        );
        assert_eq!(resolved.host(), Some("example.com"));
        // No override returns the original URI unchanged.
        let plain = crate::transport_hints::TransportHints::default();
        assert_eq!(
            resolve_native_request_uri(uri.clone(), &plain).unwrap(),
            uri
        );
    }

    #[test]
    fn native_target_override_preserves_star_and_error_classification() {
        let uri: http::Uri = "https://example.com/original".parse().expect("valid URI");
        let star = crate::transport_hints::TransportHints {
            target: Some(bytes::Bytes::from_static(b"*")),
            ..Default::default()
        };
        let resolved = resolve_native_request_uri(uri.clone(), &star).expect("star resolves");
        assert_eq!(
            resolved
                .path_and_query()
                .map(http::uri::PathAndQuery::as_str),
            Some("*")
        );

        let invalid_utf8 = crate::transport_hints::TransportHints {
            target: Some(bytes::Bytes::from_static(b"/\xff")),
            ..Default::default()
        };
        assert!(matches!(
            resolve_native_request_uri(uri, &invalid_utf8),
            Err(Error::InvalidUrl(message)) if message.contains("not valid UTF-8")
        ));
    }

    #[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());
    }
}