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    pub fn proxy_for(&self, url: &Url) -> Result<Option<Url>> {
205        if bypass(url, &self.no_proxy) {
206            return Ok(None);
207        }
208        let proxy = if url.scheme() == "https" {
209            self.https_proxy.as_deref()
210        } else {
211            self.http_proxy.as_deref()
212        };
213        proxy
214            .filter(|p| !p.is_empty())
215            .map(|p| {
216                let parsed =
217                    Url::parse(p).map_err(|_| Error::new("ERR_INVALID_URL", "invalid proxy"))?;
218                if parsed.scheme() != "http" || parsed.host_str().is_none() {
219                    return Err(Error::new(
220                        "UND_ERR_NOT_SUPPORTED",
221                        "only HTTP proxies are supported",
222                    ));
223                }
224                Ok(parsed)
225            })
226            .transpose()
227    }
228}
229fn bypass(url: &Url, list: &str) -> bool {
230    let host = url.host_str().unwrap_or("");
231    list.split(',')
232        .map(str::trim)
233        .filter(|x| !x.is_empty())
234        .any(|entry| {
235            if entry == "*" {
236                return true;
237            }
238            let (domain, port) = if let Some((h, p)) = entry.rsplit_once(':') {
239                if let Ok(p) = p.parse::<u16>() {
240                    (h, Some(p))
241                } else {
242                    (entry, None)
243                }
244            } else {
245                (entry, None)
246            };
247            if port.is_some() && port != url.port_or_known_default() {
248                return false;
249            }
250            let domain = domain.trim_start_matches("*.").trim_start_matches('.');
251            host.eq_ignore_ascii_case(domain)
252                || (host.len() > domain.len()
253                    && host.as_bytes()[host.len() - domain.len() - 1] == b'.'
254                    && host[host.len() - domain.len()..].eq_ignore_ascii_case(domain))
255        })
256}
257#[derive(Debug, Clone, PartialEq, Eq)]
258pub struct PoolKey {
259    pub origin: String,
260    pub proxy: Option<String>,
261}
262impl PoolKey {
263    pub fn new(url: &Url, proxy: Option<&Url>) -> Self {
264        Self {
265            origin: url.origin().ascii_serialization(),
266            proxy: proxy.map(ToString::to_string),
267        }
268    }
269}
270/// Issued by [`Pool::acquire`]. Ids are never reused, so a stale id can never
271/// name a newer connection.
272#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
273pub struct ConnectionId(pub u64);
274/// A host-chosen key for one in-flight request's deadline in [`Pool`].
275#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
276pub struct RequestId(pub u64);
277
278/// A deadline heap keyed by `K`, holding at most one deadline per key.
279///
280/// [`next_timeout`](Self::next_timeout) is O(1) and
281/// [`set`](Self::set)/[`pop_expired`](Self::pop_expired) are O(log n), so a host
282/// with many connections or requests never rescans them to arm its timer.
283/// Replaced deadlines stay in the heap until they surface and are discarded;
284/// the heap is rebuilt whenever they outnumber the live ones, so its size stays
285/// within a constant factor of [`len`](Self::len).
286pub struct Deadlines<K> {
287    heap: BinaryHeap<Reverse<Entry<K>>>,
288    live: HashMap<K, (Instant, u64)>,
289    sequence: u64,
290}
291struct Entry<K> {
292    at: Instant,
293    sequence: u64,
294    key: K,
295}
296impl<K> PartialEq for Entry<K> {
297    fn eq(&self, other: &Self) -> bool {
298        self.cmp(other) == Ordering::Equal
299    }
300}
301impl<K> Eq for Entry<K> {}
302impl<K> PartialOrd for Entry<K> {
303    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
304        Some(self.cmp(other))
305    }
306}
307impl<K> Ord for Entry<K> {
308    // Sequence numbers are unique, so equal deadlines fire in the order set.
309    fn cmp(&self, other: &Self) -> Ordering {
310        (self.at, self.sequence).cmp(&(other.at, other.sequence))
311    }
312}
313impl<K: Copy + Eq + Hash> Default for Deadlines<K> {
314    fn default() -> Self {
315        Self::new()
316    }
317}
318impl<K: Copy + Eq + Hash> Deadlines<K> {
319    pub fn new() -> Self {
320        Self {
321            heap: BinaryHeap::new(),
322            live: HashMap::new(),
323            sequence: 0,
324        }
325    }
326    /// Replace `key`'s deadline; `None` removes it.
327    pub fn set(&mut self, key: K, deadline: Option<Instant>) {
328        match deadline {
329            Some(at) if self.live.get(&key).is_some_and(|&(d, _)| d == at) => return,
330            Some(at) => {
331                self.sequence += 1;
332                self.live.insert(key, (at, self.sequence));
333                self.heap.push(Reverse(Entry {
334                    at,
335                    sequence: self.sequence,
336                    key,
337                }));
338            }
339            None => {
340                if self.live.remove(&key).is_none() {
341                    return;
342                }
343            }
344        }
345        self.settle();
346    }
347    pub fn get(&self, key: K) -> Option<Instant> {
348        self.live.get(&key).map(|&(at, _)| at)
349    }
350    /// The earliest live deadline.
351    pub fn next_timeout(&self) -> Option<Instant> {
352        self.heap.peek().map(|Reverse(entry)| entry.at)
353    }
354    /// Remove and return one key whose deadline is at or before `now`, earliest
355    /// first. Call repeatedly until `None`.
356    pub fn pop_expired(&mut self, now: Instant) -> Option<K> {
357        if self.next_timeout()? > now {
358            return None;
359        }
360        let Reverse(entry) = self.heap.pop()?;
361        self.live.remove(&entry.key);
362        self.settle();
363        Some(entry.key)
364    }
365    pub fn len(&self) -> usize {
366        self.live.len()
367    }
368    pub fn is_empty(&self) -> bool {
369        self.live.is_empty()
370    }
371    /// Keep the top of the heap live, so `next_timeout` can simply peek.
372    fn settle(&mut self) {
373        if self.heap.len() > 2 * self.live.len() + 16 {
374            self.heap = self
375                .live
376                .iter()
377                .map(|(&key, &(at, sequence))| Reverse(Entry { at, sequence, key }))
378                .collect();
379            return;
380        }
381        while let Some(Reverse(top)) = self.heap.peek() {
382            if self.live.get(&top.key) == Some(&(top.at, top.sequence)) {
383                break;
384            }
385            self.heap.pop();
386        }
387    }
388    #[cfg(test)]
389    fn heap_len(&self) -> usize {
390        self.heap.len()
391    }
392}
393#[derive(Debug, Clone, Copy, PartialEq, Eq)]
394pub enum Acquire {
395    Connect(ConnectionId),
396    Reuse(ConnectionId),
397    Wait,
398}
399struct Slot {
400    id: ConnectionId,
401    key: PoolKey,
402    active: usize,
403    capacity: usize,
404    connecting: bool,
405    deadline: Option<Instant>,
406    closed: bool,
407}
408/// Connection reuse plus every client-wide deadline: idle connections and, when
409/// the host registers them, in-flight requests. One [`next_timeout`] covers all
410/// of them without visiting each connection or request.
411///
412/// [`next_timeout`]: Pool::next_timeout
413pub struct Pool {
414    slots: Vec<Slot>,
415    max_per_host: usize,
416    idle: Duration,
417    next: u64,
418    idle_deadlines: Deadlines<ConnectionId>,
419    request_deadlines: Deadlines<RequestId>,
420}
421impl Pool {
422    pub fn new(max_per_host: usize, idle: Duration) -> Self {
423        Self {
424            slots: Vec::new(),
425            max_per_host,
426            idle,
427            next: 1,
428            idle_deadlines: Deadlines::new(),
429            request_deadlines: Deadlines::new(),
430        }
431    }
432    /// A Connect reservation counts immediately, so concurrent callers cannot overbook.
433    pub fn acquire(&mut self, key: &PoolKey, now: Instant) -> Acquire {
434        if let Some(s) = self.slots.iter_mut().find(|s| {
435            !s.closed
436                && s.key == *key
437                && !s.connecting
438                && s.active < s.capacity
439                && s.deadline.is_none_or(|d| d > now)
440        }) {
441            s.active += 1;
442            s.deadline = None;
443            let id = s.id;
444            self.idle_deadlines.set(id, None);
445            return Acquire::Reuse(id);
446        }
447        if self
448            .slots
449            .iter()
450            .filter(|s| !s.closed && s.key == *key)
451            .count()
452            >= self.max_per_host
453        {
454            return Acquire::Wait;
455        }
456        let id = ConnectionId(self.next);
457        self.next += 1;
458        let slot = Slot {
459            id,
460            key: key.clone(),
461            active: 1,
462            capacity: 1,
463            connecting: true,
464            deadline: None,
465            closed: false,
466        };
467        if let Some(s) = self.slots.iter_mut().find(|s| s.closed) {
468            *s = slot;
469        } else {
470            self.slots.push(slot);
471        }
472        Acquire::Connect(id)
473    }
474    fn slot(&mut self, id: ConnectionId) -> Result<&mut Slot> {
475        self.slots
476            .iter_mut()
477            .find(|s| s.id == id && !s.closed)
478            .ok_or(Error::new("UND_ERR_CLOSED", "unknown connection"))
479    }
480    pub fn connected(
481        &mut self,
482        id: ConnectionId,
483        protocol: Protocol,
484        max_streams: usize,
485    ) -> Result<()> {
486        let s = self.slot(id)?;
487        s.connecting = false;
488        s.capacity = if protocol == Protocol::Http1 {
489            1
490        } else {
491            max_streams
492        };
493        Ok(())
494    }
495    pub fn release(&mut self, id: ConnectionId, reusable: bool, now: Instant) -> Result<()> {
496        let idle = self.idle;
497        let s = self.slot(id)?;
498        if s.active == 0 {
499            return Err(Error::new("UND_ERR_INVALID_ARG", "duplicate pool release"));
500        }
501        s.active -= 1;
502        if !reusable {
503            s.capacity = 0;
504        }
505        if s.active == 0 {
506            let deadline = if s.capacity == 0 { now } else { now + idle };
507            s.deadline = Some(deadline);
508            self.idle_deadlines.set(id, Some(deadline));
509        }
510        Ok(())
511    }
512    pub fn closed(&mut self, id: ConnectionId) -> Result<()> {
513        self.slot(id)?.closed = true;
514        self.idle_deadlines.set(id, None);
515        Ok(())
516    }
517    /// Whether `id` still names a live connection: acquired and neither closed,
518    /// forgotten nor expired by [`handle_timeout`](Self::handle_timeout).
519    pub fn contains(&self, id: ConnectionId) -> bool {
520        self.slots.iter().any(|s| s.id == id && !s.closed)
521    }
522    /// Drop `id` whatever its state, for a host whose own socket table no longer
523    /// has it (closed without telling the pool, or never found). Outstanding
524    /// acquisitions go with it, so its per-host place is free for the next
525    /// `acquire`. Returns whether `id` was live; a stale id is not an error.
526    pub fn forget(&mut self, id: ConnectionId) -> bool {
527        let live = self.slot(id).map(|s| s.closed = true).is_ok();
528        self.idle_deadlines.set(id, None);
529        live
530    }
531    /// The earliest idle-connection or registered request deadline, in O(1).
532    pub fn next_timeout(&self) -> Option<Instant> {
533        self.idle_deadlines
534            .next_timeout()
535            .into_iter()
536            .chain(self.request_deadlines.next_timeout())
537            .min()
538    }
539    /// Repeatedly call to obtain all Close requests at this time. No clock or timer is owned.
540    pub fn handle_timeout(&mut self, now: Instant) -> Option<ConnectionId> {
541        while let Some(id) = self.idle_deadlines.pop_expired(now) {
542            if let Ok(s) = self.slot(id) {
543                s.closed = true;
544                return Some(id);
545            }
546        }
547        None
548    }
549    /// Register (or with `None` clear) a request's next deadline, typically the
550    /// `next_timeout()` of its [`Lifecycle`] or [`Http1Connection`] after each
551    /// call that may move it. The pool never inspects the request itself.
552    pub fn set_request_deadline(&mut self, id: RequestId, deadline: Option<Instant>) {
553        self.request_deadlines.set(id, deadline);
554    }
555    pub fn request_deadline(&self, id: RequestId) -> Option<Instant> {
556        self.request_deadlines.get(id)
557    }
558    /// Repeatedly call to obtain every request whose registered deadline is at or
559    /// before `now`. Its registration is removed: call that request's
560    /// `handle_timeout(now)` and register its new `next_timeout()`, if any.
561    pub fn handle_request_timeout(&mut self, now: Instant) -> Option<RequestId> {
562        self.request_deadlines.pop_expired(now)
563    }
564}
565#[derive(Debug, Clone, Copy, PartialEq, Eq)]
566pub enum Phase {
567    Connect,
568    Headers,
569    Body,
570    Done,
571}
572#[derive(Debug, Clone, Copy, PartialEq, Eq)]
573pub enum Completion {
574    Success,
575    Error(Error),
576}
577pub struct Lifecycle {
578    phase: Phase,
579    deadline: Option<Instant>,
580    terminal: Option<Completion>,
581    delivered: bool,
582}
583impl Default for Lifecycle {
584    fn default() -> Self {
585        Self {
586            phase: Phase::Connect,
587            deadline: None,
588            terminal: None,
589            delivered: false,
590        }
591    }
592}
593impl Lifecycle {
594    pub fn transition(&mut self, phase: Phase, deadline: Option<Instant>) {
595        if self.terminal.is_none() {
596            self.phase = phase;
597            self.deadline = deadline;
598        }
599    }
600    pub fn next_timeout(&self) -> Option<Instant> {
601        self.deadline
602    }
603    pub fn handle_timeout(&mut self, now: Instant) {
604        if self.deadline.is_some_and(|d| d <= now) {
605            let (code, message) = match self.phase {
606                Phase::Connect => ("UND_ERR_CONNECT_TIMEOUT", "Connect Timeout Error"),
607                Phase::Headers => ("UND_ERR_HEADERS_TIMEOUT", "Headers Timeout Error"),
608                _ => ("UND_ERR_BODY_TIMEOUT", "Body Timeout Error"),
609            };
610            self.finish(Completion::Error(Error::new(code, message)));
611        }
612    }
613    pub fn abort(&mut self) {
614        self.finish(Completion::Error(Error::new(
615            "UND_ERR_ABORTED",
616            "Request aborted",
617        )));
618    }
619    pub fn finish(&mut self, result: Completion) {
620        if self.terminal.is_none() {
621            self.terminal = Some(result);
622            self.phase = Phase::Done;
623            self.deadline = None;
624        }
625    }
626    pub fn poll(&mut self) -> Option<Completion> {
627        if self.delivered {
628            return None;
629        }
630        let event = self.terminal?;
631        self.delivered = true;
632        Some(event)
633    }
634}
635/// After a CONNECT 2xx upgrade, the host feeds remaining bytes into TLS.
636#[derive(Debug, Clone, PartialEq, Eq)]
637pub enum TransportRequest {
638    Resolve { hostname: String, port: u16 },
639    UpgradeTls { server_name: String },
640    Ready(Protocol),
641}
642pub struct Route {
643    target: Url,
644    proxy: Option<Url>,
645    tunnel: bool,
646}
647impl Route {
648    pub fn new(target: Url, proxy: Option<Url>) -> Self {
649        Self {
650            target,
651            proxy,
652            tunnel: false,
653        }
654    }
655    pub fn resolve(&self) -> TransportRequest {
656        let peer = self.proxy.as_ref().unwrap_or(&self.target);
657        TransportRequest::Resolve {
658            hostname: peer.host_str().unwrap_or("").into(),
659            port: peer.port_or_known_default().unwrap_or(80),
660        }
661    }
662    pub fn connect_head(&self, authorization: Option<&[u8]>) -> Option<Head> {
663        if self.proxy.is_none() || self.target.scheme() != "https" {
664            return None;
665        }
666        let target = format!(
667            "{}:{}",
668            self.target.host_str().unwrap_or(""),
669            self.target.port_or_known_default().unwrap_or(443)
670        );
671        let mut headers = vec![Header::new("host", &target)];
672        if let Some(value) = authorization {
673            headers.push(Header::new("proxy-authorization", value));
674        } else if let Some(value) = self.proxy.as_ref().and_then(proxy_authorization) {
675            headers.push(Header::new("proxy-authorization", value));
676        }
677        Some(Head {
678            method: "CONNECT".into(),
679            target,
680            status: 0,
681            version: 1,
682            headers,
683            keep_alive: true,
684        })
685    }
686    /// Serialize origin-form for direct/tunnel traffic and absolute-form for HTTP proxies.
687    /// Proxy credentials are never forwarded inside an HTTPS tunnel.
688    pub fn request_head(&self, request: &Request, authorization: Option<&[u8]>) -> Head {
689        let proxy_http = self.proxy.is_some() && self.target.scheme() == "http";
690        let mut head = request.head(proxy_http);
691        head.headers
692            .retain(|h| !h.name.eq_ignore_ascii_case("proxy-authorization"));
693        if proxy_http {
694            if let Some(auth) = authorization {
695                head.headers.push(Header::new("proxy-authorization", auth));
696            } else if let Some(value) = self.proxy.as_ref().and_then(proxy_authorization) {
697                head.headers.push(Header::new("proxy-authorization", value));
698            }
699        }
700        head
701    }
702    pub fn tunnel_response(&mut self, status: u16) -> Result<TransportRequest> {
703        if !(200..300).contains(&status) {
704            return Err(Error::new("UND_ERR_PRX_TLS", "proxy CONNECT rejected"));
705        }
706        self.tunnel = true;
707        Ok(TransportRequest::UpgradeTls {
708            server_name: self.target.host_str().unwrap_or("").into(),
709        })
710    }
711    pub fn connected(&self) -> Option<TransportRequest> {
712        if self.target.scheme() == "https" {
713            if self.proxy.is_some() && !self.tunnel {
714                None
715            } else {
716                Some(TransportRequest::UpgradeTls {
717                    server_name: self.target.host_str().unwrap_or("").into(),
718                })
719            }
720        } else {
721            Some(TransportRequest::Ready(Protocol::Http1))
722        }
723    }
724}
725pub fn transport_error(kind: std::io::ErrorKind) -> Error {
726    match kind {
727        std::io::ErrorKind::ConnectionRefused => Error::new("ECONNREFUSED", "connect ECONNREFUSED"),
728        std::io::ErrorKind::ConnectionReset => Error::new("ECONNRESET", "read ECONNRESET"),
729        std::io::ErrorKind::NotFound => Error::new("ENOTFOUND", "getaddrinfo ENOTFOUND"),
730        std::io::ErrorKind::TimedOut => Error::new("ETIMEDOUT", "connect ETIMEDOUT"),
731        _ => Error::new("UND_ERR_SOCKET", "socket error"),
732    }
733}
734
735/// A single non-pipelined HTTP/1 client connection. All byte buffers are reused.
736/// The host submits output, acknowledges completion, and supplies received bytes.
737/// Do not mutate the connection while a completion-layer write borrows `output()`.
738pub struct Http1Connection {
739    decoder: crate::http1::Decoder,
740    encoder: Option<crate::http1::Encoder>,
741    output: Vec<u8>,
742    output_pos: usize,
743    lifecycle: Lifecycle,
744    active: bool,
745    used: bool,
746    upload_finished: bool,
747    close_requested: bool,
748    expect_deadline: Option<Instant>,
749    waiting_continue: bool,
750}
751impl Http1Connection {
752    pub fn new(limits: crate::http1::Limits) -> Self {
753        Self {
754            decoder: crate::http1::Decoder::new(crate::http1::Mode::Response, limits),
755            encoder: None,
756            output: Vec::new(),
757            output_pos: 0,
758            lifecycle: Lifecycle::default(),
759            active: false,
760            used: false,
761            upload_finished: false,
762            close_requested: false,
763            expect_deadline: None,
764            waiting_continue: false,
765        }
766    }
767    pub fn start(
768        &mut self,
769        head: &Head,
770        length: crate::http1::BodyLength,
771        headers_deadline: Option<Instant>,
772        continue_deadline: Option<Instant>,
773    ) -> Result<()> {
774        if self.active || !self.output().is_empty() || self.used && !self.reusable() {
775            return Err(Error::new(
776                "UND_ERR_NOT_SUPPORTED",
777                "HTTP/1 pipelining is disabled or connection closed",
778            ));
779        }
780        let encoder = crate::http1::Encoder::start(head, length, &mut self.output)?;
781        if self.used {
782            self.decoder.reset()?;
783        }
784        self.decoder.response_to(&head.method);
785        self.encoder = Some(encoder);
786        self.active = true;
787        self.used = true;
788        self.upload_finished = false;
789        self.close_requested = head.token("connection", "close");
790        self.lifecycle = Lifecycle::default();
791        self.lifecycle.transition(Phase::Headers, headers_deadline);
792        self.waiting_continue = head.token("expect", "100-continue")
793            && !matches!(
794                length,
795                crate::http1::BodyLength::Empty | crate::http1::BodyLength::Known(0)
796            );
797        self.expect_deadline = if self.waiting_continue {
798            continue_deadline
799        } else {
800            None
801        };
802        Ok(())
803    }
804    pub fn can_send_body(&self) -> bool {
805        self.active
806            && !self.waiting_continue
807            && !self.upload_finished
808            && self.lifecycle.terminal.is_none()
809    }
810    pub fn send_body(&mut self, bytes: &[u8]) -> Result<()> {
811        if !self.can_send_body() {
812            return Err(Error::new(
813                "UND_ERR_INVALID_ARG",
814                "request body is not writable",
815            ));
816        }
817        self.encoder.as_mut().unwrap().body(bytes, &mut self.output)
818    }
819    pub fn finish_body(&mut self, trailers: &[Header]) -> Result<()> {
820        if !self.can_send_body() {
821            return Err(Error::new(
822                "UND_ERR_INVALID_ARG",
823                "request body is not writable",
824            ));
825        }
826        self.encoder
827            .as_mut()
828            .unwrap()
829            .finish(trailers, &mut self.output)?;
830        self.upload_finished = true;
831        Ok(())
832    }
833    pub fn output(&self) -> &[u8] {
834        &self.output[self.output_pos..]
835    }
836    pub fn consume_output(&mut self, n: usize) -> Result<()> {
837        if n > self.output().len() {
838            return Err(Error::new(
839                "UND_ERR_INVALID_ARG",
840                "write acknowledgement exceeds output",
841            ));
842        }
843        self.output_pos += n;
844        if self.output_pos == self.output.len() {
845            self.output.clear();
846            self.output_pos = 0;
847        }
848        Ok(())
849    }
850    pub fn receive<'a>(&mut self, input: &'a [u8]) -> Result<crate::http1::Step<'a>> {
851        if !self.active || self.lifecycle.terminal.is_some() {
852            return Err(Error::new("UND_ERR_CLOSED", "no active request"));
853        }
854        let step = match self.decoder.receive(input) {
855            Ok(step) => step,
856            Err(e) => {
857                self.fail(e);
858                return Err(e);
859            }
860        };
861        match &step.event {
862            Some(crate::http1::Event::Informational(h)) if h.status == 100 => {
863                self.waiting_continue = false;
864                self.expect_deadline = None;
865            }
866            Some(crate::http1::Event::Head(_)) => {
867                self.expect_deadline = None;
868                self.waiting_continue = false;
869                if !self.upload_finished {
870                    self.close_requested = true;
871                    self.upload_finished = true;
872                }
873                self.lifecycle.transition(Phase::Body, None);
874            }
875            Some(crate::http1::Event::End | crate::http1::Event::Upgrade) => {
876                self.lifecycle.finish(Completion::Success);
877                self.active = false;
878            }
879            _ => {}
880        }
881        Ok(step)
882    }
883    /// Refresh body timeout on progress using host-supplied time.
884    pub fn set_body_deadline(&mut self, deadline: Option<Instant>) {
885        self.lifecycle.transition(Phase::Body, deadline);
886    }
887    pub fn eof(&mut self) -> Result<()> {
888        match self.decoder.eof() {
889            Ok(()) => Ok(()),
890            Err(e) => {
891                self.fail(e);
892                Err(e)
893            }
894        }
895    }
896    pub fn next_timeout(&self) -> Option<Instant> {
897        self.lifecycle
898            .next_timeout()
899            .into_iter()
900            .chain(self.expect_deadline)
901            .min()
902    }
903    pub fn handle_timeout(&mut self, now: Instant) {
904        self.lifecycle.handle_timeout(now);
905        if self.lifecycle.terminal.is_some() {
906            self.active = false;
907            self.close_requested = true;
908            self.expect_deadline = None;
909        } else if self.expect_deadline.is_some_and(|d| d <= now) {
910            self.waiting_continue = false;
911            self.expect_deadline = None;
912        }
913    }
914    pub fn abort(&mut self) {
915        self.lifecycle.abort();
916        self.active = false;
917        self.close_requested = true;
918        self.expect_deadline = None;
919    }
920    pub fn fail(&mut self, error: Error) {
921        self.lifecycle.finish(Completion::Error(error));
922        self.active = false;
923        self.close_requested = true;
924        self.expect_deadline = None;
925    }
926    pub fn poll_completion(&mut self) -> Option<Completion> {
927        self.lifecycle.poll()
928    }
929    pub fn reusable(&self) -> bool {
930        !self.active && !self.close_requested && self.lifecycle.delivered && self.decoder.reusable()
931    }
932}
933
934fn proxy_authorization(proxy: &Url) -> Option<String> {
935    use base64::Engine;
936    if proxy.username().is_empty() && proxy.password().is_none() {
937        return None;
938    }
939    let mut credential =
940        percent_encoding::percent_decode_str(proxy.username()).collect::<Vec<u8>>();
941    credential.push(b':');
942    credential.extend(percent_encoding::percent_decode_str(
943        proxy.password().unwrap_or(""),
944    ));
945    Some(format!(
946        "Basic {}",
947        base64::engine::general_purpose::STANDARD.encode(credential)
948    ))
949}
950
951#[cfg(test)]
952mod tests {
953    use super::*;
954
955    #[test]
956    fn replaced_deadlines_do_not_accumulate() {
957        let now = Instant::now();
958        let mut deadlines = Deadlines::new();
959        for key in 0..100u32 {
960            deadlines.set(key, Some(now + Duration::from_secs(1000)));
961        }
962        // A request refreshing its body deadline on every chunk, far from the top.
963        for step in 0..100_000u64 {
964            deadlines.set(7, Some(now + Duration::from_secs(2000 + step)));
965            assert!(deadlines.heap_len() <= 2 * deadlines.len() + 17);
966        }
967        assert_eq!(deadlines.len(), 100);
968        assert_eq!(
969            deadlines.next_timeout(),
970            Some(now + Duration::from_secs(1000))
971        );
972        let late = now + Duration::from_secs(1_000_000);
973        let mut popped = 0;
974        while let Some(key) = deadlines.pop_expired(late) {
975            assert_eq!(key == 7, popped == 99, "the refreshed key fires last");
976            popped += 1;
977        }
978        assert_eq!(popped, 100);
979        assert_eq!(deadlines.heap_len(), 0);
980    }
981}