Skip to main content

cpa_exec/
proxy.rs

1//! Proxy-aware HTTP clients with Go net/http semantics, shared by every executor
2//! (helps/proxy_helpers.go, sdk/proxyutil, net/http client behaviour; M4-0029).
3//!
4//! - [`Proxy`]: the effective proxy for a request (credential, then
5//!   `requests.proxy-url`), parsed like `proxyutil.Parse`.
6//! - [`GoClients`]: clients keyed by effective proxy in a bounded 64-entry LRU, with
7//!   Go's standard-transport proxy rules (empty inherits environment proxies, `direct`
8//!   and `none` bypass them, an unusable URL falls back to the default transport).
9//! - [`send`] and [`GoHeaders`]: one exchange the way `http.Client.Do` performs it:
10//!   HTTP/1.1 header line order, custom Host, manual redirects with Referer and
11//!   credential stripping, and transparent gzip only when Go would have asked for it.
12//! - [`lines`]: `bufio.Scanner` line semantics for event streams.
13//!
14//! Claude's first-party Node/OpenSSL profile lives in tls.rs; it uses [`Proxy`] and
15//! [`Hooks`] from here so both kinds of client share proxy and test routing.
16//!
17//! ponytail: no request-scoped proxy override (Go `RequestProxyURL` / `opts.ProxyURL`);
18//! ExecRequest carries none. Add a field to the execution envelope when a route needs it.
19
20use cpa_core::gostr::GoStr;
21use std::net::SocketAddr;
22use std::sync::{Arc, Mutex};
23
24use bytes::Bytes;
25use cpa_core::config::{Config, go_url};
26use cpa_core::credential::Credential;
27use cpa_core::exec::{ExecError, FailureScope, UpstreamStage, UsageSink};
28use futures_util::StreamExt;
29use futures_util::stream::BoxStream;
30use http::HeaderMap;
31use wreq::tls::trust::CertStore;
32
33/// Bounded like Go's executor transport caches.
34pub(crate) const CACHE_CAPACITY: usize = 64;
35
36/// Test-only routing: extra trust roots replace the default store, and resolve
37/// overrides pin logical hosts to local addresses while URL, Host and SNI stay
38/// unchanged. Production uses [`Hooks::default`].
39#[derive(Clone, Default)]
40pub struct Hooks {
41    pub trust: Option<CertStore>,
42    pub resolve: Vec<(String, SocketAddr)>,
43}
44
45impl Hooks {
46    pub(crate) fn apply(&self, mut builder: wreq::ClientBuilder) -> wreq::ClientBuilder {
47        if let Some(trust) = &self.trust {
48            builder = builder.tls_cert_store(trust.clone());
49        }
50        for (host, addr) in &self.resolve {
51            builder = builder.resolve(host.clone(), *addr);
52        }
53        builder
54    }
55}
56
57/// Effective proxy for one request (`effectiveProxyURL` + `proxyutil.Parse`).
58#[derive(Debug, Clone, PartialEq, Eq, Hash)]
59pub enum Proxy {
60    /// Nothing configured.
61    Inherit,
62    /// `direct` / `none`: bypass every proxy, including environment ones.
63    Direct,
64    /// http, https, socks5 or socks5h URL, as configured.
65    Url(String),
66    /// Unparseable or unsupported: Go logs it and behaves as if none were configured.
67    Invalid,
68}
69
70impl Proxy {
71    pub fn parse(raw: &str) -> Self {
72        let raw = raw.trim();
73        if raw.is_empty() {
74            return Self::Inherit;
75        }
76        if raw.eq_ignore_ascii_case("direct") || raw.eq_ignore_ascii_case("none") {
77            return Self::Direct;
78        }
79        match url::Url::parse(raw) {
80            Ok(u)
81                if u.host_str().is_some_and(|h| !h.is_empty())
82                    && matches!(u.scheme(), "http" | "https" | "socks5" | "socks5h") =>
83            {
84                Self::Url(raw.into())
85            }
86            _ => Self::Invalid,
87        }
88    }
89
90    /// Credential `proxy_url` (attribute, then file metadata), then `requests.proxy-url`.
91    pub fn effective(credential: &Credential, cfg: &Config) -> Self {
92        Self::parse(&Self::effective_url(credential, cfg))
93    }
94
95    /// The setting `effective` parses, trimmed (Go `proxyURLForAuth`).
96    pub fn effective_url(credential: &Credential, cfg: &Config) -> String {
97        Self::own_url(credential).map_or_else(|| Self::global_url(cfg), str::to_owned)
98    }
99
100    /// The credential's own proxy setting, trimmed, when it has one.
101    pub(crate) fn own_url(credential: &Credential) -> Option<&str> {
102        credential
103            .attributes
104            .get("proxy_url")
105            .map(String::as_str)
106            .filter(|s| !s.trim().is_empty())
107            .or_else(|| credential.str("proxy_url"))
108            .map(str::trim)
109            .filter(|s| !s.is_empty())
110    }
111
112    /// `requests.proxy-url`, trimmed.
113    pub(crate) fn global_url(cfg: &Config) -> String {
114        cfg.document
115            .get("requests")
116            .and_then(|r| r.get("proxy-url"))
117            .and_then(serde_yaml_ng::Value::as_str)
118            .unwrap_or_default()
119            .trim()
120            .to_owned()
121    }
122
123    /// Applies this proxy to a client. `inherit_env` mirrors Go's standard transport,
124    /// which honours environment proxies when nothing is configured; the Claude uTLS
125    /// transports dial directly instead.
126    pub fn apply(&self, builder: wreq::ClientBuilder, inherit_env: bool) -> wreq::Result<wreq::ClientBuilder> {
127        match self {
128            Proxy::Url(url) => Ok(builder.proxy(wreq::Proxy::all(wreq_proxy_url(url).as_str())?)),
129            Proxy::Inherit | Proxy::Invalid if inherit_env => EnvProxy::current().apply(builder),
130            _ => Ok(builder.no_proxy()),
131        }
132    }
133}
134
135/// Go's `http.ProxyFromEnvironment` (golang.org/x/net/http/httpproxy), read once like
136/// Go's `envProxyFunc`: `HTTP_PROXY` for http and `HTTPS_PROXY` for https targets (upper
137/// case first), never `ALL_PROXY`; `localhost` and loopback addresses always bypass;
138/// `NO_PROXY` adds exclusions, and `*` disables both proxies.
139///
140/// ponytail: wreq's exclusion matcher stands in for Go's. Port-specific `NO_PROXY`
141/// entries are dropped (Go bypasses only that port), a leading-dot entry also matches
142/// the bare domain, and the CGI rule ignores `HTTP_PROXY` instead of failing requests.
143#[derive(Debug, Clone, Default, PartialEq, Eq)]
144pub(crate) struct EnvProxy {
145    http: Option<String>,
146    https: Option<String>,
147    /// wreq `NoProxy` list including Go's implicit loopback bypass; `None` for `*`.
148    no_proxy: Option<String>,
149}
150
151impl EnvProxy {
152    fn current() -> &'static Self {
153        static ENV: std::sync::OnceLock<EnvProxy> = std::sync::OnceLock::new();
154        ENV.get_or_init(|| Self::from_lookup(|name| std::env::var(name).ok()))
155    }
156
157    /// `FromEnvironment` + `config.init` over a variable lookup.
158    pub(crate) fn from_lookup(get: impl Fn(&str) -> Option<String>) -> Self {
159        let any = |names: [&str; 2]| names.iter().find_map(|n| get(n).filter(|v| !v.is_empty()));
160        let cgi = get("REQUEST_METHOD").is_some_and(|v| !v.is_empty());
161        let http = any(["HTTP_PROXY", "http_proxy"])
162            .filter(|_| !cgi)
163            .and_then(|v| env_proxy_url(&v));
164        let https = any(["HTTPS_PROXY", "https_proxy"]).and_then(|v| env_proxy_url(&v));
165        let mut entries: Vec<String> = ["localhost", "127.0.0.0/8", "::1", "::ffff:127.0.0.0/104"]
166            .map(String::from)
167            .into();
168        let mut all = false;
169        for entry in any(["NO_PROXY", "no_proxy"]).unwrap_or_default().split(',') {
170            let entry = entry.trim().go_lower();
171            if entry.is_empty() {
172                continue;
173            }
174            if entry == "*" {
175                all = true;
176                break;
177            }
178            let bare_ip = entry.parse::<std::net::IpAddr>().is_ok() || entry.contains('/');
179            let has_port = !bare_ip
180                && entry
181                    .rsplit_once(':')
182                    .is_some_and(|(_, port)| !port.is_empty() && port.bytes().all(|b| b.is_ascii_digit()));
183            if has_port {
184                continue;
185            }
186            entries.push(
187                entry
188                    .strip_prefix('*')
189                    .filter(|e| e.starts_with('.'))
190                    .unwrap_or(&entry)
191                    .to_owned(),
192            );
193        }
194        Self {
195            http,
196            https,
197            no_proxy: (!all).then(|| entries.join(",")),
198        }
199    }
200
201    fn apply(&self, builder: wreq::ClientBuilder) -> wreq::Result<wreq::ClientBuilder> {
202        let mut builder = builder.no_proxy();
203        let Some(no_proxy) = &self.no_proxy else {
204            return Ok(builder);
205        };
206        let exclusions = || wreq::NoProxy::from_string(no_proxy);
207        if let Some(url) = &self.http {
208            builder = builder.proxy(wreq::Proxy::http(url.as_str())?.no_proxy(exclusions()));
209        }
210        if let Some(url) = &self.https {
211            builder = builder.proxy(wreq::Proxy::https(url.as_str())?.no_proxy(exclusions()));
212        }
213        Ok(builder)
214    }
215}
216
217/// httpproxy `parseProxy`: a value without scheme or host is retried as `http://`.
218fn env_proxy_url(raw: &str) -> Option<String> {
219    let parsed = url::Url::parse(raw)
220        .ok()
221        .filter(|u| u.host_str().is_some_and(|h| !h.is_empty()))
222        .or_else(|| url::Url::parse(&format!("http://{raw}")).ok())?;
223    Some(wreq_proxy_url(parsed.as_str().trim_end_matches('/')))
224}
225
226/// Go's SOCKS5 dialer always sends the hostname to the proxy, so `socks5` resolves
227/// remotely there; wreq does that only for `socks5h`.
228fn wreq_proxy_url(url: &str) -> String {
229    match url.split_once("://") {
230        Some((scheme, rest)) if scheme.eq_ignore_ascii_case("socks5") => format!("socks5h://{rest}"),
231        _ => url.to_owned(),
232    }
233}
234
235/// `proxyutil.Redact`: a proxy URL safe for logs (no user info).
236pub fn redact(raw: &str) -> String {
237    match url::Url::parse(raw.trim()) {
238        Ok(mut u) => {
239            if !u.username().is_empty() || u.password().is_some() {
240                let _ = u.set_username("xxxxx");
241                let _ = u.set_password(None);
242            }
243            u.to_string()
244        }
245        Err(_) => "<invalid proxy url>".into(),
246    }
247}
248
249/// Go standard-transport clients keyed by effective proxy, most recently used first.
250pub struct GoClients {
251    hooks: Hooks,
252    /// Injected default (tests point it at local mocks); used for Inherit/Invalid.
253    default: Option<wreq::Client>,
254    cache: Mutex<Vec<(Proxy, wreq::Client)>>,
255    /// Never offer HTTP/2 (Go transports with `ForceAttemptHTTP2` off and an empty
256    /// `TLSNextProto`, as Antigravity's).
257    http1: bool,
258    /// Idle connections kept per host and how long (Go `MaxIdleConnsPerHost` and
259    /// `IdleConnTimeout`); `None` keeps wreq's defaults.
260    pool: Option<(usize, Option<std::time::Duration>)>,
261    /// The clients tell timed requests whether they dialled ([`watch_connections`]).
262    timed: bool,
263}
264
265impl GoClients {
266    pub fn new(hooks: Hooks) -> Self {
267        Self {
268            hooks,
269            default: None,
270            cache: Mutex::default(),
271            http1: false,
272            pool: None,
273            timed: false,
274        }
275    }
276
277    /// Clients keeping at most `max_idle_per_host` idle connections per host (0 closes
278    /// each connection after its response), each for at most `idle_timeout`. Additive
279    /// (Antigravity's connection pool settings).
280    pub fn pool(mut self, max_idle_per_host: usize, idle_timeout: Option<std::time::Duration>) -> Self {
281        self.pool = Some((max_idle_per_host, idle_timeout));
282        self
283    }
284
285    /// Clients that speak only HTTP/1.1. Additive (Antigravity).
286    pub fn http1_only(mut self) -> Self {
287        self.http1 = true;
288        self
289    }
290
291    /// Every unproxied request uses `client` (local mock servers in tests).
292    pub fn with_default(client: wreq::Client) -> Self {
293        Self {
294            hooks: Hooks::default(),
295            default: Some(client),
296            cache: Mutex::default(),
297            http1: false,
298            pool: None,
299            timed: false,
300        }
301    }
302
303    /// Clients for an executor that times its requests (`send_routed` with a usage
304    /// sink): they report whether a request dialled its connection.
305    pub fn timed(mut self) -> Self {
306        self.timed = true;
307        self
308    }
309
310    pub fn get(&self, proxy: &Proxy) -> wreq::Client {
311        if let (Some(default), Proxy::Inherit | Proxy::Invalid) = (&self.default, proxy) {
312            return default.clone();
313        }
314        if matches!(proxy, Proxy::Invalid) {
315            tracing::warn!("unusable proxy configuration; using the default transport");
316        }
317        self.try_get(proxy).unwrap_or_else(|| {
318            tracing::warn!(proxy = %match proxy { Proxy::Url(u) => redact(u), _ => String::new() }, "proxy client failed; using the default transport");
319            self.default.clone().unwrap_or_else(|| plain_client(self.timed))
320        })
321    }
322
323    /// The client for exactly `proxy`, or `None` when it cannot be built; never falls
324    /// back to another transport (management `api-call` applies Go's own fallbacks).
325    pub fn try_get(&self, proxy: &Proxy) -> Option<wreq::Client> {
326        let mut cache = self.cache.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
327        if let Some(i) = cache.iter().position(|(p, _)| p == proxy) {
328            let entry = cache.remove(i);
329            let client = entry.1.clone();
330            cache.insert(0, entry);
331            return Some(client);
332        }
333        let mut builder = wreq::Client::builder().redirect(wreq::redirect::Policy::none());
334        if self.timed {
335            builder = watch_connections(builder);
336        }
337        let mut builder = self.hooks.apply(builder);
338        if self.http1 {
339            builder = builder.http1_only();
340        }
341        if let Some((max_idle, idle_timeout)) = self.pool {
342            builder = builder.pool_max_idle_per_host(max_idle).pool_idle_timeout(idle_timeout);
343        }
344        let built = proxy.apply(builder, true).and_then(wreq::ClientBuilder::build);
345        let client = built.ok()?;
346        cache.insert(0, (proxy.clone(), client.clone()));
347        cache.truncate(CACHE_CAPACITY);
348        Some(client)
349    }
350}
351
352/// A plain client whose redirects are followed by [`send`], not by wreq, with Go's
353/// environment proxy rules.
354pub fn default_client() -> wreq::Client {
355    plain_client(false)
356}
357
358fn plain_client(timed: bool) -> wreq::Client {
359    let mut builder = wreq::Client::builder().redirect(wreq::redirect::Policy::none());
360    if timed {
361        builder = watch_connections(builder);
362    }
363    EnvProxy::current()
364        .apply(builder)
365        .and_then(wreq::ClientBuilder::build)
366        .expect("default HTTP client")
367}
368
369/// Go `textproto.CanonicalMIMEHeaderKey`.
370pub fn canonical_header(name: &str) -> String {
371    let valid = !name.is_empty()
372        && name
373            .bytes()
374            .all(|b| b.is_ascii_alphanumeric() || b"!#$%&'*+-.^_`|~".contains(&b));
375    if !valid {
376        return name.to_owned();
377    }
378    let mut upper = true;
379    name.chars()
380        .map(|c| {
381            let out = if upper {
382                c.to_ascii_uppercase()
383            } else {
384                c.to_ascii_lowercase()
385            };
386            upper = c == '-';
387            out
388        })
389        .collect()
390}
391
392/// Headers in the order Go's HTTP/1.1 client writes them: Host, User-Agent (Go's
393/// default when unset), Content-Length, the remaining request headers sorted by key
394/// with their values in order, then the transport's Accept-Encoding.
395#[derive(Clone, Default)]
396pub struct GoHeaders {
397    headers: Vec<(String, String)>,
398    /// Go `http.Transport.DisableCompression`: the transport never asks for gzip.
399    compression_disabled: bool,
400    /// Build every hop's URL and request target as Go's `net/url` does.
401    exact_target: bool,
402}
403
404impl GoHeaders {
405    pub fn new() -> Self {
406        Self::default()
407    }
408
409    /// `http.Header.Set`.
410    pub fn set(&mut self, name: &str, value: impl Into<String>) {
411        let name = canonical_header(name);
412        self.headers.retain(|(n, _)| *n != name);
413        self.headers.push((name, value.into()));
414    }
415
416    /// Appends a value under a key that keeps its exact spelling (a raw `http.Header`
417    /// map key in Go, written as stored).
418    pub fn add_raw(&mut self, name: &str, value: impl Into<String>) {
419        self.headers.push((name.to_owned(), value.into()));
420    }
421
422    /// Go `http.Transport.DisableCompression` (Devin's transport): no automatic
423    /// `Accept-Encoding: gzip`, so responses are never decoded transparently.
424    pub fn disable_compression(&mut self) {
425        self.compression_disabled = true;
426    }
427
428    /// Go's `net/url` for every hop: the caller's URL is parsed as `url.Parse` does, each
429    /// redirect `Location` is resolved with `URL.Parse` (`ResolveReference`), and each
430    /// request goes out with Go's `URL.RequestURI()`, so dot segments, percent escapes and
431    /// the query stay as Go keeps them. A URL or `Location` Go fails to parse, one without
432    /// a host, and one whose target wreq would not send as is ([`go_target`]) fail the
433    /// request. Without it every URL is normalized as WHATWG parses it.
434    pub fn exact_target(&mut self) {
435        self.exact_target = true;
436    }
437
438    /// The headers as Go's `http.Header` holds them before sending (`Header.Clone()`, what
439    /// request logging records): set names canonical, raw names as written, values in
440    /// order. The transport's own lines (Host from the URL, the default User-Agent,
441    /// Content-Length, Accept-Encoding) are not included.
442    pub fn pairs(&self) -> &[(String, String)] {
443        &self.headers
444    }
445
446    pub fn get(&self, name: &str) -> Option<&str> {
447        let name = canonical_header(name);
448        self.headers.iter().find(|(n, _)| *n == name).map(|(_, v)| v.as_str())
449    }
450
451    /// Removes every value of `name` (`http.Header.Del`) and returns the first.
452    pub fn take(&mut self, name: &str) -> Option<String> {
453        let name = canonical_header(name);
454        let mut first = None;
455        self.headers.retain(|(n, v)| {
456            if *n == name {
457                first.get_or_insert_with(|| v.clone());
458                false
459            } else {
460                true
461            }
462        });
463        first
464    }
465
466    /// Applies the headers with their wire spelling. `order` is an exact header order
467    /// (Claude's ordered request writer); `None` writes Go net/http's order. Returns
468    /// whether the transport asked for gzip itself (no explicit Accept-Encoding or
469    /// Range), which is the only case Go decodes transparently.
470    pub fn apply(self, builder: wreq::RequestBuilder, order: Option<&[String]>) -> (wreq::RequestBuilder, bool) {
471        self.apply_gzip(builder, order, true, false)
472    }
473
474    /// [`GoHeaders::apply`]; `gzip_allowed` is false for HEAD, which Go's transport
475    /// never asks to compress, and `http2` picks Go's HTTP/2 default User-Agent.
476    fn apply_gzip(
477        mut self,
478        builder: wreq::RequestBuilder,
479        order: Option<&[String]>,
480        gzip_allowed: bool,
481        http2: bool,
482    ) -> (wreq::RequestBuilder, bool) {
483        let auto_gzip = gzip_allowed
484            && !self.compression_disabled
485            && self.get("Accept-Encoding").is_none()
486            && self.get("Range").is_none();
487        // A custom Host header becomes the request Host (util.applyCustomHeaders).
488        let host = self.take("Host").filter(|h| !h.is_empty());
489        self.take("Content-Length");
490        match self.get("User-Agent") {
491            // net/http's defaultUserAgent, or x/net/http2's on an HTTP/2 connection.
492            None => {
493                let ua = if http2 {
494                    "Go-http-client/2.0"
495                } else {
496                    "Go-http-client/1.1"
497                };
498                self.headers.push(("User-Agent".into(), ua.into()));
499            }
500            // Go writes no User-Agent line for an empty value (`Header["User-Agent"] = []string{""}`).
501            Some("") => {
502                self.take("User-Agent");
503            }
504            Some(_) => {}
505        }
506        let mut wire = wreq::header::OrigHeaderMap::new();
507        match order {
508            Some(order) => {
509                for name in order {
510                    wire.insert(name.clone());
511                }
512            }
513            None => {
514                wire.insert("Host");
515                wire.insert("User-Agent");
516                wire.insert("Content-Length");
517                let mut rest: Vec<&String> = self
518                    .headers
519                    .iter()
520                    .map(|(n, _)| n)
521                    .filter(|n| *n != "User-Agent")
522                    .collect();
523                rest.sort();
524                rest.dedup();
525                for name in rest {
526                    wire.insert(name.clone());
527                }
528            }
529        }
530        if auto_gzip {
531            wire.insert("Accept-Encoding");
532            self.headers.push(("Accept-Encoding".into(), "gzip".into()));
533        }
534        let mut builder = builder.orig_headers(wire).default_headers(false);
535        if let Some(host) = host {
536            builder = builder.header("Host", host);
537        }
538        for (name, value) in self.headers {
539            builder = builder.header(name, value);
540        }
541        (builder, auto_gzip)
542    }
543}
544
545/// How one request hop is sent: its client, and an exact header order for Claude's
546/// native transport (`None` is Go net/http's order). Go's `http.Client` picks the
547/// round tripper per hop, so a redirect can change both.
548pub struct Route {
549    pub client: wreq::Client,
550    pub order: Option<Vec<String>>,
551}
552
553/// A response with Go net/http semantics: the body is decoded only for gzip the
554/// transport requested itself, and is never framed or buffered here.
555pub struct Upstream {
556    pub status: u16,
557    pub headers: HeaderMap,
558    pub body: BoxStream<'static, Result<Bytes, ExecError>>,
559}
560
561/// Largest error body kept from an upstream rejection.
562// ponytail: Go reads error bodies without a bound; 16 MiB keeps a hostile upstream from
563// exhausting a small VPS while covering any real provider error.
564pub const MAX_ERROR_BODY: usize = 16 << 20;
565
566fn body_error(_: std::io::Error) -> ExecError {
567    ExecError::local(502, FailureScope::Transport, "upstream request failed")
568}
569
570/// POSTs `body` the way Go's `http.Client.Do` does, following redirects itself: at
571/// most ten requests, 301/302/303 turn a POST into a body-less GET, 307/308 resend the
572/// body, the initial headers are copied to every hop (credentials only within the
573/// original domain), a custom Host survives only relative redirects, and the previous
574/// URL becomes `Referer`. `timeout` bounds the exchange.
575pub async fn send(
576    client: &wreq::Client,
577    url: &str,
578    headers: GoHeaders,
579    body: impl Into<Bytes>,
580    timeout: Option<std::time::Duration>,
581) -> Result<Upstream, ExecError> {
582    request(client, wreq::Method::POST, url, headers, Some(body.into()), timeout).await
583}
584
585/// [`send`] for any method: `http.NewRequest(method, url, body)` then `Client.Do`, with
586/// Go's redirect rules for that method (a GET follows 301-303 and 307/308 as a GET).
587/// `None` is Go's nil body.
588pub async fn request(
589    client: &wreq::Client,
590    method: wreq::Method,
591    url: &str,
592    headers: GoHeaders,
593    body: Option<Bytes>,
594    timeout: Option<std::time::Duration>,
595) -> Result<Upstream, ExecError> {
596    let route = |_: &url::Url| {
597        Ok(Route {
598            client: client.clone(),
599            order: None,
600        })
601    };
602    send_request(&route, method, url, headers, body, timeout).await
603}
604
605/// [`send`] with the client and header order chosen per hop. With `timing`, the
606/// exchange reports its stages to the attempt's usage record ([`UpstreamStage`]).
607pub async fn send_routed(
608    route: &(dyn Fn(&url::Url) -> Result<Route, ExecError> + Sync),
609    url: &str,
610    headers: GoHeaders,
611    body: Bytes,
612    timeout: Option<std::time::Duration>,
613    timing: Option<&UsageSink>,
614) -> Result<Upstream, ExecError> {
615    let raw = exchange(
616        route,
617        wreq::Method::POST,
618        url,
619        headers,
620        Some(body),
621        timeout,
622        true,
623        timing,
624    )
625    .await;
626    upstream(raw)
627}
628
629/// [`send_routed`] for any method. `None` is Go's nil body: no body and no
630/// Content-Length (management `api-call`).
631pub async fn send_request(
632    route: &(dyn Fn(&url::Url) -> Result<Route, ExecError> + Sync),
633    method: wreq::Method,
634    url: &str,
635    headers: GoHeaders,
636    body: Option<Bytes>,
637    timeout: Option<std::time::Duration>,
638) -> Result<Upstream, ExecError> {
639    upstream(exchange(route, method, url, headers, body, timeout, true, None).await)
640}
641
642/// What [`send_request`] makes of an exchange: errors and body errors sanitised.
643fn upstream(raw: Result<RawUpstream, SendError>) -> Result<Upstream, ExecError> {
644    raw.map(RawUpstream::sanitized).map_err(sanitize)
645}
646
647/// The error [`send_request`] reports for a [`SendError`].
648fn sanitize(error: SendError) -> ExecError {
649    match error {
650        SendError::Transport { error, .. } => crate::upstream::transport_error(error),
651        SendError::Local { error, .. } => error,
652        // What `transport_error` makes of the transport's own timeout.
653        SendError::Timeout { .. } => ExecError::local(502, FailureScope::Transport, "upstream request failed"),
654    }
655}
656
657/// Why [`send_request_raw`] failed: the transport's own error (which may name the URL,
658/// so callers decide what to show), a locally generated one, or `Timeout`: the
659/// deadline passed before a hop was sent, so nothing was sent, and callers report it
660/// as they report the transport's own timeout. `url` is the URL Go's client names in
661/// its `*url.Error`: the failing hop (the caller's URL as written for the first
662/// request), or the rejected `Location` when the redirect limit is hit.
663// ponytail: wreq has no public constructor for its timeout error, so `Timeout` cannot be
664// a `Transport`; each renderer repeats what it makes of one. Fold it into `Transport`
665// if wreq ever exposes that constructor.
666#[derive(Debug)]
667pub enum SendError {
668    Transport { error: wreq::Error, url: String },
669    Local { error: ExecError, url: String },
670    Timeout { url: String },
671}
672
673/// [`Upstream`] with the transport's raw body errors and the response's protocol
674/// version (Go's response reader keeps framing headers by version).
675pub struct RawUpstream {
676    pub status: u16,
677    pub version: http::Version,
678    pub headers: HeaderMap,
679    pub body: BoxStream<'static, Result<Bytes, std::io::Error>>,
680}
681
682impl RawUpstream {
683    /// The [`Upstream`] [`send_request`] returns: body errors sanitised.
684    fn sanitized(self) -> Upstream {
685        Upstream {
686            status: self.status,
687            headers: self.headers,
688            body: self.body.map(|r| r.map_err(body_error)).boxed(),
689        }
690    }
691
692    /// [`Self::sanitized`] for a body read line by line, as Go's `bufio.Scanner` reads
693    /// it: a body the upstream ended early fails with the scanner's `unexpected EOF`;
694    /// every other read failure keeps `upstream request failed`.
695    // ponytail: Go names resets and timeouts by their own error text, which can carry
696    // the upstream URL; those keep this crate's generic text.
697    fn scanned(self) -> Upstream {
698        let map = |error: std::io::Error| {
699            if ended_early(&error) {
700                ExecError::local(502, FailureScope::Transport, "unexpected EOF")
701            } else {
702                body_error(error)
703            }
704        };
705        Upstream {
706            status: self.status,
707            headers: self.headers,
708            body: self.body.map(move |r| r.map_err(map)).boxed(),
709        }
710    }
711}
712
713/// Whether a body read failed because the upstream ended the body before its length or
714/// final chunk (the transport's `UnexpectedEof`, Go's `io.ErrUnexpectedEOF`), rather
715/// than a reset or a timeout.
716fn ended_early(error: &std::io::Error) -> bool {
717    let mut cause: Option<&(dyn std::error::Error + 'static)> = Some(error);
718    while let Some(current) = cause {
719        if current
720            .downcast_ref::<std::io::Error>()
721            .is_some_and(|io| io.kind() == std::io::ErrorKind::UnexpectedEof)
722        {
723            return true;
724        }
725        cause = current.source();
726    }
727    false
728}
729
730/// [`send_request`] without error sanitising, for callers that report Go's own error
731/// text (the plugin host's `host.http.*`).
732pub async fn send_request_raw(
733    route: &(dyn Fn(&url::Url) -> Result<Route, ExecError> + Sync),
734    method: wreq::Method,
735    url: &str,
736    headers: GoHeaders,
737    body: Option<Bytes>,
738    timeout: Option<std::time::Duration>,
739) -> Result<RawUpstream, SendError> {
740    exchange(route, method, url, headers, body, timeout, true, None).await
741}
742
743/// [`send`] for a response body the caller reads line by line: a body cut short fails
744/// with Go's `unexpected EOF` ([`RawUpstream::scanned`]).
745pub async fn send_scanned(
746    client: &wreq::Client,
747    url: &str,
748    headers: GoHeaders,
749    body: Bytes,
750    timeout: Option<std::time::Duration>,
751) -> Result<Upstream, ExecError> {
752    let route = |_: &url::Url| {
753        Ok(Route {
754            client: client.clone(),
755            order: None,
756        })
757    };
758    exchange(
759        &route,
760        wreq::Method::POST,
761        url,
762        headers,
763        Some(body),
764        timeout,
765        true,
766        None,
767    )
768    .await
769    .map(RawUpstream::scanned)
770    .map_err(sanitize)
771}
772
773/// [`request`] without following redirects: a client whose `CheckRedirect` returns
774/// `http.ErrUseLastResponse` hands back the first answer, 3xx included. Untimed: it
775/// reports no stages (Antigravity's grounding redirects).
776pub async fn request_once(
777    client: &wreq::Client,
778    method: wreq::Method,
779    url: &str,
780    headers: GoHeaders,
781    body: Option<Bytes>,
782    timeout: Option<std::time::Duration>,
783) -> Result<Upstream, ExecError> {
784    let route = |_: &url::Url| {
785        Ok(Route {
786            client: client.clone(),
787            order: None,
788        })
789    };
790    upstream(exchange(&route, method, url, headers, body, timeout, false, None).await)
791}
792
793/// One request and, with `follow_redirects`, the redirects it is answered with, as
794/// Go's `Client.Do`; without, the first answer, 3xx included. With `timing`, the
795/// exchange reports its stages to the attempt's usage record ([`UpstreamStage`]).
796#[allow(clippy::too_many_arguments)]
797async fn exchange(
798    route: &(dyn Fn(&url::Url) -> Result<Route, ExecError> + Sync),
799    method: wreq::Method,
800    url: &str,
801    headers: GoHeaders,
802    body: Option<Bytes>,
803    timeout: Option<std::time::Duration>,
804    follow_redirects: bool,
805    timing: Option<&UsageSink>,
806) -> Result<RawUpstream, SendError> {
807    let local = |error: ExecError, url: &str| SendError::Local {
808        error,
809        url: url.to_owned(),
810    };
811    let invalid = || {
812        local(
813            ExecError::local(500, FailureScope::Request, "invalid upstream URL"),
814            url,
815        )
816    };
817    // GoHeaders::exact_target: Go's URL for the current hop and the target sent for it.
818    // `current` then only routes the hop.
819    let mut go = None;
820    if headers.exact_target {
821        let mut parsed = go_url::Escaped::parse(url).ok_or_else(invalid)?;
822        parsed.remove_empty_port();
823        let target = go_target(&parsed).ok_or_else(invalid)?;
824        go = Some((parsed, target));
825    }
826    let routed = go.as_ref().map(|(_, target)| target.to_string());
827    let initial = url::Url::parse(routed.as_deref().unwrap_or(url)).map_err(|_| invalid())?;
828    let explicit_referer = headers.get("Referer").map(str::to_owned);
829    let mut current = initial.clone();
830    // req.Host: the custom Host of the current hop, if any.
831    let mut host = headers.get("Host").filter(|h| !h.is_empty()).map(str::to_owned);
832    let mut method = method;
833    // Go's redirect `includeBody` says whether a 301/302/303 has dropped the body; it
834    // starts true even for a nil body.
835    let has_body = body.is_some();
836    let mut include_body = true;
837    let body = body.unwrap_or_default();
838    let mut strip_sensitive = false;
839    let mut hop_headers = headers.clone();
840    let mut sent: u32 = 0;
841    // Go's `Client.Timeout`: one deadline for every hop, taken before the first. Cost:
842    // one `Instant` per request and one clock read per hop.
843    // ponytail: wreq starts the body's share of a hop timeout afresh when the headers
844    // arrive, so the final body can take up to that hop's budget again (the whole
845    // exchange stays under twice `timeout`). Bounding it by the deadline needs a second
846    // timer per response; add one if a caller ever reads long bodies under a timeout.
847    let deadline = timeout.and_then(|t| std::time::Instant::now().checked_add(t));
848    if let Some(timing) = timing {
849        timing.upstream_stage(UpstreamStage::Started);
850    }
851    let (response, auto_gzip, dialled) = loop {
852        // The URL Go's client reports for this hop.
853        let hop_url = if sent == 0 { url.to_owned() } else { current.to_string() };
854        let hop = route(&current).map_err(|e| local(e, &hop_url))?;
855        let builder = match &go {
856            Some((_, target)) => hop.client.request(method.clone(), target.clone()),
857            None => hop.client.request(method.clone(), current.as_str()),
858        }
859        .redirect(wreq::redirect::Policy::none());
860        // Go's standard transport (no exact order) speaks HTTP/2 wherever ALPN picks it.
861        let go_transport = hop.order.is_none();
862        let mut send_headers = hop_headers.clone();
863        // Go `send`: the hop URL's user info becomes Basic auth unless the hop already
864        // carries an Authorization header.
865        if let Some((go_hop, _)) = &go
866            && let Some(credentials) = go_hop.basic_credentials()
867            && send_headers.get("Authorization").is_none_or(str::is_empty)
868        {
869            use base64::Engine as _;
870            let encoded = base64::engine::general_purpose::STANDARD.encode(credentials);
871            send_headers.set("Authorization", format!("Basic {encoded}"));
872        }
873        let (builder, auto_gzip) = send_headers.apply_gzip(
874            builder,
875            hop.order.as_deref(),
876            method != wreq::Method::HEAD,
877            go_transport && expects_http2(&current),
878        );
879        let sends_body = has_body && include_body && !body.is_empty();
880        // A timed body is watched where the hop speaks HTTP/1.1: Claude's native
881        // transport, and plain `http://`, which Go's transport never upgrades (no h2c).
882        // ponytail: an `https://` hop on the standard transport may speak HTTP/2, where
883        // wreq resends a request a graceful GOAWAY refused only if its body is a plain
884        // one, so those hops report no `Connected` or `BodySent`. Watching them needs a
885        // resendable watched body in wreq.
886        let watched = timing.filter(|_| !go_transport || current.scheme() == "http");
887        let builder = if let (true, Some(timing)) = (sends_body, watched) {
888            builder.body(wreq::Body::wrap(TimedBody::new(body.clone(), timing.clone(), sent)))
889        } else if sends_body {
890            builder.body(body.clone())
891        } else if matches!(method.as_str(), "POST" | "PUT" | "PATCH") {
892            // transferWriter.shouldSendContentLength: these methods always carry a length,
893            // so a nil or empty body still says Content-Length: 0.
894            builder.header(http::header::CONTENT_LENGTH, "0")
895        } else {
896            builder
897        };
898        // Last, so the hop's setup counts too. Go sends nothing once the deadline has
899        // passed; wreq would send even with a zero timeout: it polls the request before
900        // its timer, which fires on a 1 ms tick.
901        let builder = match deadline.map(|d| d.saturating_duration_since(std::time::Instant::now())) {
902            Some(remaining) if remaining.is_zero() => return Err(SendError::Timeout { url: hop_url }),
903            Some(remaining) => builder.timeout(remaining),
904            None => builder,
905        };
906        // The hop goes out now: what an earlier hop measured no longer applies. Its
907        // body reports nothing before the client polls it.
908        if let Some(timing) = timing {
909            timing.upstream_stage(UpstreamStage::Hop {
910                hop: sent,
911                body: sends_body,
912            });
913        }
914        // A timed hop learns from the connection hook whether it dialled.
915        let (response, dialled) = match timing {
916            Some(_) => {
917                DIALLED
918                    .scope(std::cell::Cell::new(false), async {
919                        let response = builder.send().await;
920                        (response, DIALLED.with(std::cell::Cell::get))
921                    })
922                    .await
923            }
924            None => (builder.send().await, false),
925        };
926        let response = response.map_err(|error| SendError::Transport {
927            error,
928            url: hop_url.clone(),
929        })?;
930        if go_transport {
931            remember_protocol(&current, response.version() == http::Version::HTTP_2);
932        }
933        sent += 1;
934        let status = response.status().as_u16();
935        // Go reads the header's bytes; a Go-URL hop takes any UTF-8 (escaped as Go does).
936        let location = response
937            .headers()
938            .get(http::header::LOCATION)
939            .and_then(|v| match go {
940                Some(_) => std::str::from_utf8(v.as_bytes()).ok(),
941                None => v.to_str().ok(),
942            })
943            .unwrap_or_default()
944            .to_owned();
945        if !follow_redirects || !matches!(status, 301 | 302 | 303 | 307 | 308) || location.is_empty() {
946            break (response, auto_gzip, dialled);
947        }
948        // Go's `URL.Parse(loc)`; a Go-URL hop fails without a host ("no Host in request
949        // URL") or where wreq would rewrite its target. The error names no part of it.
950        let (next, next_go) = match &go {
951            Some((base, _)) => {
952                let resolved = base.join(&location).and_then(|next| {
953                    let target = go_target(&next)?;
954                    let routed = url::Url::parse(&target.to_string()).ok()?;
955                    Some((routed, (next, target)))
956                });
957                let (next, next_go) = resolved.ok_or_else(|| {
958                    local(
959                        ExecError::local(500, FailureScope::Transport, "invalid redirect target"),
960                        &hop_url,
961                    )
962                })?;
963                (next, Some(next_go))
964            }
965            None => {
966                let next = current.join(&location).map_err(|_| {
967                    local(
968                        ExecError::local(
969                            500,
970                            FailureScope::Transport,
971                            format!("failed to parse Location header {location:?}"),
972                        ),
973                        &hop_url,
974                    )
975                })?;
976                (next, None)
977            }
978        };
979        // defaultCheckRedirect: len(via) >= 10.
980        if sent >= 10 {
981            return Err(local(
982                ExecError::local(500, FailureScope::Transport, "stopped after 10 redirects"),
983                &location,
984            ));
985        }
986        if (301..=303).contains(&status) {
987            include_body = false;
988            if method != wreq::Method::GET && method != wreq::Method::HEAD {
989                method = wreq::Method::GET;
990            }
991        }
992        if !strip_sensitive && initial.host_str() != next.host_str() {
993            let (sub, parent) = (
994                next.host_str().unwrap_or_default(),
995                initial.host_str().unwrap_or_default(),
996            );
997            let subdomain = !sub.contains([':', '%'])
998                && sub.len() > parent.len()
999                && sub.ends_with(parent)
1000                && sub.as_bytes()[sub.len() - parent.len() - 1] == b'.';
1001            strip_sensitive = sub != parent && !subdomain;
1002        }
1003        hop_headers = headers.clone();
1004        if strip_sensitive {
1005            for name in [
1006                "Authorization",
1007                "Www-Authenticate",
1008                "Cookie",
1009                "Cookie2",
1010                "Proxy-Authorization",
1011                "Proxy-Authenticate",
1012            ] {
1013                hop_headers.take(name);
1014            }
1015        }
1016        if !include_body {
1017            for name in [
1018                "Content-Encoding",
1019                "Content-Language",
1020                "Content-Location",
1021                "Content-Type",
1022            ] {
1023                hop_headers.take(name);
1024            }
1025        }
1026        // Only a relative Location keeps the current hop's custom Host (go.dev/issue/22233).
1027        hop_headers.take("Host");
1028        host = host.filter(|_| url::Url::parse(&location).is_err());
1029        if let Some(host) = &host {
1030            hop_headers.set("Host", host.clone());
1031        }
1032        hop_headers.take("Referer");
1033        if !(current.scheme() == "https" && next.scheme() == "http") {
1034            let referer = explicit_referer
1035                .clone()
1036                .or_else(|| go.as_ref().map(|(url, _)| url.referer()))
1037                .unwrap_or_else(|| {
1038                    let mut last = current.clone();
1039                    let _ = last.set_username("");
1040                    let _ = last.set_password(None);
1041                    last.to_string()
1042                });
1043            hop_headers.set("Referer", referer);
1044        }
1045        current = next;
1046        if next_go.is_some() {
1047            go = next_go;
1048        }
1049    };
1050    let status = response.status().as_u16();
1051    let version = response.version();
1052    let mut headers = response.headers().clone();
1053    let gzip = auto_gzip
1054        && headers
1055            .get(http::header::CONTENT_ENCODING)
1056            .and_then(|v| v.to_str().ok())
1057            .is_some_and(|v| v.eq_ignore_ascii_case("gzip"));
1058    let stream = response.bytes_stream().map(|r| r.map_err(std::io::Error::other));
1059    let stream = match timing {
1060        Some(timing) => {
1061            timing.upstream_stage(UpstreamStage::Headers { dialled });
1062            futures_util::future::Either::Left(Counted::new(stream, timing.clone()))
1063        }
1064        None => futures_util::future::Either::Right(stream),
1065    };
1066    let body = if gzip {
1067        headers.remove(http::header::CONTENT_ENCODING);
1068        headers.remove(http::header::CONTENT_LENGTH);
1069        let reader = tokio::io::BufReader::new(tokio_util::io::StreamReader::new(stream));
1070        let mut decoder = async_compression::tokio::bufread::GzipDecoder::new(reader);
1071        decoder.multiple_members(true);
1072        tokio_util::io::ReaderStream::new(decoder).boxed()
1073    } else {
1074        stream.boxed()
1075    };
1076    Ok(RawUpstream {
1077        status,
1078        version,
1079        headers,
1080        body,
1081    })
1082}
1083
1084// ---- exchange timings ------------------------------------------------------------
1085//
1086// Cost for a timed request (one with a usage record): a clock read and a lock of the
1087// attempt's record per stage, one boxed body per HTTP/1.1 hop, a task-local scope per
1088// hop, and an addition per response chunk; a request body piece costs a reference
1089// count. Untimed requests pay nothing here.
1090
1091tokio::task_local! {
1092    /// Set by [`watch_connections`]' hook: the timed hop being sent dialled a
1093    /// connection.
1094    static DIALLED: std::cell::Cell<bool>;
1095}
1096
1097/// The connection hook of the clients that timed requests use (Claude's native client
1098/// and [`GoClients::timed`]): a timed request learns whether a connection was dialled
1099/// while it waited ([`UpstreamStage::Headers`]). The connector runs in the task that
1100/// sends the request until the connection is ready; a dial that loses to a pooled
1101/// connection finishes in the background and is not counted. wreq races the pool
1102/// against the whole new connection, its HTTP handshake included, so a dial that
1103/// finished can still lose to a pooled connection that came free: a dial does not say
1104/// which connection served the request, only its absence does (a pooled one).
1105///
1106/// Cost, on every request of a hooked client, timed or not: wreq type-erases a
1107/// connector stack with layers, so each request clones a few boxed services (also for a
1108/// pooled connection) and each dial boxes a few more futures. Other clients have no
1109/// hook and pay nothing.
1110// ponytail: a caller-built client (`ClaudeExecutor::with_client`, unit tests) has no
1111// hook, so its connections read as reused. Build clients through this function to time
1112// them.
1113pub(crate) fn watch_connections(builder: wreq::ClientBuilder) -> wreq::ClientBuilder {
1114    builder.connector_layer(Dials)
1115}
1116
1117#[derive(Clone, Copy)]
1118struct Dials;
1119
1120impl<S> tower::Layer<S> for Dials {
1121    type Service = Dialling<S>;
1122    fn layer(&self, inner: S) -> Dialling<S> {
1123        Dialling(inner)
1124    }
1125}
1126
1127#[derive(Clone)]
1128struct Dialling<S>(S);
1129
1130impl<S: tower::Service<R>, R> tower::Service<R> for Dialling<S> {
1131    type Response = S::Response;
1132    type Error = S::Error;
1133    type Future = futures_util::future::Inspect<S::Future, fn(&Result<S::Response, S::Error>)>;
1134
1135    fn poll_ready(&mut self, cx: &mut std::task::Context<'_>) -> std::task::Poll<Result<(), S::Error>> {
1136        self.0.poll_ready(cx)
1137    }
1138
1139    fn call(&mut self, target: R) -> Self::Future {
1140        use futures_util::FutureExt;
1141        self.0.call(target).inspect(dialled::<S::Response, S::Error>)
1142    }
1143}
1144
1145fn dialled<T, E>(result: &Result<T, E>) {
1146    if result.is_ok() {
1147        let _ = DIALLED.try_with(|dialled| dialled.set(true));
1148    }
1149}
1150
1151/// How much of a timed request body the client is handed at a time. The client takes
1152/// the next piece while its write buffer holds less than 408 KiB (wreq's HTTP/1.1
1153/// limit), so the last piece marks the body as handed over once all but under 472 KiB
1154/// went to the socket.
1155const BODY_PIECE: usize = 64 << 10;
1156
1157/// A timed request body for hop `hop`: the same bytes and exact length as a plain one,
1158/// in [`BODY_PIECE`] pieces of the same buffer, reporting [`UpstreamStage::Connected`]
1159/// when the client first asks for it and [`UpstreamStage::BodySent`] when it takes the
1160/// last piece.
1161struct TimedBody {
1162    rest: Bytes,
1163    len: u64,
1164    asked: bool,
1165    hop: u32,
1166    timing: UsageSink,
1167}
1168
1169impl TimedBody {
1170    fn new(body: Bytes, timing: UsageSink, hop: u32) -> Self {
1171        Self {
1172            len: body.len() as u64,
1173            rest: body,
1174            asked: false,
1175            hop,
1176            timing,
1177        }
1178    }
1179}
1180
1181impl http_body::Body for TimedBody {
1182    type Data = Bytes;
1183    type Error = std::convert::Infallible;
1184
1185    fn poll_frame(
1186        mut self: std::pin::Pin<&mut Self>,
1187        _: &mut std::task::Context<'_>,
1188    ) -> std::task::Poll<Option<Result<http_body::Frame<Bytes>, Self::Error>>> {
1189        let this = &mut *self;
1190        if !std::mem::replace(&mut this.asked, true) {
1191            this.timing.upstream_stage(UpstreamStage::Connected { hop: this.hop });
1192        }
1193        if this.rest.is_empty() {
1194            return std::task::Poll::Ready(None);
1195        }
1196        let piece = this.rest.split_to(this.rest.len().min(BODY_PIECE));
1197        if this.rest.is_empty() {
1198            this.timing.upstream_stage(UpstreamStage::BodySent {
1199                hop: this.hop,
1200                bytes: this.len,
1201            });
1202        }
1203        std::task::Poll::Ready(Some(Ok(http_body::Frame::data(piece))))
1204    }
1205
1206    fn is_end_stream(&self) -> bool {
1207        self.rest.is_empty()
1208    }
1209
1210    fn size_hint(&self) -> http_body::SizeHint {
1211        http_body::SizeHint::with_exact(self.rest.len() as u64)
1212    }
1213}
1214
1215/// A timed response body: reports [`UpstreamStage::FirstByte`] at its first bytes and
1216/// [`UpstreamStage::BodyRead`] when it ends or is dropped.
1217struct Counted<S> {
1218    inner: S,
1219    bytes: u64,
1220    done: bool,
1221    timing: UsageSink,
1222}
1223
1224impl<S> Counted<S> {
1225    fn new(inner: S, timing: UsageSink) -> Self {
1226        Self {
1227            inner,
1228            bytes: 0,
1229            done: false,
1230            timing,
1231        }
1232    }
1233
1234    fn finish(&mut self) {
1235        if !std::mem::replace(&mut self.done, true) {
1236            self.timing
1237                .upstream_stage(UpstreamStage::BodyRead { bytes: self.bytes });
1238        }
1239    }
1240}
1241
1242impl<S: futures_util::Stream<Item = std::io::Result<Bytes>> + Unpin> futures_util::Stream for Counted<S> {
1243    type Item = S::Item;
1244
1245    fn poll_next(
1246        mut self: std::pin::Pin<&mut Self>,
1247        cx: &mut std::task::Context<'_>,
1248    ) -> std::task::Poll<Option<Self::Item>> {
1249        let item = std::task::ready!(self.inner.poll_next_unpin(cx));
1250        match &item {
1251            Some(Ok(chunk)) if !chunk.is_empty() => {
1252                if self.bytes == 0 {
1253                    self.timing.upstream_stage(UpstreamStage::FirstByte);
1254                }
1255                self.bytes += chunk.len() as u64;
1256            }
1257            None => self.finish(),
1258            Some(_) => {}
1259        }
1260        std::task::Poll::Ready(item)
1261    }
1262}
1263
1264impl<S> Drop for Counted<S> {
1265    fn drop(&mut self) {
1266        self.finish();
1267    }
1268}
1269
1270/// Origins (`scheme://host:port`) and whether their last answer came over HTTP/2.
1271/// Go's default User-Agent names the negotiated protocol, which wreq only reports
1272/// after the request is written.
1273fn origin_protocols() -> &'static Mutex<std::collections::HashMap<String, bool>> {
1274    static PROTOCOLS: std::sync::OnceLock<Mutex<std::collections::HashMap<String, bool>>> = std::sync::OnceLock::new();
1275    PROTOCOLS.get_or_init(Mutex::default)
1276}
1277
1278fn origin(url: &url::Url) -> String {
1279    format!(
1280        "{}://{}:{}",
1281        url.scheme(),
1282        url.host_str().unwrap_or_default(),
1283        url.port_or_known_default().unwrap_or_default()
1284    )
1285}
1286
1287/// Whether Go's transport would speak HTTP/2 to `url`: what its origin last answered
1288/// with; never over plain HTTP (no h2c).
1289// ponytail: an origin not seen yet counts as HTTP/1.1, so the first request to an h2
1290// upstream says Go-http-client/1.1 where Go says /2.0 (only callers that set no
1291// User-Agent). The protocol itself is always the negotiated one.
1292fn expects_http2(url: &url::Url) -> bool {
1293    url.scheme() == "https"
1294        && origin_protocols()
1295            .lock()
1296            .unwrap_or_else(std::sync::PoisonError::into_inner)
1297            .get(&origin(url))
1298            .copied()
1299            .unwrap_or(false)
1300}
1301
1302fn remember_protocol(url: &url::Url, http2: bool) {
1303    let mut protocols = origin_protocols()
1304        .lock()
1305        .unwrap_or_else(std::sync::PoisonError::into_inner);
1306    // Bounded: forgetting only brings back the first-request assumption.
1307    if protocols.len() >= 1024 {
1308        protocols.clear();
1309    }
1310    protocols.insert(origin(url), http2);
1311}
1312
1313/// The request Go sends for `url`: its scheme and authority as written, then
1314/// `URL.RequestURI()`. `None` where Go fails (no host, an opaque URL included: `http::Uri`
1315/// needs one too) or wreq would not send that: a host Go escapes or IDNA-encodes on the
1316/// way out (a percent escape, which includes an IPv6 zone, or a non-ASCII byte), or a
1317/// target `http::Uri` does not carry as written (given a string, wreq would re-parse and
1318/// normalize it).
1319pub fn go_target(url: &go_url::Escaped) -> Option<http::Uri> {
1320    let host = url.host();
1321    if !host.is_ascii() || host.contains('%') {
1322        return None;
1323    }
1324    let target = url.request_uri();
1325    // Never the user info: Go sends it only as Basic auth (see `exchange`).
1326    let uri = http::Uri::try_from(format!("{}://{host}{target}", url.scheme())).ok()?;
1327    // wreq writes the Host header from the URI and drops a port Go writes there: an
1328    // empty one, or the scheme's default however it is spelled (`:80`, `:080`, `:443`).
1329    let default_port = if url.scheme() == "https" { 443 } else { 80 };
1330    if host.ends_with(':') || uri.port_u16() == Some(default_port) {
1331        return None;
1332    }
1333    (uri.path_and_query().map(http::uri::PathAndQuery::as_str) == Some(target.as_str())).then_some(uri)
1334}
1335
1336/// `io.ReadAll` up to `limit` bytes; with `lossy`, a read error keeps what arrived
1337/// (Go's error paths ignore it).
1338pub async fn read_all(
1339    mut body: BoxStream<'static, Result<Bytes, ExecError>>,
1340    limit: usize,
1341    lossy: bool,
1342) -> Result<Bytes, ExecError> {
1343    let mut out = bytes::BytesMut::new();
1344    while out.len() < limit {
1345        match body.next().await {
1346            Some(Ok(chunk)) => out.extend_from_slice(&chunk[..chunk.len().min(limit - out.len())]),
1347            Some(Err(_)) if lossy => break,
1348            Some(Err(error)) => return Err(error),
1349            None => break,
1350        }
1351    }
1352    Ok(out.freeze())
1353}
1354
1355/// `bufio.Scanner` with `ScanLines` and `Buffer(nil, max)`: one item per line without
1356/// its `\n`, a trailing `\r` dropped, the final unterminated line included, and
1357/// `bufio.ErrTooLong` when no newline falls within `max` bytes of a line's start (the
1358/// scanner's buffer never holds more). An I/O error ends the stream after the line
1359/// buffered before it, as `Scan` returns that last token before reporting `Err`.
1360// ponytail: the body's end (or error) is taken to arrive with its last bytes, as net/http
1361// returns EOF for a Content-Length body and gzip.Reader at its end; the stream cannot
1362// tell. When Go's reader reports the end on a read of its own, Go rejects an
1363// unterminated final line of exactly `max` bytes that this accepts.
1364pub fn lines(
1365    body: BoxStream<'static, Result<Bytes, ExecError>>,
1366    max: usize,
1367) -> BoxStream<'static, Result<Bytes, ExecError>> {
1368    struct State {
1369        body: BoxStream<'static, Result<Bytes, ExecError>>,
1370        /// Unconsumed input from the current line's start.
1371        buf: bytes::BytesMut,
1372        /// Bytes of `buf` already searched for a newline.
1373        scanned: usize,
1374        /// The reader ended (EOF or the error below).
1375        ended: bool,
1376        error: Option<ExecError>,
1377        finished: bool,
1378    }
1379    let drop_cr = |line: &[u8]| Bytes::copy_from_slice(line.strip_suffix(b"\r").unwrap_or(line));
1380    futures_util::stream::unfold(
1381        State {
1382            body,
1383            buf: bytes::BytesMut::new(),
1384            scanned: 0,
1385            ended: false,
1386            error: None,
1387            finished: false,
1388        },
1389        move |mut st| async move {
1390            if st.finished {
1391                return None;
1392            }
1393            loop {
1394                let window = st.buf.len().min(max);
1395                if let Some(pos) = st.buf[st.scanned.min(window)..window].iter().position(|b| *b == b'\n') {
1396                    let at = st.scanned.min(window) + pos;
1397                    let line = st.buf.split_to(at + 1);
1398                    st.scanned = 0;
1399                    return Some((Ok(drop_cr(&line[..at])), st));
1400                }
1401                st.scanned = window;
1402                // A full buffer without a newline: more data is too long, while the end
1403                // of the body makes it the final line.
1404                if st.buf.len() > max {
1405                    st.finished = true;
1406                    let error = ExecError::local(500, FailureScope::Request, "bufio.Scanner: token too long");
1407                    return Some((Err(error), st));
1408                }
1409                if st.ended {
1410                    if !st.buf.is_empty() {
1411                        let line = st.buf.split();
1412                        st.scanned = 0;
1413                        return Some((Ok(drop_cr(&line)), st));
1414                    }
1415                    st.finished = true;
1416                    return st.error.take().map(|error| (Err(error), st));
1417                }
1418                match st.body.next().await {
1419                    Some(Ok(chunk)) => st.buf.extend_from_slice(&chunk),
1420                    Some(Err(error)) => {
1421                        st.ended = true;
1422                        st.error = Some(error);
1423                    }
1424                    None => st.ended = true,
1425                }
1426            }
1427        },
1428    )
1429    .boxed()
1430}
1431
1432/// Shared handle for executors: Go clients plus the hooks used to build them.
1433pub type SharedGoClients = Arc<GoClients>;
1434
1435#[cfg(test)]
1436mod tests {
1437    use super::*;
1438
1439    /// A body read line by line fails with Go's scanner text `unexpected EOF` only when
1440    /// the upstream ended it early; a reset keeps the generic transport text. Every
1441    /// reply sends five of the hundred bytes or a partial chunked body, then, once the
1442    /// client holds the response, closes or resets the connection.
1443    #[tokio::test]
1444    async fn scanned_bodies_name_only_an_early_end_unexpected_eof() {
1445        use tokio::io::{AsyncBufReadExt, AsyncWriteExt};
1446        const LENGTH: &str = "HTTP/1.1 200 OK\r\nContent-Length: 100\r\n\r\nhello";
1447        const CHUNKED: &str = "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n5\r\nhello\r\n";
1448        for (head, reset, want) in [
1449            (LENGTH, false, "unexpected EOF"),
1450            (CHUNKED, false, "unexpected EOF"),
1451            (LENGTH, true, "upstream request failed"),
1452            (CHUNKED, true, "upstream request failed"),
1453        ] {
1454            let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
1455            let url = format!("http://{}/", listener.local_addr().unwrap());
1456            let (holds_response, response_held) = tokio::sync::oneshot::channel::<()>();
1457            tokio::spawn(async move {
1458                let (socket, _) = listener.accept().await.unwrap();
1459                let mut socket = tokio::io::BufReader::new(socket);
1460                // The request has no body: its head ends with an empty line.
1461                let mut line = String::new();
1462                while socket.read_line(&mut line).await.unwrap() > 0 && line != "\r\n" {
1463                    line.clear();
1464                }
1465                socket.write_all(head.as_bytes()).await.unwrap();
1466                socket.flush().await.unwrap();
1467                let _ = response_held.await;
1468                if reset {
1469                    #[allow(deprecated)]
1470                    socket.get_ref().set_linger(Some(std::time::Duration::ZERO)).unwrap();
1471                }
1472            });
1473            let client = wreq::Client::builder().build().unwrap();
1474            let upstream = send_scanned(&client, &url, GoHeaders::new(), Bytes::new(), None)
1475                .await
1476                .unwrap();
1477            holds_response.send(()).unwrap();
1478            let mut read = lines(upstream.body, 1 << 20);
1479            let error = loop {
1480                match read.next().await.expect("a read error") {
1481                    Ok(_) => {}
1482                    Err(error) => break error,
1483                }
1484            };
1485            assert_eq!(
1486                (
1487                    String::from_utf8_lossy(&error.body).into_owned(),
1488                    error.status,
1489                    error.scope
1490                ),
1491                (want.to_owned(), 502, FailureScope::Transport),
1492                "{head:?} reset={reset}"
1493            );
1494        }
1495    }
1496
1497    #[test]
1498    fn proxy_parsing_scheme_mapping_and_redaction() {
1499        assert_eq!(Proxy::parse(" "), Proxy::Inherit);
1500        assert_eq!(Proxy::parse("DIRECT"), Proxy::Direct);
1501        assert_eq!(Proxy::parse("none"), Proxy::Direct);
1502        assert_eq!(Proxy::parse("http://p:1"), Proxy::Url("http://p:1".into()));
1503        assert_eq!(Proxy::parse("socks5://u:p@p:1"), Proxy::Url("socks5://u:p@p:1".into()));
1504        assert_eq!(Proxy::parse("ftp://p"), Proxy::Invalid);
1505        assert_eq!(Proxy::parse("p:1"), Proxy::Invalid);
1506        assert_eq!(wreq_proxy_url("socks5://h:1"), "socks5h://h:1");
1507        assert_eq!(wreq_proxy_url("SOCKS5://h:1"), "socks5h://h:1");
1508        assert_eq!(wreq_proxy_url("socks5h://h:1"), "socks5h://h:1");
1509        assert_eq!(wreq_proxy_url("http://h:1"), "http://h:1");
1510        assert_eq!(redact("http://user:secret@p:1/"), "http://xxxxx@p:1/");
1511        assert!(!redact("socks5://user:secret@p:1").contains("secret"));
1512    }
1513
1514    #[test]
1515    fn effective_proxy_precedence() {
1516        let cfg = Config::parse("requests:\n  proxy-url: http://global:1\n").unwrap();
1517        let mut c = Credential::from_file(
1518            std::path::Path::new("/a"),
1519            std::path::Path::new("/a/c.json"),
1520            serde_json::json!({"type":"claude","proxy_url":"socks5://meta:2"})
1521                .as_object()
1522                .unwrap()
1523                .clone(),
1524        )
1525        .unwrap();
1526        assert_eq!(Proxy::effective(&c, &cfg), Proxy::Url("socks5://meta:2".into()));
1527        c.attributes.insert("proxy_url".into(), "direct".into());
1528        assert_eq!(Proxy::effective(&c, &cfg), Proxy::Direct);
1529        c.attributes.clear();
1530        c.metadata.remove("proxy_url");
1531        assert_eq!(Proxy::effective(&c, &cfg), Proxy::Url("http://global:1".into()));
1532    }
1533
1534    #[test]
1535    fn environment_proxies_follow_go_httpproxy() {
1536        let env = |vars: &[(&str, &str)]| {
1537            let vars: std::collections::HashMap<String, String> =
1538                vars.iter().map(|(k, v)| ((*k).to_owned(), (*v).to_owned())).collect();
1539            EnvProxy::from_lookup(|name| vars.get(name).cloned())
1540        };
1541        let e = env(&[
1542            ("ALL_PROXY", "http://all:1"),
1543            ("http_proxy", "lower:2"),
1544            ("HTTP_PROXY", "upper:3"),
1545            ("https_proxy", "socks5://s:4"),
1546            ("no_proxy", " Example.com, *.corp.test, 10.0.0.0/8, host:8080, ::2 "),
1547        ]);
1548        assert_eq!(
1549            e.http.as_deref(),
1550            Some("http://upper:3"),
1551            "upper case first; schemeless is http"
1552        );
1553        assert_eq!(e.https.as_deref(), Some("socks5h://s:4"));
1554        assert_eq!(
1555            e.no_proxy.as_deref(),
1556            Some("localhost,127.0.0.0/8,::1,::ffff:127.0.0.0/104,example.com,.corp.test,10.0.0.0/8,::2")
1557        );
1558        assert_eq!(
1559            env(&[("ALL_PROXY", "http://all:1")]),
1560            env(&[]),
1561            "ALL_PROXY is never read"
1562        );
1563        assert_eq!(env(&[("HTTP_PROXY", "http://p:1"), ("NO_PROXY", "a,*")]).no_proxy, None);
1564        assert_eq!(
1565            env(&[("HTTP_PROXY", "http://p:1"), ("REQUEST_METHOD", "GET")]).http,
1566            None
1567        );
1568    }
1569
1570    #[tokio::test]
1571    async fn environment_proxy_bypasses_loopback_and_proxies_the_rest() {
1572        use tokio::io::{AsyncReadExt, AsyncWriteExt};
1573        async fn first_line(listener: tokio::net::TcpListener) -> String {
1574            let (mut socket, _) = listener.accept().await.unwrap();
1575            let mut buf = vec![0; 4096];
1576            let n = socket.read(&mut buf).await.unwrap();
1577            let _ = socket
1578                .write_all(b"HTTP/1.1 204 No Content\r\nContent-Length: 0\r\n\r\n")
1579                .await;
1580            String::from_utf8_lossy(&buf[..n]).lines().next().unwrap().to_owned()
1581        }
1582        let proxy = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
1583        let target = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
1584        let (proxy_addr, target_addr) = (proxy.local_addr().unwrap(), target.local_addr().unwrap());
1585        let env = EnvProxy::from_lookup(|name| (name == "HTTP_PROXY").then(|| format!("http://{proxy_addr}")));
1586        let client = env
1587            .apply(wreq::Client::builder().redirect(wreq::redirect::Policy::none()))
1588            .unwrap()
1589            .build()
1590            .unwrap();
1591        let direct = tokio::spawn(first_line(target));
1592        client.post(format!("http://{target_addr}/v1")).send().await.unwrap();
1593        assert_eq!(
1594            direct.await.unwrap(),
1595            "POST /v1 HTTP/1.1",
1596            "loopback never uses the proxy"
1597        );
1598        let proxied = tokio::spawn(first_line(proxy));
1599        client.post("http://upstream.invalid/v1").send().await.unwrap();
1600        assert_eq!(proxied.await.unwrap(), "POST http://upstream.invalid/v1 HTTP/1.1");
1601    }
1602
1603    #[test]
1604    fn canonical_header_matches_go() {
1605        assert_eq!(canonical_header("x-msh-device-id"), "X-Msh-Device-Id");
1606        assert_eq!(canonical_header("content-TYPE"), "Content-Type");
1607        assert_eq!(canonical_header("bad header"), "bad header");
1608    }
1609
1610    fn go_fixture() -> serde_json::Value {
1611        serde_json::from_str(include_str!("../tests/fixtures/proxy_go.json")).unwrap()
1612    }
1613
1614    /// What a local upstream saw of one request.
1615    #[derive(Debug, Default, Clone, PartialEq)]
1616    struct Saw {
1617        proto: String,
1618        client_alpn: Option<Vec<String>>,
1619        user_agent: String,
1620    }
1621
1622    /// Answers one HTTP/1.1 request on `io` with `ok`, recording its User-Agent.
1623    async fn serve_h1<S: tokio::io::AsyncRead + tokio::io::AsyncWrite + Unpin>(mut io: S, saw: Arc<Mutex<Saw>>) {
1624        use tokio::io::{AsyncReadExt, AsyncWriteExt};
1625        let mut head = Vec::new();
1626        let mut byte = [0u8; 1];
1627        while !head.ends_with(b"\r\n\r\n") {
1628            if io.read(&mut byte).await.unwrap_or(0) == 0 {
1629                return;
1630            }
1631            head.push(byte[0]);
1632        }
1633        let head = String::from_utf8_lossy(&head).into_owned();
1634        {
1635            let mut s = saw.lock().unwrap();
1636            s.proto = head
1637                .lines()
1638                .next()
1639                .unwrap_or_default()
1640                .rsplit(' ')
1641                .next()
1642                .unwrap_or_default()
1643                .to_owned();
1644            s.user_agent = head
1645                .lines()
1646                .find_map(|l| l.split_once(':').filter(|(k, _)| k.eq_ignore_ascii_case("user-agent")))
1647                .map(|(_, v)| v.trim().to_owned())
1648                .unwrap_or_default();
1649        }
1650        let _ = io
1651            .write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\nok")
1652            .await;
1653        let _ = io.shutdown().await;
1654    }
1655
1656    /// A local upstream: TLS offering the `alpn` protocols (HTTP/2 when the client picks
1657    /// it), or plain HTTP/1.1 for `None`. Returns its address, CA PEM and observations.
1658    async fn upstream(alpn: Option<&'static [u8]>) -> (SocketAddr, Vec<u8>, Arc<Mutex<Saw>>) {
1659        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
1660        let addr = listener.local_addr().unwrap();
1661        let saw = Arc::new(Mutex::new(Saw::default()));
1662        let Some(alpn) = alpn else {
1663            let seen = saw.clone();
1664            tokio::spawn(async move {
1665                while let Ok((tcp, _)) = listener.accept().await {
1666                    tokio::spawn(serve_h1(tcp, seen.clone()));
1667                }
1668            });
1669            return (addr, Vec::new(), saw);
1670        };
1671        let (ca, mut builder) = crate::test_tls::builder(&["upstream.test.invalid".to_owned()]);
1672        let offered = saw.clone();
1673        builder.set_alpn_select_callback(move |_, client| {
1674            // The client's offer, decoded from the wire's length-prefixed names.
1675            let (mut names, mut rest) = (Vec::new(), client);
1676            while let Some((&len, tail)) = rest.split_first() {
1677                let (name, tail) = tail.split_at(usize::from(len).min(tail.len()));
1678                names.push(String::from_utf8_lossy(name).into_owned());
1679                rest = tail;
1680            }
1681            offered.lock().unwrap().client_alpn = Some(names);
1682            btls::ssl::select_next_proto(alpn, client).ok_or(btls::ssl::AlpnError::NOACK)
1683        });
1684        let acceptor = builder.build();
1685        let seen = saw.clone();
1686        tokio::spawn(async move {
1687            while let Ok((tcp, _)) = listener.accept().await {
1688                let ssl = btls::ssl::Ssl::new(acceptor.context()).unwrap();
1689                let mut tls = tokio_btls::SslStream::new(ssl, tcp).unwrap();
1690                if std::pin::Pin::new(&mut tls).accept().await.is_err() {
1691                    continue;
1692                }
1693                let seen = seen.clone();
1694                tokio::spawn(async move {
1695                    if tls.ssl().selected_alpn_protocol() != Some(b"h2") {
1696                        return serve_h1(tls, seen).await;
1697                    }
1698                    let mut conn = http2::server::handshake(tls).await.unwrap();
1699                    while let Some(Ok((request, mut respond))) = conn.accept().await {
1700                        {
1701                            let mut s = seen.lock().unwrap();
1702                            s.proto = "HTTP/2.0".into();
1703                            s.user_agent = request
1704                                .headers()
1705                                .get("user-agent")
1706                                .map(|v| v.to_str().unwrap().to_owned())
1707                                .unwrap_or_default();
1708                        }
1709                        let response = http::Response::builder().status(200).body(()).unwrap();
1710                        let mut body = respond.send_response(response, false).unwrap();
1711                        body.send_data(Bytes::from_static(b"ok"), true).unwrap();
1712                    }
1713                });
1714            }
1715        });
1716        (addr, ca, saw)
1717    }
1718
1719    /// An HTTP CONNECT proxy tunnelling every request to `target`.
1720    async fn connect_proxy(target: SocketAddr) -> SocketAddr {
1721        use tokio::io::{AsyncReadExt, AsyncWriteExt};
1722        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
1723        let addr = listener.local_addr().unwrap();
1724        tokio::spawn(async move {
1725            while let Ok((mut client, _)) = listener.accept().await {
1726                tokio::spawn(async move {
1727                    let mut head = Vec::new();
1728                    let mut byte = [0u8; 1];
1729                    while !head.ends_with(b"\r\n\r\n") {
1730                        if client.read(&mut byte).await.unwrap_or(0) == 0 {
1731                            return;
1732                        }
1733                        head.push(byte[0]);
1734                    }
1735                    if !head.starts_with(b"CONNECT ") {
1736                        return;
1737                    }
1738                    let mut upstream = tokio::net::TcpStream::connect(target).await.unwrap();
1739                    client
1740                        .write_all(b"HTTP/1.1 200 Connection established\r\n\r\n")
1741                        .await
1742                        .unwrap();
1743                    let _ = tokio::io::copy_bidirectional(&mut client, &mut upstream).await;
1744                });
1745            }
1746        });
1747        addr
1748    }
1749
1750    /// Go's `http.DefaultTransport` keeps idle connections per host; the standard clients
1751    /// (API-key base URLs, OpenAI-compatible hosts) reuse theirs over HTTP/2 and HTTP/1.1.
1752    #[tokio::test]
1753    async fn go_clients_reuse_connections() {
1754        use std::sync::atomic::Ordering::SeqCst;
1755        let host = "upstream-reuse.test";
1756        for alpn in [&b"\x02h2\x08http/1.1"[..], b"\x08http/1.1"] {
1757            let (ca, addr, accepted) = crate::test_tls::counting_upstream(host, alpn).await;
1758            let clients = GoClients::new(Hooks {
1759                trust: Some(CertStore::from_pem_stack(ca).unwrap()),
1760                resolve: vec![(host.to_owned(), addr)],
1761            });
1762            let url = format!("https://{host}:{}/v1/messages", addr.port());
1763            for _ in 0..3 {
1764                let upstream = send(&clients.get(&Proxy::Direct), &url, GoHeaders::new(), "{}", None)
1765                    .await
1766                    .unwrap();
1767                let body: Vec<Bytes> = upstream.body.map(Result::unwrap).collect().await;
1768                assert_eq!(body.concat(), b"ok");
1769            }
1770            assert_eq!(accepted.load(SeqCst), 1, "ALPN {alpn:?}");
1771        }
1772    }
1773
1774    /// Go's cloned default transport (proxyutil, ForceAttemptHTTP2) offers h2 and
1775    /// http/1.1 to every TLS upstream and speaks HTTP/2 wherever the upstream picks it,
1776    /// directly or through a CONNECT proxy; plain HTTP stays HTTP/1.1. Go's default
1777    /// User-Agent names the protocol (tests/reference/proxy/main.go `protocols`).
1778    #[tokio::test]
1779    async fn go_clients_negotiate_http2_like_go() {
1780        let fixture = go_fixture();
1781        let cases = fixture["protocols"].as_array().unwrap();
1782        assert_eq!(cases.len(), 5);
1783        for case in cases {
1784            let name = case["name"].as_str().unwrap();
1785            let alpn: Option<&'static [u8]> = match case["server"].as_str().unwrap() {
1786                "h2" => Some(b"\x02h2\x08http/1.1"),
1787                "h1" => Some(b"\x08http/1.1"),
1788                _ => None,
1789            };
1790            let (addr, ca, saw) = upstream(alpn).await;
1791            let host = "upstream.test.invalid";
1792            let proxy = match case["proxy"].as_str().unwrap() {
1793                "http" => Proxy::Url(format!("http://{}", connect_proxy(addr).await)),
1794                _ => Proxy::Direct,
1795            };
1796            let clients = GoClients::new(Hooks {
1797                trust: (!ca.is_empty()).then(|| CertStore::from_pem_stack(ca).unwrap()),
1798                resolve: vec![(host.to_owned(), addr)],
1799            });
1800            let scheme = if alpn.is_some() { "https" } else { "http" };
1801            let route = |_: &url::Url| {
1802                Ok(Route {
1803                    client: clients.get(&proxy),
1804                    order: None,
1805                })
1806            };
1807            let want = Saw {
1808                proto: case["proto"].as_str().unwrap().to_owned(),
1809                client_alpn: case["client_alpn"]
1810                    .as_array()
1811                    .map(|a| a.iter().map(|p| p.as_str().unwrap().to_owned()).collect()),
1812                user_agent: case["user_agent"].as_str().unwrap().to_owned(),
1813            };
1814            // The second request knows the origin's protocol; the first assumes HTTP/1.1
1815            // for the default User-Agent only (see `expects_http2`).
1816            for attempt in ["first", "second"] {
1817                let upstream = send_request(
1818                    &route,
1819                    wreq::Method::GET,
1820                    &format!("{scheme}://{host}:{}/", addr.port()),
1821                    GoHeaders::new(),
1822                    None,
1823                    Some(std::time::Duration::from_secs(10)),
1824                )
1825                .await
1826                .unwrap_or_else(|e| panic!("{name}: {e:?}"));
1827                assert_eq!(upstream.status, 200, "{name}");
1828                assert_eq!(read_all(upstream.body, 16, false).await.unwrap(), "ok", "{name}");
1829                let mut want = want.clone();
1830                if attempt == "first" && want.proto == "HTTP/2.0" {
1831                    want.user_agent = "Go-http-client/1.1".into();
1832                }
1833                assert_eq!(*saw.lock().unwrap(), want, "{name} ({attempt} request)");
1834            }
1835        }
1836    }
1837
1838    /// Every case runs the real `bufio.Scanner` in tests/reference/proxy/main.go.
1839    #[tokio::test]
1840    async fn lines_follow_go_bufio_scanner() {
1841        for case in go_fixture()["lines"].as_array().unwrap() {
1842            let name = case["name"].as_str().unwrap();
1843            let mut items: Vec<Result<Bytes, ExecError>> = case["chunks"]
1844                .as_array()
1845                .into_iter()
1846                .flatten()
1847                .map(|c| Ok(Bytes::from(c.as_str().unwrap().to_owned())))
1848                .collect();
1849            let failed = case["io_error"].as_bool().unwrap();
1850            if failed {
1851                items.push(Err(ExecError::local(502, FailureScope::Transport, "read failed")));
1852            }
1853            let body = futures_util::stream::iter(items);
1854            // A failed reader is never read again; otherwise the body ends (EOF).
1855            let body = if failed {
1856                body.chain(futures_util::stream::pending()).boxed()
1857            } else {
1858                body.boxed()
1859            };
1860            let max = case["max"].as_u64().unwrap() as usize;
1861            let got: Vec<_> = lines(body, max).collect().await;
1862            // Go with a separate terminal read differs only where the ponytail says.
1863            let separate = (case["separate_tokens"].clone(), case["separate_error"].clone());
1864            let attached = (case["tokens"].clone(), case["error"].clone());
1865            let differs = ["exact-max-unterminated", "exact-max-then-io-error"].contains(&name);
1866            assert_eq!(separate != attached, differs, "{name}: Go's two terminal modes");
1867            let (tokens, errors): (Vec<_>, Vec<_>) = got.into_iter().partition(Result::is_ok);
1868            let tokens: Vec<String> = tokens
1869                .into_iter()
1870                .map(|t| String::from_utf8(t.unwrap().to_vec()).unwrap())
1871                .collect();
1872            let want: Vec<&str> = case["tokens"]
1873                .as_array()
1874                .unwrap()
1875                .iter()
1876                .map(|t| t.as_str().unwrap())
1877                .collect();
1878            assert_eq!(tokens, want, "{name}");
1879            let error = errors.into_iter().map(|e| e.unwrap_err()).collect::<Vec<_>>();
1880            match case["error"].as_str().unwrap() {
1881                "" => assert!(error.is_empty(), "{name}"),
1882                "too_long" => {
1883                    assert_eq!(error.len(), 1, "{name}");
1884                    assert_eq!(error[0].body, "bufio.Scanner: token too long", "{name}");
1885                }
1886                _ => {
1887                    assert_eq!(error.len(), 1, "{name}");
1888                    assert_eq!(error[0].body, "read failed", "{name}");
1889                }
1890            }
1891        }
1892    }
1893
1894    /// Go's `http.Client` redirect hops for GET and POST (tests/reference/proxy/main.go),
1895    /// replayed through [`request`] against the same scripted servers.
1896    #[tokio::test]
1897    async fn redirects_follow_go_http_client() {
1898        type Hops = Arc<Mutex<Vec<serde_json::Value>>>;
1899        async fn record(
1900            hops: Hops,
1901            script: Arc<serde_json::Value>,
1902            // Location placeholders and their values, replaced in order.
1903            other: Arc<[(&'static str, String); 3]>,
1904            request: axum::extract::Request,
1905        ) -> axum::response::Response {
1906            use axum::response::IntoResponse;
1907            let (parts, body) = request.into_parts();
1908            let body = axum::body::to_bytes(body, 1 << 20).await.unwrap();
1909            let header = |name: &str| {
1910                parts
1911                    .headers
1912                    .get_all(name)
1913                    .iter()
1914                    .map(|v| v.to_str().unwrap().to_owned())
1915                    .collect::<Vec<_>>()
1916                    .join(",")
1917            };
1918            // Go's server looks the script up by `URL.Path`, unescaped.
1919            let raw = parts.uri.path().as_bytes();
1920            let mut path = Vec::with_capacity(raw.len());
1921            let mut i = 0;
1922            while i < raw.len() {
1923                let hex = raw
1924                    .get(i + 1..i + 3)
1925                    .and_then(|h| u8::from_str_radix(std::str::from_utf8(h).ok()?, 16).ok());
1926                match (raw[i], hex) {
1927                    (b'%', Some(byte)) => {
1928                        path.push(byte);
1929                        i += 3;
1930                    }
1931                    (byte, _) => {
1932                        path.push(byte);
1933                        i += 1;
1934                    }
1935                }
1936            }
1937            let path = String::from_utf8(path).unwrap();
1938            let target = parts.uri.path_and_query().map_or("/", |p| p.as_str()).to_owned();
1939            hops.lock().unwrap().push(serde_json::json!({
1940                "user_agent": header("user-agent"),
1941                "has_user_agent": parts.headers.contains_key("user-agent"),
1942                "accept_encoding": header("accept-encoding"),
1943                "method": parts.method.as_str(),
1944                "path": target,
1945                "host": header("host"),
1946                "referer": header("referer"),
1947                "authorization": header("authorization"),
1948                "content_type": header("content-type"),
1949                "content_length": header("content-length"),
1950                "body": String::from_utf8_lossy(&body),
1951            }));
1952            // The exact request target first, as the recorder does.
1953            let Some(step) = script.get(&target).or_else(|| script.get(&path)) else {
1954                return (http::StatusCode::OK, "done").into_response();
1955            };
1956            let status = http::StatusCode::from_u16(step["status"].as_u64().unwrap() as u16).unwrap();
1957            let location = other
1958                .iter()
1959                .fold(step["location"].as_str().unwrap().to_owned(), |l, (token, value)| {
1960                    l.replace(token, value)
1961                });
1962            if location.is_empty() {
1963                return status.into_response();
1964            }
1965            let location = http::HeaderValue::from_bytes(location.as_bytes()).unwrap();
1966            (status, [(http::header::LOCATION, location)]).into_response()
1967        }
1968        let fixture = go_fixture();
1969        let cases = fixture["redirects"].as_array().unwrap();
1970        // Go's net/url builds every hop; Rust does with exact_target, and normalizes
1971        // otherwise, which only dot segments, escaping and malformed targets tell apart.
1972        // cliproxy-rs refuses a hop whose Host header wreq would write without Go's port
1973        // (DIFFERENCES "Model catalog reads"): the hops before it go out, then the
1974        // request fails.
1975        const REFUSED: &[(&str, usize)] = &[
1976            ("get-empty-then-default-port-location-exact", 1),
1977            ("get-default-port-source-exact", 0),
1978        ];
1979        // Goes to port 80, which a test cannot listen on; `go_url` tests cover its
1980        // Host and Referer.
1981        const PORT_80: &str = "get-empty-port-source-exact";
1982        let runs = cases.iter().filter(|case| case["name"] != PORT_80).flat_map(|case| {
1983            let start = case["start"].as_str().unwrap();
1984            let both = start == "/start";
1985            [(case, true)].into_iter().chain(both.then_some((case, false)))
1986        });
1987        for (case, exact) in runs {
1988            let name = format!("{} (exact: {exact})", case["name"].as_str().unwrap());
1989            let main = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
1990            let second = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
1991            let (main_addr, other_port) = (main.local_addr().unwrap(), second.local_addr().unwrap().port());
1992            let other = Arc::new([
1993                ("OTHERHOST", format!("localhost:{other_port}")),
1994                ("MAINHOST", main_addr.to_string()),
1995                ("OTHER", format!("http://localhost:{other_port}")),
1996            ]);
1997            let hops: Hops = Arc::default();
1998            let script = Arc::new(case["script"].clone());
1999            for listener in [main, second] {
2000                let (hops, script, other) = (hops.clone(), script.clone(), other.clone());
2001                let app = axum::Router::new().fallback(move |request: axum::extract::Request| {
2002                    record(hops.clone(), script.clone(), other.clone(), request)
2003                });
2004                tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
2005            }
2006            let mut headers = GoHeaders::new();
2007            for (k, v) in case["headers"].as_object().unwrap() {
2008                headers.set(k, v.as_str().unwrap());
2009            }
2010            if exact {
2011                headers.exact_target();
2012            }
2013            if case["disable_compression"].as_bool().unwrap() {
2014                headers.disable_compression();
2015            }
2016            let base = format!("http://{main_addr}");
2017            let method = wreq::Method::from_bytes(case["method"].as_str().unwrap().as_bytes()).unwrap();
2018            let body = case["body"].as_str().map(|b| Bytes::from(b.to_owned()));
2019            let start = case["start"].as_str().unwrap();
2020            let target = match (case["target"].as_str(), case["user"].as_str()) {
2021                (Some(target), _) => target.replacen("MAINHOST", &main_addr.to_string(), 1),
2022                (None, Some(user)) => format!("http://{user}@{main_addr}{start}"),
2023                (None, None) => format!("{base}{start}"),
2024            };
2025            let refused = REFUSED.iter().find(|(n, _)| case["name"] == *n).map(|(_, sent)| *sent);
2026            let result = request(
2027                &default_client(),
2028                method,
2029                &target,
2030                headers,
2031                body,
2032                Some(std::time::Duration::from_secs(10)),
2033            )
2034            .await;
2035            match &result {
2036                Ok(upstream) => {
2037                    assert!(refused.is_none(), "{name}: not refused");
2038                    assert_eq!(u64::from(upstream.status), case["status"].as_u64().unwrap(), "{name}");
2039                }
2040                // A refusal names the URL invalid; reaching for port 80 fails differently.
2041                Err(e) if refused.is_some() => {
2042                    assert!(String::from_utf8_lossy(&e.body).contains("invalid"), "{name}: {e:?}")
2043                }
2044                Err(_) => assert!(case["error"].as_bool().unwrap(), "{name}: unexpected error"),
2045            }
2046            let got: Vec<serde_json::Value> = hops
2047                .lock()
2048                .unwrap()
2049                .iter()
2050                .map(|h| {
2051                    let mut h = h.clone();
2052                    let host = h["host"]
2053                        .as_str()
2054                        .unwrap()
2055                        .replace(&main_addr.to_string(), "BASE")
2056                        .replace(&format!("localhost:{other_port}"), "OTHER");
2057                    h["host"] = host.into();
2058                    h["referer"] = h["referer"].as_str().unwrap().replacen(&base, "BASE", 1).into();
2059                    h
2060                })
2061                .collect();
2062            let want = case["hops"].as_array().unwrap();
2063            assert_eq!(&got, &want[..refused.unwrap_or(want.len())], "{name}");
2064        }
2065    }
2066
2067    /// Go's `Client.Timeout` bounds the whole exchange: redirect hops share one deadline
2068    /// instead of each getting the full timeout.
2069    #[tokio::test]
2070    async fn timeout_bounds_all_redirect_hops_together() {
2071        use axum::response::IntoResponse;
2072        const TIMEOUT: std::time::Duration = std::time::Duration::from_millis(1000);
2073        const HOP: std::time::Duration = std::time::Duration::from_millis(600);
2074        async fn get(target: String) -> Result<RawUpstream, SendError> {
2075            let client = default_client();
2076            let route = |_: &url::Url| {
2077                Ok(Route {
2078                    client: client.clone(),
2079                    order: None,
2080                })
2081            };
2082            send_request_raw(
2083                &route,
2084                wreq::Method::GET,
2085                &target,
2086                GoHeaders::new(),
2087                None,
2088                Some(TIMEOUT),
2089            )
2090            .await
2091        }
2092        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
2093        let addr = listener.local_addr().unwrap();
2094        let app = axum::Router::new().fallback(|uri: http::Uri| async move {
2095            tokio::time::sleep(HOP).await;
2096            match uri.path() {
2097                "/1" => (http::StatusCode::FOUND, [(http::header::LOCATION, "/2")]).into_response(),
2098                "/2" => (http::StatusCode::FOUND, [(http::header::LOCATION, "/3")]).into_response(),
2099                _ => "done".into_response(),
2100            }
2101        });
2102        tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
2103        // One slow hop within the timeout still succeeds, body included.
2104        let upstream = get(format!("http://{addr}/3")).await.unwrap();
2105        assert_eq!(upstream.status, 200);
2106        let body: Vec<Bytes> = upstream.body.map(Result::unwrap).collect().await;
2107        assert_eq!(body.concat(), b"done");
2108        // Each hop is under the timeout, but the second ends past the shared deadline.
2109        match get(format!("http://{addr}/1")).await {
2110            Err(SendError::Transport { error, url }) => {
2111                assert!(error.is_timeout(), "{error:?}");
2112                assert_eq!(url, format!("http://{addr}/2"));
2113            }
2114            Err(other) => panic!("{other:?}"),
2115            Ok(upstream) => panic!("followed both redirects: {}", upstream.status),
2116        }
2117    }
2118
2119    /// Once the deadline has passed, Go sends nothing: a 307 that would resend a POST
2120    /// body to its target ends without a request, with the error a transport timeout
2121    /// gives callers.
2122    #[tokio::test]
2123    async fn no_hop_is_sent_after_the_deadline() {
2124        use axum::response::IntoResponse;
2125        use std::sync::atomic::{AtomicUsize, Ordering::SeqCst};
2126        const TIMEOUT: std::time::Duration = std::time::Duration::from_millis(200);
2127        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
2128        let addr = listener.local_addr().unwrap();
2129        let hits = Arc::new(AtomicUsize::new(0));
2130        let seen = hits.clone();
2131        let app = axum::Router::new().fallback(move |uri: http::Uri| {
2132            let seen = seen.clone();
2133            async move {
2134                if uri.path() == "/start" {
2135                    return (
2136                        http::StatusCode::TEMPORARY_REDIRECT,
2137                        [(http::header::LOCATION, "/target")],
2138                    )
2139                        .into_response();
2140                }
2141                seen.fetch_add(1, SeqCst);
2142                "done".into_response()
2143            }
2144        });
2145        tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
2146        let client = default_client();
2147        // The redirect handling uses up the budget: choosing the second hop's client
2148        // takes longer than the whole timeout.
2149        let route = |next: &url::Url| {
2150            if next.path() == "/target" {
2151                std::thread::sleep(TIMEOUT);
2152            }
2153            Ok(Route {
2154                client: client.clone(),
2155                order: None,
2156            })
2157        };
2158        let start = format!("http://{addr}/start");
2159        let post = || Some(Bytes::from_static(b"{}"));
2160        let sent = send_request_raw(
2161            &route,
2162            wreq::Method::POST,
2163            &start,
2164            GoHeaders::new(),
2165            post(),
2166            Some(TIMEOUT),
2167        )
2168        .await;
2169        match sent {
2170            Err(SendError::Timeout { url }) => assert_eq!(url, format!("http://{addr}/target")),
2171            Err(other) => panic!("{other:?}"),
2172            Ok(upstream) => panic!("followed the redirect: {}", upstream.status),
2173        }
2174        // The same error as a transport timeout through `send_request`.
2175        let error = send_request(
2176            &route,
2177            wreq::Method::POST,
2178            &start,
2179            GoHeaders::new(),
2180            post(),
2181            Some(TIMEOUT),
2182        )
2183        .await
2184        .err()
2185        .unwrap();
2186        // `transport_error` reads nothing from the wreq error; any one will do.
2187        let any = default_client().get("not a url").send().await.unwrap_err();
2188        let transport = crate::upstream::transport_error(any);
2189        assert_eq!(
2190            (error.status, error.scope, error.body),
2191            (transport.status, transport.scope, transport.body)
2192        );
2193        assert_eq!(hits.load(SeqCst), 0, "the redirect target got a request");
2194    }
2195
2196    /// The stages a timed exchange reported, in order.
2197    #[derive(Default)]
2198    struct Stages(Mutex<Vec<UpstreamStage>>);
2199
2200    impl cpa_core::exec::UsageObserver for Stages {
2201        fn response_body(&self, _: cpa_core::format::Format, _: &[u8]) {}
2202        fn response_line(&self, _: cpa_core::format::Format, _: &[u8]) {}
2203        fn request(&self, _: cpa_core::format::Format, _: &[u8]) {}
2204        fn upstream_stage(&self, stage: UpstreamStage) {
2205            self.0.lock().unwrap().push(stage);
2206        }
2207    }
2208
2209    /// A timed request goes out byte for byte as an untimed one (the same head with its
2210    /// `Content-Length`, the same body), and reports its stages in order, its sizes, and
2211    /// whether a connection was dialled for it.
2212    #[tokio::test]
2213    async fn timed_requests_send_the_same_bytes_and_report_their_stages() {
2214        use tokio::io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt};
2215        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
2216        let url = format!("http://{}/v1/messages", listener.local_addr().unwrap());
2217        let wire = Arc::new(Mutex::new(Vec::<Vec<u8>>::new()));
2218        let seen = wire.clone();
2219        tokio::spawn(async move {
2220            while let Ok((conn, _)) = listener.accept().await {
2221                let seen = seen.clone();
2222                tokio::spawn(async move {
2223                    let mut conn = tokio::io::BufReader::new(conn);
2224                    loop {
2225                        let (mut raw, mut length) = (Vec::new(), 0);
2226                        loop {
2227                            let mut line = String::new();
2228                            if conn.read_line(&mut line).await.unwrap_or(0) == 0 {
2229                                return;
2230                            }
2231                            raw.extend_from_slice(line.as_bytes());
2232                            if let Some(v) = line.to_ascii_lowercase().strip_prefix("content-length:") {
2233                                length = v.trim().parse().unwrap();
2234                            }
2235                            if line == "\r\n" {
2236                                break;
2237                            }
2238                        }
2239                        let mut body = vec![0; length];
2240                        conn.read_exact(&mut body).await.unwrap();
2241                        raw.extend_from_slice(&body);
2242                        seen.lock().unwrap().push(raw);
2243                        let reply = b"HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\nhello";
2244                        conn.get_mut().write_all(reply).await.unwrap();
2245                    }
2246                });
2247            }
2248        });
2249        // Several pieces, and a tail that is not a whole piece.
2250        let body = Bytes::from(vec![b'a'; 2 * BODY_PIECE + 123]);
2251        let clients = GoClients::new(Hooks::default()).timed();
2252        let route = |_: &url::Url| {
2253            Ok(Route {
2254                client: clients.get(&Proxy::Direct),
2255                order: None,
2256            })
2257        };
2258        let headers = || {
2259            let mut h = GoHeaders::new();
2260            h.set("Content-Type", "application/json");
2261            h
2262        };
2263        let stages = Arc::new(Stages::default());
2264        let sink = UsageSink::new(stages.clone());
2265        let read = |up: Upstream| async move {
2266            let chunks: Vec<_> = up.body.collect().await;
2267            chunks.into_iter().map(Result::unwrap).collect::<Vec<_>>().concat()
2268        };
2269        let plain = send_routed(&route, &url, headers(), body.clone(), None, None)
2270            .await
2271            .unwrap();
2272        assert_eq!(read(plain).await, b"hello");
2273        assert!(
2274            stages.0.lock().unwrap().is_empty(),
2275            "an untimed request reports nothing"
2276        );
2277        // A new client, so the first timed request dials.
2278        let clients = GoClients::new(Hooks::default()).timed();
2279        let route = |_: &url::Url| {
2280            Ok(Route {
2281                client: clients.get(&Proxy::Direct),
2282                order: None,
2283            })
2284        };
2285        for dialled in [true, false] {
2286            let timed = send_routed(&route, &url, headers(), body.clone(), None, Some(&sink))
2287                .await
2288                .unwrap();
2289            assert_eq!(read(timed).await, b"hello");
2290            let reported = std::mem::take(&mut *stages.0.lock().unwrap());
2291            assert_eq!(
2292                reported,
2293                [
2294                    UpstreamStage::Started,
2295                    UpstreamStage::Hop { hop: 0, body: true },
2296                    UpstreamStage::Connected { hop: 0 },
2297                    UpstreamStage::BodySent {
2298                        hop: 0,
2299                        bytes: body.len() as u64
2300                    },
2301                    UpstreamStage::Headers { dialled },
2302                    UpstreamStage::FirstByte,
2303                    UpstreamStage::BodyRead { bytes: 5 },
2304                ]
2305            );
2306        }
2307        let wire = wire.lock().unwrap();
2308        assert_eq!(wire.len(), 3);
2309        assert!(String::from_utf8_lossy(&wire[0]).contains(&format!("Content-Length: {}", body.len())));
2310        assert_eq!(wire[0], wire[1], "timed and untimed requests differ on the wire");
2311        assert_eq!(wire[0], wire[2]);
2312    }
2313
2314    #[test]
2315    fn client_cache_is_bounded_lru() {
2316        let clients = GoClients::new(Hooks::default());
2317        for i in 0..=CACHE_CAPACITY {
2318            clients.get(&Proxy::Url(format!("http://p{i}:1")));
2319        }
2320        assert_eq!(clients.cache.lock().unwrap().len(), CACHE_CAPACITY);
2321        assert!(
2322            clients
2323                .cache
2324                .lock()
2325                .unwrap()
2326                .iter()
2327                .all(|(p, _)| *p != Proxy::Url("http://p0:1".into()))
2328        );
2329        clients.get(&Proxy::Url("http://p1:1".into()));
2330        assert_eq!(clients.cache.lock().unwrap()[0].0, Proxy::Url("http://p1:1".into()));
2331    }
2332}