hyper_util/client/legacy/connect/
mod.rs

1//! Connectors used by the `Client`.
2//!
3//! This module contains:
4//!
5//! - A default [`HttpConnector`][] that does DNS resolution and establishes
6//!   connections over TCP.
7//! - Types to build custom connectors.
8//!
9//! # Connectors
10//!
11//! A "connector" is a [`Service`][] that takes a [`Uri`][] destination, and
12//! its `Response` is some type implementing [`Read`][], [`Write`][],
13//! and [`Connection`][].
14//!
15//! ## Custom Connectors
16//!
17//! A simple connector that ignores the `Uri` destination and always returns
18//! a TCP connection to the same address could be written like this:
19//!
20//! ```rust,ignore
21//! let connector = tower::service_fn(|_dst| async {
22//!     tokio::net::TcpStream::connect("127.0.0.1:1337")
23//! })
24//! ```
25//!
26//! Or, fully written out:
27//!
28//! ```
29//! use std::{future::Future, net::SocketAddr, pin::Pin, task::{self, Poll}};
30//! use http::Uri;
31//! use tokio::net::TcpStream;
32//! use tower_service::Service;
33//!
34//! #[derive(Clone)]
35//! struct LocalConnector;
36//!
37//! impl Service<Uri> for LocalConnector {
38//!     type Response = TcpStream;
39//!     type Error = std::io::Error;
40//!     // We can't "name" an `async` generated future.
41//!     type Future = Pin<Box<
42//!         dyn Future<Output = Result<Self::Response, Self::Error>> + Send
43//!     >>;
44//!
45//!     fn poll_ready(&mut self, _: &mut task::Context<'_>) -> Poll<Result<(), Self::Error>> {
46//!         // This connector is always ready, but others might not be.
47//!         Poll::Ready(Ok(()))
48//!     }
49//!
50//!     fn call(&mut self, _: Uri) -> Self::Future {
51//!         Box::pin(TcpStream::connect(SocketAddr::from(([127, 0, 0, 1], 1337))))
52//!     }
53//! }
54//! ```
55//!
56//! It's worth noting that for `TcpStream`s, the [`HttpConnector`][] is a
57//! better starting place to extend from.
58//!
59//! [`HttpConnector`]: HttpConnector
60//! [`Service`]: tower_service::Service
61//! [`Uri`]: ::http::Uri
62//! [`Read`]: hyper::rt::Read
63//! [`Write`]: hyper::rt::Write
64//! [`Connection`]: Connection
65use 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
90/// Describes a type returned by a connector.
91pub trait Connection {
92    /// Return metadata describing the connection.
93    fn connected(&self) -> Connected;
94}
95
96/// Extra information about the connected transport.
97///
98/// This can be used to inform recipients about things like if ALPN
99/// was used, or if connected to an HTTP proxy.
100#[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        // print the address of the pill—this makes debugging issues much easier
116        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    /// Create new `Connected` type with empty metadata.
150    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    /// Set whether the connected transport is to an HTTP proxy.
160    ///
161    /// This setting will affect if HTTP/1 requests written on the transport
162    /// will have the request-target in absolute-form or origin-form:
163    ///
164    /// - When `proxy(false)`:
165    ///
166    /// ```http
167    /// GET /guide HTTP/1.1
168    /// ```
169    ///
170    /// - When `proxy(true)`:
171    ///
172    /// ```http
173    /// GET http://hyper.rs/guide HTTP/1.1
174    /// ```
175    ///
176    /// Default is `false`.
177    pub fn proxy(mut self, is_proxied: bool) -> Connected {
178        self.is_proxied = is_proxied;
179        self
180    }
181
182    /// Determines if the connected transport is to an HTTP proxy.
183    pub fn is_proxied(&self) -> bool {
184        self.is_proxied
185    }
186
187    /// Set extra connection information to be set in the extensions of every `Response`.
188    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    /// Copies the extra connection information into an `Extensions` map.
198    pub fn get_extras(&self, extensions: &mut Extensions) {
199        if let Some(extra) = &self.extra {
200            extra.set(extensions);
201        }
202    }
203
204    /// Set that the connected transport negotiated HTTP/2 as its next protocol.
205    pub fn negotiated_h2(mut self) -> Connected {
206        self.alpn = Alpn::H2;
207        self
208    }
209
210    /// Determines if the connected transport negotiated HTTP/2 as its next protocol.
211    pub fn is_negotiated_h2(&self) -> bool {
212        self.alpn == Alpn::H2
213    }
214
215    /// Poison this connection
216    ///
217    /// A poisoned connection will not be reused for subsequent requests by the pool
218    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    // Don't public expose that `Connected` is `Clone`, unsure if we want to
226    // keep that contract...
227    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
237// ===== impl Extra =====
238
239impl 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// This indirection allows the `Connected` to have a type-erased "extra" value,
263// while that type still knows its inner extra type. This allows the correct
264// TypeId to be used when inserting into `res.extensions_mut()`.
265#[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    /// Connect to a destination, returning an IO transport.
313    ///
314    /// A connector receives a [`Uri`](::http::Uri) and returns a `Future` of the
315    /// ready connection.
316    ///
317    /// # Trait Alias
318    ///
319    /// This is really just an *alias* for the `tower::Service` trait, with
320    /// additional bounds set for convenience *inside* hyper. You don't actually
321    /// implement this trait, but `tower::Service<Uri>` instead.
322    // The `Sized` bound is to prevent creating `dyn Connect`, since they cannot
323    // fit the `Connect` bounds because of the blanket impl for `Service`.
324    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        // If a user composes connectors and at each stage, there's "extra"
412        // info to attach, it shouldn't override the previous extras.
413
414        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        // Just like extensions, inserting the same type overrides previous type.
432        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}