cyper_core/
connector.rs

1use std::{
2    future::Future,
3    io,
4    pin::Pin,
5    task::{Context, Poll},
6};
7
8use hyper::Uri;
9use send_wrapper::SendWrapper;
10use tower_service::Service;
11
12use crate::{HttpStream, TlsBackend};
13
14/// An HTTP connector service.
15///
16/// It panics when called in a different thread other than the thread creates
17/// it.
18#[derive(Debug, Clone)]
19pub struct Connector {
20    tls: TlsBackend,
21}
22
23impl Connector {
24    /// Creates the connector with specific TLS backend.
25    pub fn new(tls: TlsBackend) -> Self {
26        Self { tls }
27    }
28}
29
30impl Service<Uri> for Connector {
31    type Error = io::Error;
32    type Future = Pin<Box<dyn Future<Output = io::Result<Self::Response>> + Send>>;
33    type Response = HttpStream;
34
35    fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
36        Poll::Ready(Ok(()))
37    }
38
39    fn call(&mut self, req: Uri) -> Self::Future {
40        Box::pin(SendWrapper::new(HttpStream::connect(req, self.tls.clone())))
41    }
42}