Skip to main content

turnloop_http/
client.rs

1//! Host-driven fetch transport policy. DNS, environment, sockets and time are inputs.
2//! JS body conversions, promise dispatch and abort reason objects remain in Perry.
3use crate::{
4    Error, Result,
5    http1::{Head, Header},
6};
7use std::{
8    cmp::{Ordering, Reverse},
9    collections::{BinaryHeap, HashMap},
10    hash::Hash,
11    net::IpAddr,
12    time::{Duration, Instant},
13};
14use url::Url;
15#[derive(Debug, Clone, Copy, PartialEq, Eq)]
16pub enum Protocol {
17    Http1,
18    Http2,
19}
20pub fn from_alpn(alpn: Option<&[u8]>) -> Result<Protocol> {
21    match alpn {
22        Some(b"h2") => Ok(Protocol::Http2),
23        None | Some(b"http/1.1") => Ok(Protocol::Http1),
24        _ => Err(Error::new("UND_ERR_NOT_SUPPORTED", "unsupported ALPN")),
25    }
26}
27/// Resolver implementations enqueue host operations; they must not block here.
28pub trait Resolver {
29    type Token: Copy;
30    fn resolve(&mut self, hostname: &str, port: u16) -> Result<Self::Token>;
31    fn poll(&mut self, token: Self::Token, addresses: &mut Vec<IpAddr>) -> Option<Result<()>>;
32    fn cancel(&mut self, token: Self::Token);
33}
34pub const DEFAULT_MAX_REDIRECTS: usize = 20;
35#[derive(Debug, Clone, Copy, PartialEq, Eq)]
36pub enum RedirectMode {
37    Follow,
38    Manual,
39    Error,
40}
41#[derive(Debug, Clone)]
42pub struct Request {
43    pub url: Url,
44    pub method: String,
45    pub headers: Vec<Header>,
46    pub body: Vec<u8>,
47    pub replayable: bool,
48    pub redirects: usize,
49}
50impl Request {
51    pub fn new(url: &str, method: &str) -> Result<Self> {
52        let url = Url::parse(url).map_err(|_| Error::new("ERR_INVALID_URL", "invalid URL"))?;
53        if !matches!(url.scheme(), "http" | "https") || url.host_str().is_none() {
54            return Err(Error::new("ERR_INVALID_URL", "unsupported URL scheme"));
55        }
56        if !url.username().is_empty() || url.password().is_some() {
57            return Err(Error::new(
58                "UND_ERR_INVALID_ARG",
59                "URL contains credentials",
60            ));
61        }
62        http::Method::from_bytes(method.as_bytes())
63            .map_err(|_| Error::new("UND_ERR_INVALID_ARG", "invalid method"))?;
64        if ["CONNECT", "TRACE", "TRACK"]
65            .iter()
66            .any(|m| method.eq_ignore_ascii_case(m))
67        {
68            return Err(Error::new("UND_ERR_INVALID_ARG", "forbidden fetch method"));
69        }
70        let method = if ["DELETE", "GET", "HEAD", "OPTIONS", "POST", "PUT"]
71            .iter()
72            .any(|m| method.eq_ignore_ascii_case(m))
73        {
74            method.to_ascii_uppercase()
75        } else {
76            method.into()
77        };
78        Ok(Self {
79            url,
80            method,
81            headers: Vec::new(),
82            body: Vec::new(),
83            replayable: true,
84            redirects: 0,
85        })
86    }
87    pub fn head(&self, absolute_form: bool) -> Head {
88        let mut url = self.url.clone();
89        url.set_fragment(None);
90        let target = if absolute_form {
91            url.as_str().to_owned()
92        } else {
93            let mut target = url.path().to_string();
94            if let Some(q) = url.query() {
95                target.push('?');
96                target.push_str(q);
97            }
98            target
99        };
100        let mut headers = self.headers.clone();
101        headers.retain(|h| !h.name.eq_ignore_ascii_case("host"));
102        headers.push(Header::new("host", authority(&url)));
103        Head {
104            method: self.method.clone(),
105            target,
106            status: 0,
107            version: 1,
108            headers,
109            keep_alive: true,
110        }
111    }
112    /// `true` means resend the modified request; `false` exposes the response as-is.
113    pub fn redirect(
114        &mut self,
115        status: u16,
116        location: Option<&str>,
117        mode: RedirectMode,
118        max: usize,
119    ) -> Result<bool> {
120        if !matches!(status, 301 | 302 | 303 | 307 | 308) {
121            return Ok(false);
122        }
123        if mode == RedirectMode::Manual {
124            return Ok(false);
125        }
126        if mode == RedirectMode::Error {
127            return Err(Error::new("UND_ERR_REQ_RETRY", "redirect mode is error"));
128        }
129        let Some(location) = location else {
130            return Ok(false);
131        };
132        if self.redirects >= max {
133            return Err(Error::new("UND_ERR_REDIRECT", "redirect count exceeded"));
134        }
135        let next = self
136            .url
137            .join(location)
138            .map_err(|_| Error::new("ERR_INVALID_URL", "invalid redirect URL"))?;
139        if !matches!(next.scheme(), "http" | "https")
140            || !next.username().is_empty()
141            || next.password().is_some()
142        {
143            return Err(Error::new("UND_ERR_INVALID_ARG", "invalid redirect target"));
144        }
145        let rewrite = (matches!(status, 301 | 302) && self.method == "POST")
146            || (status == 303 && self.method != "GET" && self.method != "HEAD");
147        if !rewrite && !self.replayable {
148            return Err(Error::new(
149                "UND_ERR_REQ_RETRY",
150                "streaming body cannot be replayed",
151            ));
152        }
153        if self.url.origin() != next.origin() {
154            self.headers.retain(|h| {
155                ![
156                    "authorization",
157                    "proxy-authorization",
158                    "cookie",
159                    "cookie2",
160                    "host",
161                ]
162                .iter()
163                .any(|name| h.name.eq_ignore_ascii_case(name))
164            });
165        }
166        if rewrite {
167            self.method = "GET".into();
168            self.body.clear();
169            self.replayable = true;
170            self.headers.retain(|h| {
171                ![
172                    "content-encoding",
173                    "content-language",
174                    "content-location",
175                    "content-type",
176                    "content-length",
177                    "transfer-encoding",
178                ]
179                .iter()
180                .any(|name| h.name.eq_ignore_ascii_case(name))
181            });
182        }
183        self.url = next;
184        self.redirects += 1;
185        Ok(true)
186    }
187}
188fn authority(url: &Url) -> String {
189    let mut result = url.host_str().unwrap_or("").to_string();
190    if let Some(port) = url.port() {
191        result.push(':');
192        result.push_str(&port.to_string());
193    }
194    result
195}
196#[derive(Debug, Default, Clone)]
197pub struct ProxyEnvironment {
198    pub http_proxy: Option<String>,
199    pub https_proxy: Option<String>,
200    pub no_proxy: String,
201}
202impl ProxyEnvironment {
203    /// Values are supplied by the host from HTTP_PROXY/HTTPS_PROXY/NO_PROXY (or lowercase overrides).
204    /// Only `http://` proxies are accepted; see [`Self::proxy_for_tls_capable`].
205    pub fn proxy_for(&self, url: &Url) -> Result<Option<Url>> {
206        self.select(url, false)
207    }
208    /// [`Self::proxy_for`], also accepting `https://` proxies. The caller must
209    /// then secure the hop to the proxy itself: after connecting, perform the
210    /// [`Route::proxy_tls`] upgrade before anything else is written.
211    pub fn proxy_for_tls_capable(&self, url: &Url) -> Result<Option<Url>> {
212        self.select(url, true)
213    }
214    fn select(&self, url: &Url, https: bool) -> Result<Option<Url>> {
215        if bypass(url, &self.no_proxy) {
216            return Ok(None);
217        }
218        let proxy = if url.scheme() == "https" {
219            self.https_proxy.as_deref()
220        } else {
221            self.http_proxy.as_deref()
222        };
223        proxy
224            .filter(|p| !p.is_empty())
225            .map(|p| {
226                let parsed =
227                    Url::parse(p).map_err(|_| Error::new("ERR_INVALID_URL", "invalid proxy"))?;
228                let scheme_ok = parsed.scheme() == "http" || (https && parsed.scheme() == "https");
229                if !scheme_ok || parsed.host_str().is_none() {
230                    return Err(Error::new(
231                        "UND_ERR_NOT_SUPPORTED",
232                        if https {
233                            "only HTTP and HTTPS proxies are supported"
234                        } else {
235                            "only HTTP proxies are supported"
236                        },
237                    ));
238                }
239                Ok(parsed)
240            })
241            .transpose()
242    }
243}
244fn bypass(url: &Url, list: &str) -> bool {
245    let host = url.host_str().unwrap_or("");
246    list.split(',')
247        .map(str::trim)
248        .filter(|x| !x.is_empty())
249        .any(|entry| {
250            if entry == "*" {
251                return true;
252            }
253            let (domain, port) = if let Some((h, p)) = entry.rsplit_once(':') {
254                if let Ok(p) = p.parse::<u16>() {
255                    (h, Some(p))
256                } else {
257                    (entry, None)
258                }
259            } else {
260                (entry, None)
261            };
262            if port.is_some() && port != url.port_or_known_default() {
263                return false;
264            }
265            let domain = domain.trim_start_matches("*.").trim_start_matches('.');
266            host.eq_ignore_ascii_case(domain)
267                || (host.len() > domain.len()
268                    && host.as_bytes()[host.len() - domain.len() - 1] == b'.'
269                    && host[host.len() - domain.len()..].eq_ignore_ascii_case(domain))
270        })
271}
272#[derive(Debug, Clone, PartialEq, Eq)]
273pub struct PoolKey {
274    pub origin: String,
275    pub proxy: Option<String>,
276}
277impl PoolKey {
278    pub fn new(url: &Url, proxy: Option<&Url>) -> Self {
279        Self {
280            origin: url.origin().ascii_serialization(),
281            proxy: proxy.map(ToString::to_string),
282        }
283    }
284}
285/// Issued by [`Pool::acquire`]. Ids are never reused, so a stale id can never
286/// name a newer connection.
287#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
288pub struct ConnectionId(pub u64);
289/// A host-chosen key for one in-flight request's deadline in [`Pool`].
290#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
291pub struct RequestId(pub u64);
292
293/// A deadline heap keyed by `K`, holding at most one deadline per key.
294///
295/// [`next_timeout`](Self::next_timeout) is O(1) and
296/// [`set`](Self::set)/[`pop_expired`](Self::pop_expired) are O(log n), so a host
297/// with many connections or requests never rescans them to arm its timer.
298/// Replaced deadlines stay in the heap until they surface and are discarded;
299/// the heap is rebuilt whenever they outnumber the live ones, so its size stays
300/// within a constant factor of [`len`](Self::len).
301pub struct Deadlines<K> {
302    heap: BinaryHeap<Reverse<Entry<K>>>,
303    live: HashMap<K, (Instant, u64)>,
304    sequence: u64,
305}
306struct Entry<K> {
307    at: Instant,
308    sequence: u64,
309    key: K,
310}
311impl<K> PartialEq for Entry<K> {
312    fn eq(&self, other: &Self) -> bool {
313        self.cmp(other) == Ordering::Equal
314    }
315}
316impl<K> Eq for Entry<K> {}
317impl<K> PartialOrd for Entry<K> {
318    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
319        Some(self.cmp(other))
320    }
321}
322impl<K> Ord for Entry<K> {
323    // Sequence numbers are unique, so equal deadlines fire in the order set.
324    fn cmp(&self, other: &Self) -> Ordering {
325        (self.at, self.sequence).cmp(&(other.at, other.sequence))
326    }
327}
328impl<K: Copy + Eq + Hash> Default for Deadlines<K> {
329    fn default() -> Self {
330        Self::new()
331    }
332}
333impl<K: Copy + Eq + Hash> Deadlines<K> {
334    pub fn new() -> Self {
335        Self {
336            heap: BinaryHeap::new(),
337            live: HashMap::new(),
338            sequence: 0,
339        }
340    }
341    /// Replace `key`'s deadline; `None` removes it.
342    pub fn set(&mut self, key: K, deadline: Option<Instant>) {
343        match deadline {
344            Some(at) if self.live.get(&key).is_some_and(|&(d, _)| d == at) => return,
345            Some(at) => {
346                self.sequence += 1;
347                self.live.insert(key, (at, self.sequence));
348                self.heap.push(Reverse(Entry {
349                    at,
350                    sequence: self.sequence,
351                    key,
352                }));
353            }
354            None => {
355                if self.live.remove(&key).is_none() {
356                    return;
357                }
358            }
359        }
360        self.settle();
361    }
362    pub fn get(&self, key: K) -> Option<Instant> {
363        self.live.get(&key).map(|&(at, _)| at)
364    }
365    /// The earliest live deadline.
366    pub fn next_timeout(&self) -> Option<Instant> {
367        self.heap.peek().map(|Reverse(entry)| entry.at)
368    }
369    /// Remove and return one key whose deadline is at or before `now`, earliest
370    /// first. Call repeatedly until `None`.
371    pub fn pop_expired(&mut self, now: Instant) -> Option<K> {
372        if self.next_timeout()? > now {
373            return None;
374        }
375        let Reverse(entry) = self.heap.pop()?;
376        self.live.remove(&entry.key);
377        self.settle();
378        Some(entry.key)
379    }
380    pub fn len(&self) -> usize {
381        self.live.len()
382    }
383    pub fn is_empty(&self) -> bool {
384        self.live.is_empty()
385    }
386    /// Keep the top of the heap live, so `next_timeout` can simply peek.
387    fn settle(&mut self) {
388        if self.heap.len() > 2 * self.live.len() + 16 {
389            self.heap = self
390                .live
391                .iter()
392                .map(|(&key, &(at, sequence))| Reverse(Entry { at, sequence, key }))
393                .collect();
394            return;
395        }
396        while let Some(Reverse(top)) = self.heap.peek() {
397            if self.live.get(&top.key) == Some(&(top.at, top.sequence)) {
398                break;
399            }
400            self.heap.pop();
401        }
402    }
403    #[cfg(test)]
404    fn heap_len(&self) -> usize {
405        self.heap.len()
406    }
407}
408#[derive(Debug, Clone, Copy, PartialEq, Eq)]
409pub enum Acquire {
410    Connect(ConnectionId),
411    Reuse(ConnectionId),
412    Wait,
413}
414struct Slot {
415    id: ConnectionId,
416    key: PoolKey,
417    active: usize,
418    capacity: usize,
419    connecting: bool,
420    deadline: Option<Instant>,
421    closed: bool,
422}
423/// Connection reuse plus every client-wide deadline: idle connections and, when
424/// the host registers them, in-flight requests. One [`next_timeout`] covers all
425/// of them without visiting each connection or request.
426///
427/// [`next_timeout`]: Pool::next_timeout
428pub struct Pool {
429    slots: Vec<Slot>,
430    max_per_host: usize,
431    idle: Duration,
432    next: u64,
433    idle_deadlines: Deadlines<ConnectionId>,
434    request_deadlines: Deadlines<RequestId>,
435}
436impl Pool {
437    pub fn new(max_per_host: usize, idle: Duration) -> Self {
438        Self {
439            slots: Vec::new(),
440            max_per_host,
441            idle,
442            next: 1,
443            idle_deadlines: Deadlines::new(),
444            request_deadlines: Deadlines::new(),
445        }
446    }
447    /// A Connect reservation counts immediately, so concurrent callers cannot overbook.
448    pub fn acquire(&mut self, key: &PoolKey, now: Instant) -> Acquire {
449        if let Some(s) = self.slots.iter_mut().find(|s| {
450            !s.closed
451                && s.key == *key
452                && !s.connecting
453                && s.active < s.capacity
454                && s.deadline.is_none_or(|d| d > now)
455        }) {
456            s.active += 1;
457            s.deadline = None;
458            let id = s.id;
459            self.idle_deadlines.set(id, None);
460            return Acquire::Reuse(id);
461        }
462        if self
463            .slots
464            .iter()
465            .filter(|s| !s.closed && s.key == *key)
466            .count()
467            >= self.max_per_host
468        {
469            return Acquire::Wait;
470        }
471        let id = ConnectionId(self.next);
472        self.next += 1;
473        let slot = Slot {
474            id,
475            key: key.clone(),
476            active: 1,
477            capacity: 1,
478            connecting: true,
479            deadline: None,
480            closed: false,
481        };
482        if let Some(s) = self.slots.iter_mut().find(|s| s.closed) {
483            *s = slot;
484        } else {
485            self.slots.push(slot);
486        }
487        Acquire::Connect(id)
488    }
489    fn slot(&mut self, id: ConnectionId) -> Result<&mut Slot> {
490        self.slots
491            .iter_mut()
492            .find(|s| s.id == id && !s.closed)
493            .ok_or(Error::new("UND_ERR_CLOSED", "unknown connection"))
494    }
495    pub fn connected(
496        &mut self,
497        id: ConnectionId,
498        protocol: Protocol,
499        max_streams: usize,
500    ) -> Result<()> {
501        let s = self.slot(id)?;
502        s.connecting = false;
503        s.capacity = if protocol == Protocol::Http1 {
504            1
505        } else {
506            max_streams
507        };
508        Ok(())
509    }
510    pub fn release(&mut self, id: ConnectionId, reusable: bool, now: Instant) -> Result<()> {
511        let idle = self.idle;
512        let s = self.slot(id)?;
513        if s.active == 0 {
514            return Err(Error::new("UND_ERR_INVALID_ARG", "duplicate pool release"));
515        }
516        s.active -= 1;
517        if !reusable {
518            s.capacity = 0;
519        }
520        if s.active == 0 {
521            let deadline = if s.capacity == 0 { now } else { now + idle };
522            s.deadline = Some(deadline);
523            self.idle_deadlines.set(id, Some(deadline));
524        }
525        Ok(())
526    }
527    pub fn closed(&mut self, id: ConnectionId) -> Result<()> {
528        self.slot(id)?.closed = true;
529        self.idle_deadlines.set(id, None);
530        Ok(())
531    }
532    /// Whether `id` still names a live connection: acquired and neither closed,
533    /// forgotten nor expired by [`handle_timeout`](Self::handle_timeout).
534    pub fn contains(&self, id: ConnectionId) -> bool {
535        self.slots.iter().any(|s| s.id == id && !s.closed)
536    }
537    /// Drop `id` whatever its state, for a host whose own socket table no longer
538    /// has it (closed without telling the pool, or never found). Outstanding
539    /// acquisitions go with it, so its per-host place is free for the next
540    /// `acquire`. Returns whether `id` was live; a stale id is not an error.
541    pub fn forget(&mut self, id: ConnectionId) -> bool {
542        let live = self.slot(id).map(|s| s.closed = true).is_ok();
543        self.idle_deadlines.set(id, None);
544        live
545    }
546    /// The earliest idle-connection or registered request deadline, in O(1).
547    pub fn next_timeout(&self) -> Option<Instant> {
548        self.idle_deadlines
549            .next_timeout()
550            .into_iter()
551            .chain(self.request_deadlines.next_timeout())
552            .min()
553    }
554    /// Repeatedly call to obtain all Close requests at this time. No clock or timer is owned.
555    pub fn handle_timeout(&mut self, now: Instant) -> Option<ConnectionId> {
556        while let Some(id) = self.idle_deadlines.pop_expired(now) {
557            if let Ok(s) = self.slot(id) {
558                s.closed = true;
559                return Some(id);
560            }
561        }
562        None
563    }
564    /// Register (or with `None` clear) a request's next deadline, typically the
565    /// `next_timeout()` of its [`Lifecycle`] or [`Http1Connection`] after each
566    /// call that may move it. The pool never inspects the request itself.
567    pub fn set_request_deadline(&mut self, id: RequestId, deadline: Option<Instant>) {
568        self.request_deadlines.set(id, deadline);
569    }
570    pub fn request_deadline(&self, id: RequestId) -> Option<Instant> {
571        self.request_deadlines.get(id)
572    }
573    /// Repeatedly call to obtain every request whose registered deadline is at or
574    /// before `now`. Its registration is removed: call that request's
575    /// `handle_timeout(now)` and register its new `next_timeout()`, if any.
576    pub fn handle_request_timeout(&mut self, now: Instant) -> Option<RequestId> {
577        self.request_deadlines.pop_expired(now)
578    }
579}
580#[derive(Debug, Clone, Copy, PartialEq, Eq)]
581pub enum Phase {
582    Connect,
583    Headers,
584    Body,
585    Done,
586}
587#[derive(Debug, Clone, Copy, PartialEq, Eq)]
588pub enum Completion {
589    Success,
590    Error(Error),
591}
592pub struct Lifecycle {
593    phase: Phase,
594    deadline: Option<Instant>,
595    terminal: Option<Completion>,
596    delivered: bool,
597}
598impl Default for Lifecycle {
599    fn default() -> Self {
600        Self {
601            phase: Phase::Connect,
602            deadline: None,
603            terminal: None,
604            delivered: false,
605        }
606    }
607}
608impl Lifecycle {
609    pub fn transition(&mut self, phase: Phase, deadline: Option<Instant>) {
610        if self.terminal.is_none() {
611            self.phase = phase;
612            self.deadline = deadline;
613        }
614    }
615    pub fn next_timeout(&self) -> Option<Instant> {
616        self.deadline
617    }
618    pub fn handle_timeout(&mut self, now: Instant) {
619        if self.deadline.is_some_and(|d| d <= now) {
620            let (code, message) = match self.phase {
621                Phase::Connect => ("UND_ERR_CONNECT_TIMEOUT", "Connect Timeout Error"),
622                Phase::Headers => ("UND_ERR_HEADERS_TIMEOUT", "Headers Timeout Error"),
623                _ => ("UND_ERR_BODY_TIMEOUT", "Body Timeout Error"),
624            };
625            self.finish(Completion::Error(Error::new(code, message)));
626        }
627    }
628    pub fn abort(&mut self) {
629        self.finish(Completion::Error(Error::new(
630            "UND_ERR_ABORTED",
631            "Request aborted",
632        )));
633    }
634    pub fn finish(&mut self, result: Completion) {
635        if self.terminal.is_none() {
636            self.terminal = Some(result);
637            self.phase = Phase::Done;
638            self.deadline = None;
639        }
640    }
641    pub fn poll(&mut self) -> Option<Completion> {
642        if self.delivered {
643            return None;
644        }
645        let event = self.terminal?;
646        self.delivered = true;
647        Some(event)
648    }
649}
650/// After a CONNECT 2xx upgrade, the host feeds remaining bytes into TLS.
651#[derive(Debug, Clone, PartialEq, Eq)]
652pub enum TransportRequest {
653    Resolve { hostname: String, port: u16 },
654    UpgradeTls { server_name: String },
655    Ready(Protocol),
656}
657pub struct Route {
658    target: Url,
659    proxy: Option<Url>,
660    tunnel: bool,
661}
662impl Route {
663    pub fn new(target: Url, proxy: Option<Url>) -> Self {
664        Self {
665            target,
666            proxy,
667            tunnel: false,
668        }
669    }
670    pub fn resolve(&self) -> TransportRequest {
671        let peer = self.proxy.as_ref().unwrap_or(&self.target);
672        TransportRequest::Resolve {
673            hostname: peer.host_str().unwrap_or("").into(),
674            port: peer.port_or_known_default().unwrap_or(80),
675        }
676    }
677    /// For an `https://` proxy, the TLS upgrade to the proxy itself. It comes
678    /// first, right after the connection is established: the CONNECT tunnel or
679    /// the absolute-form request then runs inside it, and an `https` target's
680    /// own TLS session nests within that tunnel.
681    pub fn proxy_tls(&self) -> Option<TransportRequest> {
682        let proxy = self.proxy.as_ref().filter(|p| p.scheme() == "https")?;
683        Some(TransportRequest::UpgradeTls {
684            server_name: proxy.host_str().unwrap_or("").into(),
685        })
686    }
687    pub fn connect_head(&self, authorization: Option<&[u8]>) -> Option<Head> {
688        if self.proxy.is_none() || self.target.scheme() != "https" {
689            return None;
690        }
691        let target = format!(
692            "{}:{}",
693            self.target.host_str().unwrap_or(""),
694            self.target.port_or_known_default().unwrap_or(443)
695        );
696        let mut headers = vec![Header::new("host", &target)];
697        if let Some(value) = authorization {
698            headers.push(Header::new("proxy-authorization", value));
699        } else if let Some(value) = self.proxy.as_ref().and_then(proxy_authorization) {
700            headers.push(Header::new("proxy-authorization", value));
701        }
702        Some(Head {
703            method: "CONNECT".into(),
704            target,
705            status: 0,
706            version: 1,
707            headers,
708            keep_alive: true,
709        })
710    }
711    /// Serialize origin-form for direct/tunnel traffic and absolute-form for HTTP proxies.
712    /// Proxy credentials are never forwarded inside an HTTPS tunnel.
713    pub fn request_head(&self, request: &Request, authorization: Option<&[u8]>) -> Head {
714        let proxy_http = self.proxy.is_some() && self.target.scheme() == "http";
715        let mut head = request.head(proxy_http);
716        head.headers
717            .retain(|h| !h.name.eq_ignore_ascii_case("proxy-authorization"));
718        if proxy_http {
719            if let Some(auth) = authorization {
720                head.headers.push(Header::new("proxy-authorization", auth));
721            } else if let Some(value) = self.proxy.as_ref().and_then(proxy_authorization) {
722                head.headers.push(Header::new("proxy-authorization", value));
723            }
724        }
725        head
726    }
727    pub fn tunnel_response(&mut self, status: u16) -> Result<TransportRequest> {
728        if !(200..300).contains(&status) {
729            return Err(Error::new("UND_ERR_PRX_TLS", "proxy CONNECT rejected"));
730        }
731        self.tunnel = true;
732        Ok(TransportRequest::UpgradeTls {
733            server_name: self.target.host_str().unwrap_or("").into(),
734        })
735    }
736    pub fn connected(&self) -> Option<TransportRequest> {
737        if self.target.scheme() == "https" {
738            if self.proxy.is_some() && !self.tunnel {
739                None
740            } else {
741                Some(TransportRequest::UpgradeTls {
742                    server_name: self.target.host_str().unwrap_or("").into(),
743                })
744            }
745        } else {
746            Some(TransportRequest::Ready(Protocol::Http1))
747        }
748    }
749}
750pub fn transport_error(kind: std::io::ErrorKind) -> Error {
751    match kind {
752        std::io::ErrorKind::ConnectionRefused => Error::new("ECONNREFUSED", "connect ECONNREFUSED"),
753        std::io::ErrorKind::ConnectionReset => Error::new("ECONNRESET", "read ECONNRESET"),
754        std::io::ErrorKind::NotFound => Error::new("ENOTFOUND", "getaddrinfo ENOTFOUND"),
755        std::io::ErrorKind::TimedOut => Error::new("ETIMEDOUT", "connect ETIMEDOUT"),
756        _ => Error::new("UND_ERR_SOCKET", "socket error"),
757    }
758}
759
760/// A single non-pipelined HTTP/1 client connection. All byte buffers are reused.
761/// The host submits output, acknowledges completion, and supplies received bytes.
762/// Do not mutate the connection while a completion-layer write borrows `output()`.
763pub struct Http1Connection {
764    decoder: crate::http1::Decoder,
765    encoder: Option<crate::http1::Encoder>,
766    output: Vec<u8>,
767    output_pos: usize,
768    lifecycle: Lifecycle,
769    active: bool,
770    used: bool,
771    upload_finished: bool,
772    close_requested: bool,
773    expect_deadline: Option<Instant>,
774    waiting_continue: bool,
775}
776impl Http1Connection {
777    pub fn new(limits: crate::http1::Limits) -> Self {
778        Self {
779            decoder: crate::http1::Decoder::new(crate::http1::Mode::Response, limits),
780            encoder: None,
781            output: Vec::new(),
782            output_pos: 0,
783            lifecycle: Lifecycle::default(),
784            active: false,
785            used: false,
786            upload_finished: false,
787            close_requested: false,
788            expect_deadline: None,
789            waiting_continue: false,
790        }
791    }
792    pub fn start(
793        &mut self,
794        head: &Head,
795        length: crate::http1::BodyLength,
796        headers_deadline: Option<Instant>,
797        continue_deadline: Option<Instant>,
798    ) -> Result<()> {
799        if self.active || !self.output().is_empty() || self.used && !self.reusable() {
800            return Err(Error::new(
801                "UND_ERR_NOT_SUPPORTED",
802                "HTTP/1 pipelining is disabled or connection closed",
803            ));
804        }
805        let encoder = crate::http1::Encoder::start(head, length, &mut self.output)?;
806        if self.used {
807            self.decoder.reset()?;
808        }
809        self.decoder.response_to(&head.method);
810        self.encoder = Some(encoder);
811        self.active = true;
812        self.used = true;
813        self.upload_finished = false;
814        self.close_requested = head.token("connection", "close");
815        self.lifecycle = Lifecycle::default();
816        self.lifecycle.transition(Phase::Headers, headers_deadline);
817        self.waiting_continue = head.token("expect", "100-continue")
818            && !matches!(
819                length,
820                crate::http1::BodyLength::Empty | crate::http1::BodyLength::Known(0)
821            );
822        self.expect_deadline = if self.waiting_continue {
823            continue_deadline
824        } else {
825            None
826        };
827        Ok(())
828    }
829    pub fn can_send_body(&self) -> bool {
830        self.active
831            && !self.waiting_continue
832            && !self.upload_finished
833            && self.lifecycle.terminal.is_none()
834    }
835    pub fn send_body(&mut self, bytes: &[u8]) -> Result<()> {
836        if !self.can_send_body() {
837            return Err(Error::new(
838                "UND_ERR_INVALID_ARG",
839                "request body is not writable",
840            ));
841        }
842        self.encoder.as_mut().unwrap().body(bytes, &mut self.output)
843    }
844    pub fn finish_body(&mut self, trailers: &[Header]) -> Result<()> {
845        if !self.can_send_body() {
846            return Err(Error::new(
847                "UND_ERR_INVALID_ARG",
848                "request body is not writable",
849            ));
850        }
851        self.encoder
852            .as_mut()
853            .unwrap()
854            .finish(trailers, &mut self.output)?;
855        self.upload_finished = true;
856        Ok(())
857    }
858    pub fn output(&self) -> &[u8] {
859        &self.output[self.output_pos..]
860    }
861    pub fn consume_output(&mut self, n: usize) -> Result<()> {
862        if n > self.output().len() {
863            return Err(Error::new(
864                "UND_ERR_INVALID_ARG",
865                "write acknowledgement exceeds output",
866            ));
867        }
868        self.output_pos += n;
869        if self.output_pos == self.output.len() {
870            self.output.clear();
871            self.output_pos = 0;
872        }
873        Ok(())
874    }
875    pub fn receive<'a>(&mut self, input: &'a [u8]) -> Result<crate::http1::Step<'a>> {
876        if !self.active || self.lifecycle.terminal.is_some() {
877            return Err(Error::new("UND_ERR_CLOSED", "no active request"));
878        }
879        let step = match self.decoder.receive(input) {
880            Ok(step) => step,
881            Err(e) => {
882                self.fail(e);
883                return Err(e);
884            }
885        };
886        match &step.event {
887            Some(crate::http1::Event::Informational(h)) if h.status == 100 => {
888                self.waiting_continue = false;
889                self.expect_deadline = None;
890            }
891            Some(crate::http1::Event::Head(_)) => {
892                self.expect_deadline = None;
893                self.waiting_continue = false;
894                if !self.upload_finished {
895                    self.close_requested = true;
896                    self.upload_finished = true;
897                }
898                self.lifecycle.transition(Phase::Body, None);
899            }
900            Some(crate::http1::Event::End | crate::http1::Event::Upgrade) => {
901                self.lifecycle.finish(Completion::Success);
902                self.active = false;
903            }
904            _ => {}
905        }
906        Ok(step)
907    }
908    /// Refresh body timeout on progress using host-supplied time.
909    pub fn set_body_deadline(&mut self, deadline: Option<Instant>) {
910        self.lifecycle.transition(Phase::Body, deadline);
911    }
912    pub fn eof(&mut self) -> Result<()> {
913        match self.decoder.eof() {
914            Ok(()) => Ok(()),
915            Err(e) => {
916                self.fail(e);
917                Err(e)
918            }
919        }
920    }
921    pub fn next_timeout(&self) -> Option<Instant> {
922        self.lifecycle
923            .next_timeout()
924            .into_iter()
925            .chain(self.expect_deadline)
926            .min()
927    }
928    pub fn handle_timeout(&mut self, now: Instant) {
929        self.lifecycle.handle_timeout(now);
930        if self.lifecycle.terminal.is_some() {
931            self.active = false;
932            self.close_requested = true;
933            self.expect_deadline = None;
934        } else if self.expect_deadline.is_some_and(|d| d <= now) {
935            self.waiting_continue = false;
936            self.expect_deadline = None;
937        }
938    }
939    pub fn abort(&mut self) {
940        self.lifecycle.abort();
941        self.active = false;
942        self.close_requested = true;
943        self.expect_deadline = None;
944    }
945    pub fn fail(&mut self, error: Error) {
946        self.lifecycle.finish(Completion::Error(error));
947        self.active = false;
948        self.close_requested = true;
949        self.expect_deadline = None;
950    }
951    pub fn poll_completion(&mut self) -> Option<Completion> {
952        self.lifecycle.poll()
953    }
954    pub fn reusable(&self) -> bool {
955        !self.active && !self.close_requested && self.lifecycle.delivered && self.decoder.reusable()
956    }
957}
958
959fn proxy_authorization(proxy: &Url) -> Option<String> {
960    use base64::Engine;
961    if proxy.username().is_empty() && proxy.password().is_none() {
962        return None;
963    }
964    let mut credential =
965        percent_encoding::percent_decode_str(proxy.username()).collect::<Vec<u8>>();
966    credential.push(b':');
967    credential.extend(percent_encoding::percent_decode_str(
968        proxy.password().unwrap_or(""),
969    ));
970    Some(format!(
971        "Basic {}",
972        base64::engine::general_purpose::STANDARD.encode(credential)
973    ))
974}
975
976#[cfg(test)]
977mod tests {
978    use super::*;
979
980    #[test]
981    fn replaced_deadlines_do_not_accumulate() {
982        let now = Instant::now();
983        let mut deadlines = Deadlines::new();
984        for key in 0..100u32 {
985            deadlines.set(key, Some(now + Duration::from_secs(1000)));
986        }
987        // A request refreshing its body deadline on every chunk, far from the top.
988        for step in 0..100_000u64 {
989            deadlines.set(7, Some(now + Duration::from_secs(2000 + step)));
990            assert!(deadlines.heap_len() <= 2 * deadlines.len() + 17);
991        }
992        assert_eq!(deadlines.len(), 100);
993        assert_eq!(
994            deadlines.next_timeout(),
995            Some(now + Duration::from_secs(1000))
996        );
997        let late = now + Duration::from_secs(1_000_000);
998        let mut popped = 0;
999        while let Some(key) = deadlines.pop_expired(late) {
1000            assert_eq!(key == 7, popped == 99, "the refreshed key fires last");
1001            popped += 1;
1002        }
1003        assert_eq!(popped, 100);
1004        assert_eq!(deadlines.heap_len(), 0);
1005    }
1006}