1- use std:: { error:: Error , task:: Context , time:: Duration } ;
1+ use std:: { error:: Error as StdError , task:: Context , time:: Duration } ;
22
3- use crate :: connect:: { Connect as TcpConnect , Connector as TcpConnector } ;
3+ use crate :: connect:: { Connect as TcpConnect , Connector2 as TcpConnector } ;
4+ use crate :: error:: { Error , ErrorMapping , with_service} ;
45use crate :: service:: { Service , ServiceCtx , ServiceFactory , apply_fn_factory, boxed} ;
56use crate :: { SharedCfg , http:: Uri , io:: IoBoxed , time:: Seconds , util:: join} ;
67
7- use super :: { Connect , Connection , error:: ConnectError , pool:: ConnectionPool } ;
8+ use super :: error:: { ClientError , ConnectError } ;
9+ use super :: { Connect , Connection , pool:: ConnectionPool } ;
810
911#[ cfg( feature = "openssl" ) ]
1012use tls_openssl:: ssl:: SslConnector as OpensslConnector ;
1113
1214#[ cfg( feature = "rustls" ) ]
1315use tls_rustls:: ClientConfig ;
1416
15- type BoxedConnector =
16- boxed:: BoxServiceFactory < SharedCfg , Connect , IoBoxed , ConnectError , Box < dyn Error > > ;
17+ type BoxedConnector = boxed:: BoxServiceFactory <
18+ SharedCfg ,
19+ Connect ,
20+ IoBoxed ,
21+ Error < ConnectError > ,
22+ Box < dyn StdError > ,
23+ > ;
1724
1825#[ derive( Debug ) ]
1926/// Manages http client network connectivity.
@@ -49,8 +56,8 @@ impl Connector {
4956 svc. call ( TcpConnect :: new ( msg. uri ) . set_addr ( msg. addr ) ) . await
5057 } )
5158 . map ( IoBoxed :: from)
52- . map_err ( ConnectError :: from)
53- . map_init_err ( |e| Box :: new ( e) as Box < dyn Error > ) ,
59+ . map_err ( |e| e . map ( ConnectError :: from) )
60+ . map_init_err ( |e| Box :: new ( e) as Box < dyn StdError > ) ,
5461 ) ,
5562 secure_svc : None ,
5663 conn_lifetime : Duration :: from_secs ( 75 ) ,
@@ -96,18 +103,18 @@ impl Connector {
96103 #[ cfg( feature = "openssl" ) ]
97104 /// Use openssl connector for secured connections.
98105 pub fn openssl ( self , connector : OpensslConnector ) -> Self {
99- use crate :: connect:: openssl:: SslConnector ;
106+ use crate :: connect:: openssl:: SslConnector2 ;
100107
101- self . secure_connector ( SslConnector :: new ( connector) )
108+ self . secure_connector ( SslConnector2 :: new ( connector) )
102109 }
103110
104111 #[ must_use]
105112 #[ cfg( feature = "rustls" ) ]
106113 /// Use rustls connector for secured connections.
107114 pub fn rustls ( self , connector : ClientConfig ) -> Self {
108- use crate :: connect:: rustls:: TlsConnector ;
115+ use crate :: connect:: rustls:: TlsConnector2 ;
109116
110- self . secure_connector ( TlsConnector :: new ( connector) )
117+ self . secure_connector ( TlsConnector2 :: new ( connector) )
111118 }
112119
113120 #[ must_use]
@@ -147,18 +154,21 @@ impl Connector {
147154 /// Use custom connector to open un-secured connections.
148155 pub fn connector < T > ( mut self , connector : T ) -> Self
149156 where
150- T : ServiceFactory < TcpConnect < Uri > , SharedCfg , Error = crate :: connect:: ConnectError >
151- + ' static ,
152- T :: InitError : Error ,
157+ T : ServiceFactory <
158+ TcpConnect < Uri > ,
159+ SharedCfg ,
160+ Error = Error < crate :: connect:: ConnectError > ,
161+ > + ' static ,
162+ T :: InitError : StdError ,
153163 IoBoxed : From < T :: Response > ,
154164 {
155165 self . svc = boxed:: factory (
156166 apply_fn_factory ( connector, async move |msg : Connect , svc| {
157167 svc. call ( TcpConnect :: new ( msg. uri ) . set_addr ( msg. addr ) ) . await
158168 } )
159169 . map ( IoBoxed :: from)
160- . map_err ( ConnectError :: from)
161- . map_init_err ( |e| Box :: new ( e) as Box < dyn Error > ) ,
170+ . map_err ( |e| e . map ( ConnectError :: from) )
171+ . map_init_err ( |e| Box :: new ( e) as Box < dyn StdError > ) ,
162172 ) ;
163173 self
164174 }
@@ -167,28 +177,31 @@ impl Connector {
167177 /// Use custom connector to open secure connections.
168178 pub fn secure_connector < T > ( mut self , connector : T ) -> Self
169179 where
170- T : ServiceFactory < TcpConnect < Uri > , SharedCfg , Error = crate :: connect:: ConnectError >
171- + ' static ,
172- T :: InitError : Error ,
180+ T : ServiceFactory <
181+ TcpConnect < Uri > ,
182+ SharedCfg ,
183+ Error = Error < crate :: connect:: ConnectError > ,
184+ > + ' static ,
185+ T :: InitError : StdError ,
173186 IoBoxed : From < T :: Response > ,
174187 {
175188 self . secure_svc = Some ( boxed:: factory (
176189 apply_fn_factory ( connector, async move |msg : Connect , svc| {
177190 svc. call ( TcpConnect :: new ( msg. uri ) . set_addr ( msg. addr ) ) . await
178191 } )
179192 . map ( IoBoxed :: from)
180- . map_err ( ConnectError :: from)
181- . map_init_err ( |e| Box :: new ( e) as Box < dyn Error > ) ,
193+ . map_err ( |e| e . map ( ConnectError :: from) )
194+ . map_init_err ( |e| Box :: new ( e) as Box < dyn StdError > ) ,
182195 ) ) ;
183196 self
184197 }
185198}
186199
187200impl ServiceFactory < Connect , SharedCfg > for Connector {
188201 type Response = Connection ;
189- type Error = ConnectError ;
202+ type Error = Error < ClientError > ;
190203 type Service = ConnectorService ;
191- type InitError = Box < dyn Error > ;
204+ type InitError = Box < dyn StdError > ;
192205
193206 async fn create ( & self , cfg : SharedCfg ) -> Result < Self :: Service , Self :: InitError > {
194207 let ssl_pool = if let Some ( ref svc) = self . secure_svc {
@@ -207,39 +220,44 @@ impl ServiceFactory<Connect, SharedCfg> for Connector {
207220 self . conn_lifetime ,
208221 self . conn_keep_alive ,
209222 self . limit ,
210- cfg,
223+ cfg. clone ( ) ,
211224 ) ;
212- Ok ( ConnectorService { tcp_pool, ssl_pool } )
225+ Ok ( ConnectorService {
226+ cfg,
227+ tcp_pool,
228+ ssl_pool,
229+ } )
213230 }
214231}
215232
216233/// Manages http client network connectivity.
217234#[ derive( Clone , Debug ) ]
218235pub struct ConnectorService {
236+ cfg : SharedCfg ,
219237 tcp_pool : ConnectionPool ,
220238 ssl_pool : Option < ConnectionPool > ,
221239}
222240
223241impl Service < Connect > for ConnectorService {
224242 type Response = Connection ;
225- type Error = ConnectError ;
243+ type Error = Error < ClientError > ;
226244
227245 #[ inline]
228246 async fn ready ( & self , ctx : ServiceCtx < ' _ , Self > ) -> Result < ( ) , Self :: Error > {
229247 if let Some ( ref ssl_pool) = self . ssl_pool {
230248 let ( r1, r2) = join ( ctx. ready ( & self . tcp_pool ) , ctx. ready ( ssl_pool) ) . await ;
231- r1?;
232- r2
249+ r1. into_error ( ) ?;
250+ r2. into_error ( )
233251 } else {
234- ctx. ready ( & self . tcp_pool ) . await
252+ ctx. ready ( & self . tcp_pool ) . await . into_error ( )
235253 }
236254 }
237255
238256 #[ inline]
239257 fn poll ( & self , cx : & mut Context < ' _ > ) -> Result < ( ) , Self :: Error > {
240- self . tcp_pool . poll ( cx) ?;
258+ self . tcp_pool . poll ( cx) . into_error ( ) ? ; //.map_err(ClientError::from) ?;
241259 if let Some ( ref ssl_pool) = self . ssl_pool {
242- ssl_pool. poll ( cx) ?;
260+ ssl_pool. poll ( cx) . into_error ( ) ? ; //map_err(ClientError::from) ?;
243261 }
244262 Ok ( ( ) )
245263 }
@@ -256,16 +274,21 @@ impl Service<Connect> for ConnectorService {
256274 req : Connect ,
257275 ctx : ServiceCtx < ' _ , Self > ,
258276 ) -> Result < Self :: Response , Self :: Error > {
259- match req. uri . scheme_str ( ) {
260- Some ( "https" | "wss" ) => {
261- if let Some ( ref conn) = self . ssl_pool {
262- ctx. call ( conn, req) . await
263- } else {
264- Err ( ConnectError :: SslIsNotSupported )
277+ with_service ( self . cfg . service ( ) , async {
278+ match req. uri . scheme_str ( ) {
279+ Some ( "https" | "wss" ) => {
280+ if let Some ( ref conn) = self . ssl_pool {
281+ ctx. call ( conn, req) . await . into_error ( )
282+ } else {
283+ Err ( Error :: from ( ClientError :: from (
284+ ConnectError :: SslIsNotSupported ,
285+ ) ) )
286+ }
265287 }
288+ _ => ctx. call ( & self . tcp_pool , req) . await . into_error ( ) ,
266289 }
267- _ => ctx . call ( & self . tcp_pool , req ) . await ,
268- }
290+ } )
291+ . await
269292 }
270293}
271294
0 commit comments