From 4b1d89daf115d098e45fb69af7db4f3c5679157c Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Wed, 24 Jul 2019 16:46:58 +0200 Subject: [PATCH] Update the url crate to 2.0 --- Cargo.toml | 4 ++-- src/client.rs | 25 +++++++++++++++++++++---- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index cb9b6147..bb43bb34 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,7 @@ readme = "README.md" homepage = "https://github.com/snapview/tungstenite-rs" documentation = "https://docs.rs/tungstenite/0.8.1" repository = "https://github.com/snapview/tungstenite-rs" -version = "0.8.1" +version = "0.9.0" [features] default = ["tls"] @@ -25,7 +25,7 @@ input_buffer = "0.2.0" log = "0.4.2" rand = "0.6.4" sha-1 = "0.8" -url = "1.7.0" +url = "2.0" utf-8 = "0.7.2" [dependencies.native-tls] diff --git a/src/client.rs b/src/client.rs index 4fc2b46c..7e711307 100644 --- a/src/client.rs +++ b/src/client.rs @@ -87,7 +87,26 @@ pub fn connect_with_config<'t, Req: Into>>( ) -> Result<(WebSocket, Response)> { let request: Request = request.into(); let mode = url_mode(&request.url)?; - let addrs = request.url.to_socket_addrs()?; + let host = request.url.host() + .ok_or_else(|| Error::Url("No host name in the URL".into()))?; + let port = request.url.port_or_known_default() + .ok_or_else(|| Error::Url("No port number in the URL".into()))?; + let addrs; + let addr; + let addrs = match host { + url::Host::Domain(domain) => { + addrs = (domain, port).to_socket_addrs()?; + addrs.as_slice() + } + url::Host::Ipv4(ip) => { + addr = (ip, port).into(); + std::slice::from_ref(&addr) + } + url::Host::Ipv6(ip) => { + addr = (ip, port).into(); + std::slice::from_ref(&addr) + } + }; let mut stream = connect_to_some(addrs, &request.url, mode)?; NoDelay::set_nodelay(&mut stream, true)?; client_with_config(request, stream, config) @@ -115,9 +134,7 @@ pub fn connect<'t, Req: Into>>(request: Req) connect_with_config(request, None) } -fn connect_to_some(addrs: A, url: &Url, mode: Mode) -> Result - where A: Iterator -{ +fn connect_to_some(addrs: &[SocketAddr], url: &Url, mode: Mode) -> Result { let domain = url.host_str().ok_or_else(|| Error::Url("No host name in the URL".into()))?; for addr in addrs { debug!("Trying to contact {} at {}...", url, addr);