hyper_util/client/legacy/connect/
mod.rs1use std::{
66    fmt::{self, Formatter},
67    sync::{
68        atomic::{AtomicBool, Ordering},
69        Arc,
70    },
71};
72
73use ::http::Extensions;
74
75#[cfg(feature = "tokio")]
76pub use self::http::{HttpConnector, HttpInfo};
77
78#[cfg(feature = "tokio")]
79pub mod dns;
80#[cfg(feature = "tokio")]
81mod http;
82
83pub mod proxy;
84
85pub(crate) mod capture;
86pub use capture::{capture_connection, CaptureConnection};
87
88pub use self::sealed::Connect;
89
90pub trait Connection {
92    fn connected(&self) -> Connected;
94}
95
96#[derive(Debug)]
101pub struct Connected {
102    pub(super) alpn: Alpn,
103    pub(super) is_proxied: bool,
104    pub(super) extra: Option<Extra>,
105    pub(super) poisoned: PoisonPill,
106}
107
108#[derive(Clone)]
109pub(crate) struct PoisonPill {
110    poisoned: Arc<AtomicBool>,
111}
112
113impl fmt::Debug for PoisonPill {
114    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
115        write!(
117            f,
118            "PoisonPill@{:p} {{ poisoned: {} }}",
119            self.poisoned,
120            self.poisoned.load(Ordering::Relaxed)
121        )
122    }
123}
124
125impl PoisonPill {
126    pub(crate) fn healthy() -> Self {
127        Self {
128            poisoned: Arc::new(AtomicBool::new(false)),
129        }
130    }
131    pub(crate) fn poison(&self) {
132        self.poisoned.store(true, Ordering::Relaxed)
133    }
134
135    pub(crate) fn poisoned(&self) -> bool {
136        self.poisoned.load(Ordering::Relaxed)
137    }
138}
139
140pub(super) struct Extra(Box<dyn ExtraInner>);
141
142#[derive(Clone, Copy, Debug, PartialEq)]
143pub(super) enum Alpn {
144    H2,
145    None,
146}
147
148impl Connected {
149    pub fn new() -> Connected {
151        Connected {
152            alpn: Alpn::None,
153            is_proxied: false,
154            extra: None,
155            poisoned: PoisonPill::healthy(),
156        }
157    }
158
159    pub fn proxy(mut self, is_proxied: bool) -> Connected {
178        self.is_proxied = is_proxied;
179        self
180    }
181
182    pub fn is_proxied(&self) -> bool {
184        self.is_proxied
185    }
186
187    pub fn extra<T: Clone + Send + Sync + 'static>(mut self, extra: T) -> Connected {
189        if let Some(prev) = self.extra {
190            self.extra = Some(Extra(Box::new(ExtraChain(prev.0, extra))));
191        } else {
192            self.extra = Some(Extra(Box::new(ExtraEnvelope(extra))));
193        }
194        self
195    }
196
197    pub fn get_extras(&self, extensions: &mut Extensions) {
199        if let Some(extra) = &self.extra {
200            extra.set(extensions);
201        }
202    }
203
204    pub fn negotiated_h2(mut self) -> Connected {
206        self.alpn = Alpn::H2;
207        self
208    }
209
210    pub fn is_negotiated_h2(&self) -> bool {
212        self.alpn == Alpn::H2
213    }
214
215    pub fn poison(&self) {
219        self.poisoned.poison();
220        tracing::debug!(
221            poison_pill = ?self.poisoned, "connection was poisoned. this connection will not be reused for subsequent requests"
222        );
223    }
224
225    pub(super) fn clone(&self) -> Connected {
228        Connected {
229            alpn: self.alpn,
230            is_proxied: self.is_proxied,
231            extra: self.extra.clone(),
232            poisoned: self.poisoned.clone(),
233        }
234    }
235}
236
237impl Extra {
240    pub(super) fn set(&self, res: &mut Extensions) {
241        self.0.set(res);
242    }
243}
244
245impl Clone for Extra {
246    fn clone(&self) -> Extra {
247        Extra(self.0.clone_box())
248    }
249}
250
251impl fmt::Debug for Extra {
252    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
253        f.debug_struct("Extra").finish()
254    }
255}
256
257trait ExtraInner: Send + Sync {
258    fn clone_box(&self) -> Box<dyn ExtraInner>;
259    fn set(&self, res: &mut Extensions);
260}
261
262#[derive(Clone)]
266struct ExtraEnvelope<T>(T);
267
268impl<T> ExtraInner for ExtraEnvelope<T>
269where
270    T: Clone + Send + Sync + 'static,
271{
272    fn clone_box(&self) -> Box<dyn ExtraInner> {
273        Box::new(self.clone())
274    }
275
276    fn set(&self, res: &mut Extensions) {
277        res.insert(self.0.clone());
278    }
279}
280
281struct ExtraChain<T>(Box<dyn ExtraInner>, T);
282
283impl<T: Clone> Clone for ExtraChain<T> {
284    fn clone(&self) -> Self {
285        ExtraChain(self.0.clone_box(), self.1.clone())
286    }
287}
288
289impl<T> ExtraInner for ExtraChain<T>
290where
291    T: Clone + Send + Sync + 'static,
292{
293    fn clone_box(&self) -> Box<dyn ExtraInner> {
294        Box::new(self.clone())
295    }
296
297    fn set(&self, res: &mut Extensions) {
298        self.0.set(res);
299        res.insert(self.1.clone());
300    }
301}
302
303pub(super) mod sealed {
304    use std::error::Error as StdError;
305    use std::future::Future;
306
307    use ::http::Uri;
308    use hyper::rt::{Read, Write};
309
310    use super::Connection;
311
312    pub trait Connect: Sealed + Sized {
325        #[doc(hidden)]
326        type _Svc: ConnectSvc;
327        #[doc(hidden)]
328        fn connect(self, internal_only: Internal, dst: Uri) -> <Self::_Svc as ConnectSvc>::Future;
329    }
330
331    pub trait ConnectSvc {
332        type Connection: Read + Write + Connection + Unpin + Send + 'static;
333        type Error: Into<Box<dyn StdError + Send + Sync>>;
334        type Future: Future<Output = Result<Self::Connection, Self::Error>> + Unpin + Send + 'static;
335
336        fn connect(self, internal_only: Internal, dst: Uri) -> Self::Future;
337    }
338
339    impl<S, T> Connect for S
340    where
341        S: tower_service::Service<Uri, Response = T> + Send + 'static,
342        S::Error: Into<Box<dyn StdError + Send + Sync>>,
343        S::Future: Unpin + Send,
344        T: Read + Write + Connection + Unpin + Send + 'static,
345    {
346        type _Svc = S;
347
348        fn connect(self, _: Internal, dst: Uri) -> crate::service::Oneshot<S, Uri> {
349            crate::service::Oneshot::new(self, dst)
350        }
351    }
352
353    impl<S, T> ConnectSvc for S
354    where
355        S: tower_service::Service<Uri, Response = T> + Send + 'static,
356        S::Error: Into<Box<dyn StdError + Send + Sync>>,
357        S::Future: Unpin + Send,
358        T: Read + Write + Connection + Unpin + Send + 'static,
359    {
360        type Connection = T;
361        type Error = S::Error;
362        type Future = crate::service::Oneshot<S, Uri>;
363
364        fn connect(self, _: Internal, dst: Uri) -> Self::Future {
365            crate::service::Oneshot::new(self, dst)
366        }
367    }
368
369    impl<S, T> Sealed for S
370    where
371        S: tower_service::Service<Uri, Response = T> + Send,
372        S::Error: Into<Box<dyn StdError + Send + Sync>>,
373        S::Future: Unpin + Send,
374        T: Read + Write + Connection + Unpin + Send + 'static,
375    {
376    }
377
378    pub trait Sealed {}
379    #[allow(missing_debug_implementations)]
380    pub struct Internal;
381}
382
383#[cfg(test)]
384mod tests {
385    use super::Connected;
386
387    #[derive(Clone, Debug, PartialEq)]
388    struct Ex1(usize);
389
390    #[derive(Clone, Debug, PartialEq)]
391    struct Ex2(&'static str);
392
393    #[derive(Clone, Debug, PartialEq)]
394    struct Ex3(&'static str);
395
396    #[test]
397    fn test_connected_extra() {
398        let c1 = Connected::new().extra(Ex1(41));
399
400        let mut ex = ::http::Extensions::new();
401
402        assert_eq!(ex.get::<Ex1>(), None);
403
404        c1.extra.as_ref().expect("c1 extra").set(&mut ex);
405
406        assert_eq!(ex.get::<Ex1>(), Some(&Ex1(41)));
407    }
408
409    #[test]
410    fn test_connected_extra_chain() {
411        let c1 = Connected::new()
415            .extra(Ex1(45))
416            .extra(Ex2("zoom"))
417            .extra(Ex3("pew pew"));
418
419        let mut ex1 = ::http::Extensions::new();
420
421        assert_eq!(ex1.get::<Ex1>(), None);
422        assert_eq!(ex1.get::<Ex2>(), None);
423        assert_eq!(ex1.get::<Ex3>(), None);
424
425        c1.extra.as_ref().expect("c1 extra").set(&mut ex1);
426
427        assert_eq!(ex1.get::<Ex1>(), Some(&Ex1(45)));
428        assert_eq!(ex1.get::<Ex2>(), Some(&Ex2("zoom")));
429        assert_eq!(ex1.get::<Ex3>(), Some(&Ex3("pew pew")));
430
431        let c2 = Connected::new()
433            .extra(Ex1(33))
434            .extra(Ex2("hiccup"))
435            .extra(Ex1(99));
436
437        let mut ex2 = ::http::Extensions::new();
438
439        c2.extra.as_ref().expect("c2 extra").set(&mut ex2);
440
441        assert_eq!(ex2.get::<Ex1>(), Some(&Ex1(99)));
442        assert_eq!(ex2.get::<Ex2>(), Some(&Ex2("hiccup")));
443    }
444}