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