1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#[cfg(any(feature = "native-tls", feature = "rustls-tls"))]
pub use https::HttpsChannel;

#[cfg(any(feature = "native-tls", feature = "rustls-tls"))]
/// Https channel.
pub mod https {
    use crate::ExchangeError;
    use futures::{future::BoxFuture, FutureExt, TryFutureExt};
    use http::{Request, Response};
    use hyper::{client::HttpConnector, Body, Client};

    cfg_if::cfg_if! {
        if #[cfg(feature = "native-tls")] {
            use hyper_tls::HttpsConnector;
        } else if #[cfg(feature = "rustls-tls")] {
            use hyper_rustls::HttpsConnector;
        }
    }

    /// Https channel.
    #[derive(Clone)]
    pub struct HttpsChannel {
        pub(crate) inner: Client<HttpsConnector<HttpConnector>>,
    }

    impl tower::Service<Request<Body>> for HttpsChannel {
        type Response = Response<Body>;
        type Error = ExchangeError;
        type Future = BoxFuture<'static, Result<Self::Response, Self::Error>>;

        fn poll_ready(
            &mut self,
            cx: &mut std::task::Context<'_>,
        ) -> std::task::Poll<Result<(), Self::Error>> {
            self.inner.poll_ready(cx).map_err(ExchangeError::Http)
        }

        fn call(&mut self, req: Request<Body>) -> Self::Future {
            tower::Service::call(&mut self.inner, req)
                .map_err(ExchangeError::Http)
                .boxed()
        }
    }
}