@@ -209,8 +209,7 @@ impl<'a> WsTransportClientBuilder<'a> {
209
209
match tls_connector {
210
210
None => TlsOrPlain :: Plain ( socket) ,
211
211
Some ( connector) => {
212
- let dns_name: & str =
213
- webpki:: DnsNameRef :: try_from_ascii_str ( self . target . host . as_str ( ) ) ?. into ( ) ;
212
+ let dns_name: & str = webpki:: DnsNameRef :: try_from_ascii_str ( & self . target . host ) ?. into ( ) ;
214
213
let tls_stream = connector. connect ( dns_name, socket) . await ?;
215
214
TlsOrPlain :: Tls ( tls_stream)
216
215
}
@@ -221,7 +220,7 @@ impl<'a> WsTransportClientBuilder<'a> {
221
220
} ;
222
221
223
222
let mut client =
224
- WsRawClient :: new ( BufReader :: new ( BufWriter :: new ( tcp_stream) ) , & self . target . host , & self . target . path ) ;
223
+ WsRawClient :: new ( BufReader :: new ( BufWriter :: new ( tcp_stream) ) , & self . target . host_header , & self . target . path ) ;
225
224
if let Some ( origin) = self . origin_header . as_ref ( ) {
226
225
client. set_origin ( origin) ;
227
226
}
@@ -274,6 +273,8 @@ pub struct Target {
274
273
sockaddrs : Vec < SocketAddr > ,
275
274
/// The host name (domain or IP address).
276
275
host : String ,
276
+ /// The Host request header specifies the host and port number of the server to which the request is being sent.
277
+ host_header : String ,
277
278
/// WebSocket stream mode, see [`Mode`] for further documentation.
278
279
mode : Mode ,
279
280
/// The HTTP host resource path.
@@ -292,32 +293,35 @@ impl Target {
292
293
} ;
293
294
let host =
294
295
url. host_str ( ) . map ( ToOwned :: to_owned) . ok_or_else ( || WsHandshakeError :: Url ( "No host in URL" . into ( ) ) ) ?;
296
+ let port = url. port_or_known_default ( ) . ok_or_else ( || WsHandshakeError :: Url ( "No port number in URL" . into ( ) ) ) ?;
297
+ let host_header = format ! ( "{}:{}" , host, port) ;
295
298
// NOTE: `Url::socket_addrs` is using the default port if it's missing (ws:// - 80, wss:// - 443)
296
299
let sockaddrs = url. socket_addrs ( || None ) . map_err ( WsHandshakeError :: ResolutionFailed ) ?;
297
- Ok ( Self { sockaddrs, host, mode, path : url. path ( ) . to_owned ( ) } )
300
+ Ok ( Self { sockaddrs, host, host_header , mode, path : url. path ( ) . to_owned ( ) } )
298
301
}
299
302
}
300
303
301
304
#[ cfg( test) ]
302
305
mod tests {
303
306
use super :: { Mode , Target , WsHandshakeError } ;
304
307
305
- fn assert_ws_target ( target : Target , host : & str , mode : Mode , path : & str ) {
308
+ fn assert_ws_target ( target : Target , host : & str , host_header : & str , mode : Mode , path : & str ) {
306
309
assert_eq ! ( & target. host, host) ;
310
+ assert_eq ! ( & target. host_header, host_header) ;
307
311
assert_eq ! ( target. mode, mode) ;
308
312
assert_eq ! ( & target. path, path) ;
309
313
}
310
314
311
315
#[ test]
312
316
fn ws_works ( ) {
313
317
let target = Target :: parse ( "ws://127.0.0.1:9933" ) . unwrap ( ) ;
314
- assert_ws_target ( target, "127.0.0.1" , Mode :: Plain , "/" ) ;
318
+ assert_ws_target ( target, "127.0.0.1" , "127.0.0.1:9933" , Mode :: Plain , "/" ) ;
315
319
}
316
320
317
321
#[ test]
318
322
fn wss_works ( ) {
319
323
let target = Target :: parse ( "wss://kusama-rpc.polkadot.io:443" ) . unwrap ( ) ;
320
- assert_ws_target ( target, "kusama-rpc.polkadot.io" , Mode :: Tls , "/" ) ;
324
+ assert_ws_target ( target, "kusama-rpc.polkadot.io" , "kusama-rpc.polkadot.io:443" , Mode :: Tls , "/" ) ;
321
325
}
322
326
323
327
#[ test]
@@ -337,12 +341,12 @@ mod tests {
337
341
#[ test]
338
342
fn default_port_works ( ) {
339
343
let target = Target :: parse ( "ws://127.0.0.1" ) . unwrap ( ) ;
340
- assert_ws_target ( target, "127.0.0.1" , Mode :: Plain , "/" ) ;
344
+ assert_ws_target ( target, "127.0.0.1" , "127.0.0.1:80" , Mode :: Plain , "/" ) ;
341
345
}
342
346
343
347
#[ test]
344
348
fn url_with_path_works ( ) {
345
- let target = Target :: parse ( "ws ://127.0.0.1/my-special-path" ) . unwrap ( ) ;
346
- assert_ws_target ( target, "127.0.0.1" , Mode :: Plain , "/my-special-path" ) ;
349
+ let target = Target :: parse ( "wss ://127.0.0.1/my-special-path" ) . unwrap ( ) ;
350
+ assert_ws_target ( target, "127.0.0.1" , "127.0.0.1:443" , Mode :: Tls , "/my-special-path" ) ;
347
351
}
348
352
}
0 commit comments