hyper_util/client/legacy/connect/
mod.rs1use 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
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
306 use ::http::Uri;
307 use hyper::rt::{Read, Write};
308
309 use super::Connection;
310
311 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 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 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}