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
use futures::{Future, Poll};
use hyper::client::HttpConnector;
use hyper::Uri;
use rustls::ClientConfig;
use std::{fmt, io};
use std::sync::Arc;
use stream::MaybeHttpsStream;
use tokio_core::reactor::Handle;
use tokio_rustls::ClientConfigExt;
use tokio_service::Service;
use webpki_roots;
#[derive(Clone)]
pub struct HttpsConnector {
http: HttpConnector,
}
impl HttpsConnector {
pub fn new(threads: usize, handle: &Handle) -> HttpsConnector {
let mut http = HttpConnector::new(threads, handle);
http.enforce_http(false);
HttpsConnector { http: http }
}
}
impl fmt::Debug for HttpsConnector {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("HttpsConnector").finish()
}
}
impl Service for HttpsConnector {
type Request = Uri;
type Response = MaybeHttpsStream;
type Error = io::Error;
type Future = HttpsConnecting;
fn call(&self, uri: Uri) -> Self::Future {
let is_https = uri.scheme() == Some("https");
let host = match uri.host() {
Some(host) => host.to_owned(),
None => return HttpsConnecting(
Box::new(
::futures::future::err(
io::Error::new(
io::ErrorKind::InvalidInput,
"invalid url, missing host"
)
)
)
),
};
let connecting = self.http.call(uri);
HttpsConnecting(if is_https {
Box::new(connecting.and_then(move |tcp| {
let mut config = ClientConfig::new();
config.root_store.add_trust_anchors(&webpki_roots::ROOTS);
Arc::new(config)
.connect_async(&host, tcp)
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))
}).map(|tls| MaybeHttpsStream::Https(tls))
.map_err(|e| io::Error::new(io::ErrorKind::Other, e)))
} else {
Box::new(connecting.map(|tcp| MaybeHttpsStream::Http(tcp)))
})
}
}
pub struct HttpsConnecting(Box<Future<Item = MaybeHttpsStream, Error = io::Error>>);
impl Future for HttpsConnecting {
type Item = MaybeHttpsStream;
type Error = io::Error;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
self.0.poll()
}
}
impl fmt::Debug for HttpsConnecting {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.pad("HttpsConnecting")
}
}