1
+ use hyper:: {
2
+ rt:: { Read , Write } ,
3
+ Uri ,
4
+ } ;
5
+ use hyper_util:: { client:: connect:: HttpConnector , rt:: TokioIo } ;
1
6
use std:: fmt;
2
7
use std:: future:: Future ;
3
8
use std:: pin:: Pin ;
4
9
use std:: task:: { Context , Poll } ;
5
-
6
- use hyper:: { client:: connect:: HttpConnector , service:: Service , Uri } ;
7
- use tokio:: io:: { AsyncRead , AsyncWrite } ;
8
10
use tokio_native_tls:: TlsConnector ;
11
+ use tower_service:: Service ;
9
12
10
13
use crate :: stream:: MaybeHttpsStream ;
11
14
@@ -20,17 +23,17 @@ pub struct HttpsConnector<T> {
20
23
}
21
24
22
25
impl HttpsConnector < HttpConnector > {
23
- /// Construct a new HttpsConnector.
26
+ /// Construct a new ` HttpsConnector` .
24
27
///
25
28
/// This uses hyper's default `HttpConnector`, and default `TlsConnector`.
26
29
/// If you wish to use something besides the defaults, use `From::from`.
27
30
///
28
31
/// # Note
29
32
///
30
33
/// By default this connector will use plain HTTP if the URL provided uses
31
- /// the HTTP scheme (eg: http://example.com/).
34
+ /// the HTTP scheme (eg: < http://example.com/> ).
32
35
///
33
- /// If you would like to force the use of HTTPS then call https_only(true)
36
+ /// If you would like to force the use of HTTPS then call ` https_only(true)`
34
37
/// on the returned connector.
35
38
///
36
39
/// # Panics
@@ -39,10 +42,12 @@ impl HttpsConnector<HttpConnector> {
39
42
///
40
43
/// To handle that error yourself, you can use the `HttpsConnector::from`
41
44
/// constructor after trying to make a `TlsConnector`.
45
+ #[ must_use]
42
46
pub fn new ( ) -> Self {
43
- native_tls:: TlsConnector :: new ( )
44
- . map ( |tls| HttpsConnector :: new_ ( tls. into ( ) ) )
45
- . unwrap_or_else ( |e| panic ! ( "HttpsConnector::new() failure: {}" , e) )
47
+ native_tls:: TlsConnector :: new ( ) . map_or_else (
48
+ |e| panic ! ( "HttpsConnector::new() failure: {}" , e) ,
49
+ |tls| HttpsConnector :: new_ ( tls. into ( ) ) ,
50
+ )
46
51
}
47
52
48
53
fn new_ ( tls : TlsConnector ) -> Self {
@@ -68,15 +73,22 @@ impl<T> HttpsConnector<T> {
68
73
69
74
/// With connector constructor
70
75
///
76
+ /// # Panics
77
+ ///
78
+ /// This will panic if the underlying TLS context could not be created.
79
+ ///
80
+ /// To handle that error yourself, you can use the `HttpsConnector::from`
81
+ /// constructor after trying to make a `TlsConnector`.
71
82
pub fn new_with_connector ( http : T ) -> Self {
72
- native_tls:: TlsConnector :: new ( )
73
- . map ( |tls| HttpsConnector :: from ( ( http, tls. into ( ) ) ) )
74
- . unwrap_or_else ( |e| {
83
+ native_tls:: TlsConnector :: new ( ) . map_or_else (
84
+ |e| {
75
85
panic ! (
76
86
"HttpsConnector::new_with_connector(<connector>) failure: {}" ,
77
87
e
78
88
)
79
- } )
89
+ } ,
90
+ |tls| HttpsConnector :: from ( ( http, tls. into ( ) ) ) ,
91
+ )
80
92
}
81
93
}
82
94
@@ -95,14 +107,14 @@ impl<T: fmt::Debug> fmt::Debug for HttpsConnector<T> {
95
107
f. debug_struct ( "HttpsConnector" )
96
108
. field ( "force_https" , & self . force_https )
97
109
. field ( "http" , & self . http )
98
- . finish ( )
110
+ . finish_non_exhaustive ( )
99
111
}
100
112
}
101
113
102
114
impl < T > Service < Uri > for HttpsConnector < T >
103
115
where
104
116
T : Service < Uri > ,
105
- T :: Response : AsyncRead + AsyncWrite + Send + Unpin ,
117
+ T :: Response : Read + Write + Send + Unpin ,
106
118
T :: Future : Send + ' static ,
107
119
T :: Error : Into < BoxError > ,
108
120
{
@@ -131,11 +143,16 @@ where
131
143
. trim_matches ( |c| c == '[' || c == ']' )
132
144
. to_owned ( ) ;
133
145
let connecting = self . http . call ( dst) ;
134
- let tls = self . tls . clone ( ) ;
146
+
147
+ let tls_connector = self . tls . clone ( ) ;
148
+
135
149
let fut = async move {
136
150
let tcp = connecting. await . map_err ( Into :: into) ?;
151
+
137
152
let maybe = if is_https {
138
- let tls = tls. connect ( & host, tcp) . await ?;
153
+ let stream = TokioIo :: new ( tcp) ;
154
+
155
+ let tls = TokioIo :: new ( tls_connector. connect ( & host, stream) . await ?) ;
139
156
MaybeHttpsStream :: Https ( tls)
140
157
} else {
141
158
MaybeHttpsStream :: Http ( tcp)
@@ -155,7 +172,7 @@ type BoxedFut<T> = Pin<Box<dyn Future<Output = Result<MaybeHttpsStream<T>, BoxEr
155
172
/// A Future representing work to connect to a URL, and a TLS handshake.
156
173
pub struct HttpsConnecting < T > ( BoxedFut < T > ) ;
157
174
158
- impl < T : AsyncRead + AsyncWrite + Unpin > Future for HttpsConnecting < T > {
175
+ impl < T : Read + Write + Unpin > Future for HttpsConnecting < T > {
159
176
type Output = Result < MaybeHttpsStream < T > , BoxError > ;
160
177
161
178
fn poll ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Self :: Output > {
0 commit comments