@@ -47,6 +47,7 @@ mod tls_stream;
47
47
pub use accept:: accept;
48
48
pub use acceptor:: { Error as AcceptError , TlsAcceptor } ;
49
49
pub use connect:: { connect, TlsConnector } ;
50
+ pub use host:: Host ;
50
51
pub use tls_stream:: TlsStream ;
51
52
52
53
#[ doc( inline) ]
@@ -97,9 +98,66 @@ mod accept {
97
98
}
98
99
}
99
100
101
+ mod host {
102
+ use url:: Url ;
103
+
104
+ /// The host part of a domain (without scheme, port and path).
105
+ ///
106
+ /// This is the argument to the [`connect`](crate::connect::connect) function. Strings and string slices are
107
+ /// converted into Hosts automatically, as is [Url](url::Url) with the `host-from-url` feature (enabled by default).
108
+ #[ derive( Debug ) ]
109
+ pub struct Host ( String ) ;
110
+
111
+ impl Host {
112
+ /// The host as string. Consumes self.
113
+ pub fn as_string ( self ) -> String {
114
+ self . 0
115
+ }
116
+ }
117
+
118
+ impl From < & str > for Host {
119
+ fn from ( host : & str ) -> Self {
120
+ Self ( host. into ( ) )
121
+ }
122
+ }
123
+
124
+ impl From < String > for Host {
125
+ fn from ( host : String ) -> Self {
126
+ Self ( host)
127
+ }
128
+ }
129
+
130
+ impl From < & String > for Host {
131
+ fn from ( host : & String ) -> Self {
132
+ Self ( host. into ( ) )
133
+ }
134
+ }
135
+
136
+ impl From < Url > for Host {
137
+ fn from ( url : Url ) -> Self {
138
+ Self (
139
+ url. host_str ( )
140
+ . expect ( "URL has to include a host part." )
141
+ . into ( ) ,
142
+ )
143
+ }
144
+ }
145
+
146
+ impl From < & Url > for Host {
147
+ fn from ( url : & Url ) -> Self {
148
+ Self (
149
+ url. host_str ( )
150
+ . expect ( "URL has to include a host part." )
151
+ . into ( ) ,
152
+ )
153
+ }
154
+ }
155
+ }
156
+
100
157
mod connect {
101
158
use std:: fmt:: { self , Debug } ;
102
159
160
+ use crate :: host:: Host ;
103
161
use crate :: runtime:: { AsyncRead , AsyncWrite } ;
104
162
use crate :: TlsStream ;
105
163
use crate :: { Certificate , Identity , Protocol } ;
@@ -127,11 +185,11 @@ mod connect {
127
185
/// # #[cfg(feature = "runtime-tokio")]
128
186
/// # fn main() {}
129
187
/// ```
130
- pub async fn connect < S > ( domain : & str , stream : S ) -> native_tls:: Result < TlsStream < S > >
188
+ pub async fn connect < S > ( host : impl Into < Host > , stream : S ) -> native_tls:: Result < TlsStream < S > >
131
189
where
132
190
S : AsyncRead + AsyncWrite + Unpin ,
133
191
{
134
- let stream = TlsConnector :: new ( ) . connect ( domain , stream) . await ?;
192
+ let stream = TlsConnector :: new ( ) . connect ( host , stream) . await ?;
135
193
Ok ( stream)
136
194
}
137
195
@@ -280,13 +338,19 @@ mod connect {
280
338
/// # #[cfg(feature = "runtime-tokio")]
281
339
/// # fn main() {}
282
340
/// ```
283
- pub async fn connect < S > ( & self , domain : & str , stream : S ) -> native_tls:: Result < TlsStream < S > >
341
+ pub async fn connect < S > (
342
+ & self ,
343
+ host : impl Into < Host > ,
344
+ stream : S ,
345
+ ) -> native_tls:: Result < TlsStream < S > >
284
346
where
285
347
S : AsyncRead + AsyncWrite + Unpin ,
286
348
{
349
+ let host: Host = host. into ( ) ;
350
+ let domain = host. as_string ( ) ;
287
351
let connector = self . builder . build ( ) ?;
288
352
let connector = crate :: connector:: TlsConnector :: from ( connector) ;
289
- let stream = connector. connect ( domain, stream) . await ?;
353
+ let stream = connector. connect ( & domain, stream) . await ?;
290
354
Ok ( stream)
291
355
}
292
356
}
0 commit comments