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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use std::fmt;
use std::future::Future;
use std::io;
use std::pin::Pin;
use std::task::{Context, Poll};
use hyper::client::connect::{Connect, Connected, Destination, HttpConnector};
pub use native_tls::Error;
use tokio_io::{AsyncRead, AsyncWrite};
use tokio_tls::TlsConnector;
use crate::stream::MaybeHttpsStream;
#[derive(Clone)]
pub struct HttpsConnector<T> {
force_https: bool,
http: T,
tls: TlsConnector,
}
impl HttpsConnector<HttpConnector> {
pub fn new() -> Result<Self, Error> {
native_tls::TlsConnector::new().map(|tls| HttpsConnector::new_(tls.into()))
}
fn new_(tls: TlsConnector) -> Self {
let mut http = HttpConnector::new();
http.enforce_http(false);
HttpsConnector::from((http, tls))
}
}
impl<T> HttpsConnector<T> {
pub fn https_only(&mut self, enable: bool) {
self.force_https = enable;
}
#[doc(hidden)]
#[deprecated(since = "0.3", note = "use `https_only` method instead")]
pub fn force_https(&mut self, enable: bool) {
self.force_https = enable;
}
}
impl<T> From<(T, TlsConnector)> for HttpsConnector<T> {
fn from(args: (T, TlsConnector)) -> HttpsConnector<T> {
HttpsConnector {
force_https: false,
http: args.0,
tls: args.1,
}
}
}
impl<T: fmt::Debug> fmt::Debug for HttpsConnector<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("HttpsConnector")
.field("force_https", &self.force_https)
.field("http", &self.http)
.finish()
}
}
impl<T> Connect for HttpsConnector<T>
where
T: Connect<Error = io::Error> + Send + Sync,
T::Future: 'static,
{
type Transport = MaybeHttpsStream<T::Transport>;
type Error = io::Error;
type Future = HttpsConnecting<T::Transport>;
fn connect(&self, dst: Destination) -> Self::Future {
let is_https = dst.scheme() == "https";
if !is_https && self.force_https {
let err = io::Error::new(
io::ErrorKind::Other,
"HTTPS scheme forced but can't be used",
);
return HttpsConnecting(Box::pin(async { Err(err) }));
}
let host = dst.host().to_owned();
let connecting = self.http.connect(dst);
let tls = self.tls.clone();
let fut = async move {
let (tcp, connected) = connecting.await?;
let maybe = if is_https {
let tls = tls
.connect(&host, tcp)
.await
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
MaybeHttpsStream::Https(tls)
} else {
MaybeHttpsStream::Http(tcp)
};
Ok((maybe, connected))
};
HttpsConnecting(Box::pin(fut))
}
}
type BoxedFut<T> =
Pin<Box<dyn Future<Output = io::Result<(MaybeHttpsStream<T>, Connected)>> + Send>>;
pub struct HttpsConnecting<T>(BoxedFut<T>);
impl<T: AsyncRead + AsyncWrite + Unpin> Future for HttpsConnecting<T> {
type Output = Result<(MaybeHttpsStream<T>, Connected), io::Error>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
Pin::new(&mut self.0).poll(cx)
}
}
impl<T> fmt::Debug for HttpsConnecting<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.pad("HttpsConnecting")
}
}