@@ -9,34 +9,56 @@ use dashmap::DashMap;
99use deadpool:: managed:: Pool ;
1010use http_types:: StatusCode ;
1111
12- #[ cfg( feature = "native-tls" ) ]
13- use async_native_tls:: TlsStream ;
14- #[ cfg( feature = "rustls" ) ]
15- use async_tls:: client:: TlsStream ;
12+ cfg_if:: cfg_if! {
13+ if #[ cfg( feature = "rustls" ) ] {
14+ use async_tls:: client:: TlsStream ;
15+ } else if #[ cfg( feature = "native-tls" ) ] {
16+ use async_native_tls:: TlsStream ;
17+ }
18+ }
1619
1720use super :: { async_trait, Error , HttpClient , Request , Response } ;
1821
1922mod tcp;
23+ #[ cfg( any( feature = "native-tls" , feature = "rustls" ) ) ]
2024mod tls;
2125
2226use tcp:: { TcpConnWrapper , TcpConnection } ;
27+ #[ cfg( any( feature = "native-tls" , feature = "rustls" ) ) ]
2328use tls:: { TlsConnWrapper , TlsConnection } ;
2429
2530// This number is based on a few random benchmarks and see whatever gave decent perf vs resource use.
2631const DEFAULT_MAX_CONCURRENT_CONNECTIONS : usize = 50 ;
2732
2833type HttpPool = DashMap < SocketAddr , Pool < TcpStream , std:: io:: Error > > ;
34+ #[ cfg( any( feature = "native-tls" , feature = "rustls" ) ) ]
2935type HttpsPool = DashMap < SocketAddr , Pool < TlsStream < TcpStream > , Error > > ;
3036
3137/// Async-h1 based HTTP Client, with connecton pooling ("Keep-Alive").
3238pub struct H1Client {
3339 http_pools : HttpPool ,
40+ #[ cfg( any( feature = "native-tls" , feature = "rustls" ) ) ]
3441 https_pools : HttpsPool ,
3542 max_concurrent_connections : usize ,
3643}
3744
3845impl Debug for H1Client {
3946 fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
47+ let https_pools = if cfg ! ( any( feature = "native-tls" , feature = "rustls" ) ) {
48+ self . http_pools
49+ . iter ( )
50+ . map ( |pool| {
51+ let status = pool. status ( ) ;
52+ format ! (
53+ "Connections: {}, Available: {}, Max: {}" ,
54+ status. size, status. available, status. max_size
55+ )
56+ } )
57+ . collect :: < Vec < String > > ( )
58+ } else {
59+ vec ! [ ]
60+ } ;
61+
4062 f. debug_struct ( "H1Client" )
4163 . field (
4264 "http_pools" ,
@@ -52,20 +74,7 @@ impl Debug for H1Client {
5274 } )
5375 . collect :: < Vec < String > > ( ) ,
5476 )
55- . field (
56- "https_pools" ,
57- & self
58- . http_pools
59- . iter ( )
60- . map ( |pool| {
61- let status = pool. status ( ) ;
62- format ! (
63- "Connections: {}, Available: {}, Max: {}" ,
64- status. size, status. available, status. max_size
65- )
66- } )
67- . collect :: < Vec < String > > ( ) ,
68- )
77+ . field ( "https_pools" , & https_pools)
6978 . field (
7079 "max_concurrent_connections" ,
7180 & self . max_concurrent_connections ,
@@ -85,6 +94,7 @@ impl H1Client {
8594 pub fn new ( ) -> Self {
8695 Self {
8796 http_pools : DashMap :: new ( ) ,
97+ #[ cfg( any( feature = "native-tls" , feature = "rustls" ) ) ]
8898 https_pools : DashMap :: new ( ) ,
8999 max_concurrent_connections : DEFAULT_MAX_CONCURRENT_CONNECTIONS ,
90100 }
@@ -94,6 +104,7 @@ impl H1Client {
94104 pub fn with_max_connections ( max : usize ) -> Self {
95105 Self {
96106 http_pools : DashMap :: new ( ) ,
107+ #[ cfg( any( feature = "native-tls" , feature = "rustls" ) ) ]
97108 https_pools : DashMap :: new ( ) ,
98109 max_concurrent_connections : max,
99110 }
@@ -106,14 +117,17 @@ impl HttpClient for H1Client {
106117 req. insert_header ( "Connection" , "keep-alive" ) ;
107118
108119 // Insert host
120+ #[ cfg( any( feature = "native-tls" , feature = "rustls" ) ) ]
109121 let host = req
110122 . url ( )
111123 . host_str ( )
112124 . ok_or_else ( || Error :: from_str ( StatusCode :: BadRequest , "missing hostname" ) ) ?
113125 . to_string ( ) ;
114126
115127 let scheme = req. url ( ) . scheme ( ) ;
116- if scheme != "http" && scheme != "https" {
128+ if scheme != "http"
129+ && ( scheme != "https" || cfg ! ( not( any( feature = "native-tls" , feature = "rustls" ) ) ) )
130+ {
117131 return Err ( Error :: from_str (
118132 StatusCode :: BadRequest ,
119133 format ! ( "invalid url scheme '{}'" , scheme) ,
@@ -124,6 +138,7 @@ impl HttpClient for H1Client {
124138 . url ( )
125139 . socket_addrs ( || match req. url ( ) . scheme ( ) {
126140 "http" => Some ( 80 ) ,
141+ #[ cfg( any( feature = "native-tls" , feature = "rustls" ) ) ]
127142 "https" => Some ( 443 ) ,
128143 _ => None ,
129144 } ) ?
@@ -156,6 +171,7 @@ impl HttpClient for H1Client {
156171 req. set_local_addr ( stream. local_addr ( ) . ok ( ) ) ;
157172 client:: connect ( TcpConnWrapper :: new ( stream) , req) . await
158173 }
174+ #[ cfg( any( feature = "native-tls" , feature = "rustls" ) ) ]
159175 "https" => {
160176 let pool_ref = if let Some ( pool_ref) = self . https_pools . get ( & addr) {
161177 pool_ref
0 commit comments