Skip to main content

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::{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        Arc,
69        atomic::{AtomicBool, Ordering},
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::{CaptureConnection, capture_connection};
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
306    use ::http::Uri;
307    use hyper::rt::{Read, Write};
308
309    use super::Connection;
310
311    /// Connect to a destination, returning an IO transport.
312    ///
313    /// A connector receives a [`Uri`](::http::Uri) and returns a `Future` of the
314    /// ready connection.
315    ///
316    /// # Trait Alias
317    ///
318    /// This is really just an *alias* for the `tower::Service` trait, with
319    /// additional bounds set for convenience *inside* hyper. You don't actually
320    /// implement this trait, but `tower::Service<Uri>` instead.
321    // The `Sized` bound is to prevent creating `dyn Connect`, since they cannot
322    // fit the `Connect` bounds because of the blanket impl for `Service`.
323    pub trait Connect: Sealed + Sized {
324        #[doc(hidden)]
325        type _Svc: ConnectSvc;
326        #[doc(hidden)]
327        fn connect(self, internal_only: Internal, dst: Uri) -> <Self::_Svc as ConnectSvc>::Future;
328    }
329
330    pub trait ConnectSvc {
331        type Connection: Read + Write + Connection + Unpin + Send + 'static;
332        type Error: Into<Box<dyn StdError + Send + Sync>>;
333        type Future: Future<Output = Result<Self::Connection, Self::Error>> + Unpin + Send + 'static;
334
335        fn connect(self, internal_only: Internal, dst: Uri) -> Self::Future;
336    }
337
338    impl<S, T> Connect for S
339    where
340        S: tower_service::Service<Uri, Response = T> + Send + 'static,
341        S::Error: Into<Box<dyn StdError + Send + Sync>>,
342        S::Future: Unpin + Send,
343        T: Read + Write + Connection + Unpin + Send + 'static,
344    {
345        type _Svc = S;
346
347        fn connect(self, _: Internal, dst: Uri) -> crate::service::Oneshot<S, Uri> {
348            crate::service::Oneshot::new(self, dst)
349        }
350    }
351
352    impl<S, T> ConnectSvc for S
353    where
354        S: tower_service::Service<Uri, Response = T> + Send + 'static,
355        S::Error: Into<Box<dyn StdError + Send + Sync>>,
356        S::Future: Unpin + Send,
357        T: Read + Write + Connection + Unpin + Send + 'static,
358    {
359        type Connection = T;
360        type Error = S::Error;
361        type Future = crate::service::Oneshot<S, Uri>;
362
363        fn connect(self, _: Internal, dst: Uri) -> Self::Future {
364            crate::service::Oneshot::new(self, dst)
365        }
366    }
367
368    impl<S, T> Sealed for S
369    where
370        S: tower_service::Service<Uri, Response = T> + Send,
371        S::Error: Into<Box<dyn StdError + Send + Sync>>,
372        S::Future: Unpin + Send,
373        T: Read + Write + Connection + Unpin + Send + 'static,
374    {
375    }
376
377    pub trait Sealed {}
378    #[allow(missing_debug_implementations)]
379    pub struct Internal;
380}
381
382#[cfg(test)]
383mod tests {
384    use super::Connected;
385
386    #[derive(Clone, Debug, PartialEq)]
387    struct Ex1(usize);
388
389    #[derive(Clone, Debug, PartialEq)]
390    struct Ex2(&'static str);
391
392    #[derive(Clone, Debug, PartialEq)]
393    struct Ex3(&'static str);
394
395    #[test]
396    fn test_connected_extra() {
397        let c1 = Connected::new().extra(Ex1(41));
398
399        let mut ex = ::http::Extensions::new();
400
401        assert_eq!(ex.get::<Ex1>(), None);
402
403        c1.extra.as_ref().expect("c1 extra").set(&mut ex);
404
405        assert_eq!(ex.get::<Ex1>(), Some(&Ex1(41)));
406    }
407
408    #[test]
409    fn test_connected_extra_chain() {
410        // If a user composes connectors and at each stage, there's "extra"
411        // info to attach, it shouldn't override the previous extras.
412
413        let c1 = Connected::new()
414            .extra(Ex1(45))
415            .extra(Ex2("zoom"))
416            .extra(Ex3("pew pew"));
417
418        let mut ex1 = ::http::Extensions::new();
419
420        assert_eq!(ex1.get::<Ex1>(), None);
421        assert_eq!(ex1.get::<Ex2>(), None);
422        assert_eq!(ex1.get::<Ex3>(), None);
423
424        c1.extra.as_ref().expect("c1 extra").set(&mut ex1);
425
426        assert_eq!(ex1.get::<Ex1>(), Some(&Ex1(45)));
427        assert_eq!(ex1.get::<Ex2>(), Some(&Ex2("zoom")));
428        assert_eq!(ex1.get::<Ex3>(), Some(&Ex3("pew pew")));
429
430        // Just like extensions, inserting the same type overrides previous type.
431        let c2 = Connected::new()
432            .extra(Ex1(33))
433            .extra(Ex2("hiccup"))
434            .extra(Ex1(99));
435
436        let mut ex2 = ::http::Extensions::new();
437
438        c2.extra.as_ref().expect("c2 extra").set(&mut ex2);
439
440        assert_eq!(ex2.get::<Ex1>(), Some(&Ex1(99)));
441        assert_eq!(ex2.get::<Ex2>(), Some(&Ex2("hiccup")));
442    }
443}