@@ -135,26 +135,30 @@ impl Connected {
135
135
*/
136
136
}
137
137
138
- fn connect ( addr : & SocketAddr , handle : & Handle ) -> io:: Result < ConnectFuture > {
139
- let builder = match addr {
140
- & SocketAddr :: V4 ( _) => TcpBuilder :: new_v4 ( ) ?,
141
- & SocketAddr :: V6 ( _) => TcpBuilder :: new_v6 ( ) ?,
142
- } ;
143
-
144
- if cfg ! ( windows) {
145
- // Windows requires a socket be bound before calling connect
146
- let any: SocketAddr = match addr {
147
- & SocketAddr :: V4 ( _) => {
148
- ( [ 0 , 0 , 0 , 0 ] , 0 ) . into ( )
149
- } ,
150
- & SocketAddr :: V6 ( _) => {
151
- ( [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] , 0 ) . into ( )
152
- }
138
+ fn connect ( addr : & SocketAddr , handle : & Option < Handle > ) -> io:: Result < ConnectFuture > {
139
+ if let Some ( ref handle) = * handle {
140
+ let builder = match addr {
141
+ & SocketAddr :: V4 ( _) => TcpBuilder :: new_v4 ( ) ?,
142
+ & SocketAddr :: V6 ( _) => TcpBuilder :: new_v6 ( ) ?,
153
143
} ;
154
- builder. bind ( any) ?;
155
- }
156
144
157
- Ok ( TcpStream :: connect_std ( builder. to_tcp_stream ( ) ?, addr, handle) )
145
+ if cfg ! ( windows) {
146
+ // Windows requires a socket be bound before calling connect
147
+ let any: SocketAddr = match addr {
148
+ & SocketAddr :: V4 ( _) => {
149
+ ( [ 0 , 0 , 0 , 0 ] , 0 ) . into ( )
150
+ } ,
151
+ & SocketAddr :: V6 ( _) => {
152
+ ( [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] , 0 ) . into ( )
153
+ }
154
+ } ;
155
+ builder. bind ( any) ?;
156
+ }
157
+
158
+ Ok ( TcpStream :: connect_std ( builder. to_tcp_stream ( ) ?, addr, handle) )
159
+ } else {
160
+ Ok ( TcpStream :: connect ( addr) )
161
+ }
158
162
}
159
163
160
164
/// A connector for the `http` scheme.
@@ -164,7 +168,7 @@ fn connect(addr: &SocketAddr, handle: &Handle) -> io::Result<ConnectFuture> {
164
168
pub struct HttpConnector {
165
169
executor : HttpConnectExecutor ,
166
170
enforce_http : bool ,
167
- handle : Handle ,
171
+ handle : Option < Handle > ,
168
172
keep_alive_timeout : Option < Duration > ,
169
173
}
170
174
@@ -173,7 +177,16 @@ impl HttpConnector {
173
177
///
174
178
/// Takes number of DNS worker threads.
175
179
#[ inline]
176
- pub fn new ( threads : usize , handle : & Handle ) -> HttpConnector {
180
+ pub fn new ( threads : usize ) -> HttpConnector {
181
+ HttpConnector :: new_with_handle_opt ( threads, None )
182
+ }
183
+
184
+ /// Construct a new HttpConnector with a specific Tokio handle.
185
+ pub fn new_with_handle ( threads : usize , handle : Handle ) -> HttpConnector {
186
+ HttpConnector :: new_with_handle_opt ( threads, Some ( handle) )
187
+ }
188
+
189
+ fn new_with_handle_opt ( threads : usize , handle : Option < Handle > ) -> HttpConnector {
177
190
let pool = CpuPoolBuilder :: new ( )
178
191
. name_prefix ( "hyper-dns" )
179
192
. pool_size ( threads)
@@ -184,14 +197,13 @@ impl HttpConnector {
184
197
/// Construct a new HttpConnector.
185
198
///
186
199
/// Takes an executor to run blocking tasks on.
187
- #[ inline]
188
- pub fn new_with_executor < E : ' static > ( executor : E , handle : & Handle ) -> HttpConnector
200
+ pub fn new_with_executor < E : ' static > ( executor : E , handle : Option < Handle > ) -> HttpConnector
189
201
where E : Executor < HttpConnectorBlockingTask > + Send + Sync
190
202
{
191
203
HttpConnector {
192
204
executor : HttpConnectExecutor ( Arc :: new ( executor) ) ,
193
205
enforce_http : true ,
194
- handle : handle . clone ( ) ,
206
+ handle,
195
207
keep_alive_timeout : None ,
196
208
}
197
209
}
@@ -257,7 +269,7 @@ impl Connect for HttpConnector {
257
269
}
258
270
259
271
#[ inline]
260
- fn invalid_url ( err : InvalidUrl , handle : & Handle ) -> HttpConnecting {
272
+ fn invalid_url ( err : InvalidUrl , handle : & Option < Handle > ) -> HttpConnecting {
261
273
HttpConnecting {
262
274
state : State :: Error ( Some ( io:: Error :: new ( io:: ErrorKind :: InvalidInput , err) ) ) ,
263
275
handle : handle. clone ( ) ,
@@ -292,7 +304,7 @@ impl StdError for InvalidUrl {
292
304
#[ must_use = "futures do nothing unless polled" ]
293
305
pub struct HttpConnecting {
294
306
state : State ,
295
- handle : Handle ,
307
+ handle : Option < Handle > ,
296
308
keep_alive_timeout : Option < Duration > ,
297
309
}
298
310
@@ -365,7 +377,7 @@ struct ConnectingTcp {
365
377
366
378
impl ConnectingTcp {
367
379
// not a Future, since passing a &Handle to poll
368
- fn poll ( & mut self , handle : & Handle ) -> Poll < TcpStream , io:: Error > {
380
+ fn poll ( & mut self , handle : & Option < Handle > ) -> Poll < TcpStream , io:: Error > {
369
381
let mut err = None ;
370
382
loop {
371
383
if let Some ( ref mut current) = self . current {
@@ -431,42 +443,38 @@ mod tests {
431
443
#![ allow( deprecated) ]
432
444
use std:: io;
433
445
use futures:: Future ;
434
- use tokio:: runtime:: Runtime ;
435
446
use super :: { Connect , Destination , HttpConnector } ;
436
447
437
448
#[ test]
438
449
fn test_errors_missing_authority ( ) {
439
- let runtime = Runtime :: new ( ) . unwrap ( ) ;
440
450
let uri = "/foo/bar?baz" . parse ( ) . unwrap ( ) ;
441
451
let dst = Destination {
442
452
uri,
443
453
} ;
444
- let connector = HttpConnector :: new ( 1 , runtime . handle ( ) ) ;
454
+ let connector = HttpConnector :: new ( 1 ) ;
445
455
446
456
assert_eq ! ( connector. connect( dst) . wait( ) . unwrap_err( ) . kind( ) , io:: ErrorKind :: InvalidInput ) ;
447
457
}
448
458
449
459
#[ test]
450
460
fn test_errors_enforce_http ( ) {
451
- let runtime = Runtime :: new ( ) . unwrap ( ) ;
452
461
let uri = "https://example.domain/foo/bar?baz" . parse ( ) . unwrap ( ) ;
453
462
let dst = Destination {
454
463
uri,
455
464
} ;
456
- let connector = HttpConnector :: new ( 1 , runtime . handle ( ) ) ;
465
+ let connector = HttpConnector :: new ( 1 ) ;
457
466
458
467
assert_eq ! ( connector. connect( dst) . wait( ) . unwrap_err( ) . kind( ) , io:: ErrorKind :: InvalidInput ) ;
459
468
}
460
469
461
470
462
471
#[ test]
463
472
fn test_errors_missing_scheme ( ) {
464
- let runtime = Runtime :: new ( ) . unwrap ( ) ;
465
473
let uri = "example.domain" . parse ( ) . unwrap ( ) ;
466
474
let dst = Destination {
467
475
uri,
468
476
} ;
469
- let connector = HttpConnector :: new ( 1 , runtime . handle ( ) ) ;
477
+ let connector = HttpConnector :: new ( 1 ) ;
470
478
471
479
assert_eq ! ( connector. connect( dst) . wait( ) . unwrap_err( ) . kind( ) , io:: ErrorKind :: InvalidInput ) ;
472
480
}
0 commit comments