@@ -19,6 +19,11 @@ use std::sync::Arc;
19
19
use std:: task:: { Context , Poll } ;
20
20
use std:: { io, net} ;
21
21
use tokio:: io:: { AsyncRead , AsyncWrite } ;
22
+ use tokio:: net:: TcpStream ;
23
+ #[ cfg( feature = "runtime" ) ]
24
+ use tower:: util:: MapResponse ;
25
+ #[ cfg( feature = "runtime" ) ]
26
+ use tower:: ServiceExt ;
22
27
use tower_layer:: Layer ;
23
28
use tower_service:: Service ;
24
29
@@ -29,32 +34,41 @@ pub struct HttpsConnector<T> {
29
34
inner : Inner ,
30
35
}
31
36
37
+ /// Specialized version of [`HttpConnector`] with responses wrapped with
38
+ /// [`TokioIo::new`] in order to bring back compatibility with Tokio traits.
39
+ pub type TokioHttpConnector =
40
+ MapResponse < HttpConnector , fn ( TokioIo < TcpStream > ) -> TokioIo < TokioIo < TcpStream > > > ;
41
+
32
42
#[ cfg( feature = "runtime" ) ]
33
- impl HttpsConnector < HttpConnector > {
43
+ impl HttpsConnector < TokioHttpConnector > {
34
44
/// Creates a a new `HttpsConnector` using default settings.
35
45
///
36
46
/// The Hyper `HttpConnector` is used to perform the TCP socket connection. ALPN is configured to support both
37
47
/// HTTP/2 and HTTP/1.1.
38
48
///
39
49
/// Requires the `runtime` Cargo feature.
40
- pub fn new ( ) -> Result < HttpsConnector < HttpConnector > , ErrorStack > {
50
+ pub fn new ( ) -> Result < Self , ErrorStack > {
41
51
let mut http = HttpConnector :: new ( ) ;
42
52
http. enforce_http ( false ) ;
43
53
44
- HttpsLayer :: new ( ) . map ( |l| l. layer ( http) )
54
+ HttpsLayer :: new ( ) . map ( |l| l. layer ( http. map_response ( TokioIo :: new as _ ) ) )
45
55
}
46
56
}
47
57
48
58
impl < S , T > HttpsConnector < S >
49
59
where
50
- S : Service < Uri , Response = TokioIo < T > > + Send ,
60
+ S : Service < Uri , Response = T > + Send ,
51
61
S :: Error : Into < Box < dyn Error + Send + Sync > > ,
52
62
S :: Future : Unpin + Send + ' static ,
53
63
T : AsyncRead + AsyncWrite + Connection + Unpin + fmt:: Debug + Sync + Send + ' static ,
54
64
{
55
65
/// Creates a new `HttpsConnector`.
56
66
///
57
67
/// The session cache configuration of `ssl` will be overwritten.
68
+ ///
69
+ /// If the provided service's response type does not fit the trait
70
+ /// requirements because it is closer to the Hyper ecosystem than the Tokio
71
+ /// one, wrapping your responses with [`TokioIo`] should work.
58
72
pub fn with_connector (
59
73
http : S ,
60
74
ssl : SslConnectorBuilder ,
@@ -215,7 +229,7 @@ impl Inner {
215
229
216
230
impl < T , S > Service < Uri > for HttpsConnector < S >
217
231
where
218
- S : Service < Uri , Response = TokioIo < T > > + Send ,
232
+ S : Service < Uri , Response = T > + Send ,
219
233
S :: Error : Into < Box < dyn Error + Send + Sync > > ,
220
234
S :: Future : Unpin + Send + ' static ,
221
235
T : AsyncRead + AsyncWrite + Connection + Unpin + fmt:: Debug + Sync + Send + ' static ,
@@ -244,7 +258,7 @@ where
244
258
let connect = self . http . call ( uri) ;
245
259
246
260
let f = async {
247
- let conn = connect. await . map_err ( Into :: into) ?. into_inner ( ) ;
261
+ let conn = connect. await . map_err ( Into :: into) ?;
248
262
249
263
let ( inner, uri) = match tls_setup {
250
264
Some ( ( inner, uri) ) => ( inner, uri) ,
0 commit comments