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
141
142
143
144
145
146
147
148
149
150
151
use std::fmt;
use std::io;
use std::sync::Arc;
use futures::{Future, future, Poll};
use hyper::client::connect::{Connect, Connected, Destination, HttpConnector};
pub use native_tls::Error;
use native_tls::TlsConnector;
use tokio_tls::TlsConnectorExt;
use stream::MaybeHttpsStream;
#[derive(Clone)]
pub struct HttpsConnector<T> {
hostname_verification: bool,
force_https: bool,
http: T,
tls: Arc<TlsConnector>,
}
impl HttpsConnector<HttpConnector> {
pub fn new(threads: usize) -> Result<Self, Error> {
let mut http = HttpConnector::new(threads);
http.enforce_http(false);
let tls = TlsConnector::builder()?.build()?;
Ok(HttpsConnector::from((http, tls)))
}
}
impl<T> HttpsConnector<T> {
pub fn danger_disable_hostname_verification(&mut self, disable: bool) {
self.hostname_verification = !disable;
}
}
impl<T> HttpsConnector<T> {
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 {
hostname_verification: true,
force_https: false,
http: args.0,
tls: Arc::new(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("hostname_verification", &self.hostname_verification)
.field("force_https", &self.force_https)
.field("http", &self.http)
.finish()
}
}
impl<T> Connect for HttpsConnector<T>
where
T: Connect<Error=io::Error>,
T::Transport: 'static,
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::new(future::err(err)));
}
let host = dst.host().to_owned();
let connecting = self.http.connect(dst);
let tls = self.tls.clone();
let verification = self.hostname_verification;
let fut: BoxedFut<T::Transport> = if is_https {
let fut = connecting.and_then(move |(tcp, connected)| {
let handshake = if verification {
tls.connect_async(&host, tcp)
} else {
tls.danger_connect_async_without_providing_domain_for_certificate_verification_and_server_name_indication(tcp)
};
handshake
.map(|conn| (MaybeHttpsStream::Https(conn), connected))
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))
});
Box::new(fut)
} else {
Box::new(connecting.map(|(tcp, connected)| {
(MaybeHttpsStream::Http(tcp), connected)
}))
};
HttpsConnecting(fut)
}
}
type BoxedFut<T> = Box<Future<Item=(MaybeHttpsStream<T>, Connected), Error=io::Error> + Send>;
pub struct HttpsConnecting<T>(BoxedFut<T>);
impl<T> Future for HttpsConnecting<T> {
type Item = (MaybeHttpsStream<T>, Connected);
type Error = io::Error;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
self.0.poll()
}
}
impl<T> fmt::Debug for HttpsConnecting<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.pad("HttpsConnecting")
}
}