diff --git a/src/client.rs b/src/client.rs index b5b2ea0..16a35bd 100644 --- a/src/client.rs +++ b/src/client.rs @@ -90,7 +90,6 @@ 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 mut stream = connect_to_some(&request, mode)?; NoDelay::set_nodelay(&mut stream, true)?; client_with_config(request, stream, config) diff --git a/src/handshake/client.rs b/src/handshake/client.rs index c96c8ef..b8cb9cd 100644 --- a/src/handshake/client.rs +++ b/src/handshake/client.rs @@ -21,7 +21,6 @@ use super::{MidHandshake, HandshakeRole, ProcessingResult, convert_key}; pub struct BasicAuth { /// Basic Auth username pub username: String, - /// Optional Basic Auth password pub password: Option } @@ -31,8 +30,8 @@ impl BasicAuth { /// in HTTP headers. /// Example: Username: user, Password: pass -> "Basic dXNlcjpwYXNz" pub fn to_header_value(&self) -> String { - // TODO - clean this up - let creds = format!("{}:{}", self.username, self.password.as_ref().unwrap_or(&"".to_string())); + let password = self.password.as_ref().map_or("", String::as_str); + let creds = format!("{}:{}", self.username, password); format!("Basic {}", base64::encode(&creds)) } } @@ -49,7 +48,6 @@ pub enum ProxyAuth { pub struct Proxy { /// The URL of the proxy server to connect to pub url: Url, - /// Optional proxy authentication configuration pub auth: Option, } @@ -98,6 +96,24 @@ impl<'t> Request<'t> { } /// Sets an HTTP proxy to use when connecting to the WebSocket server + /// # Example + /// ``` + /// extern crate url; + /// + /// use tungstenite::handshake::client::{BasicAuth, Proxy, ProxyAuth, Request}; + /// use url::Url; + /// + /// let ws_url = Url::parse("ws://example.com/socket").unwrap(); + /// let mut ws_request = Request::from(ws_url); + /// + /// ws_request.set_proxy(Proxy { + /// url: "http://127.0.0.1:8899".parse().unwrap(), + /// auth: Some(ProxyAuth::Basic(BasicAuth { + /// username: "user".to_string(), + /// password: Some("pass".to_string()), + /// })), + /// }); + /// ``` pub fn set_proxy(&mut self, proxy: Proxy) { self.proxy = Some(proxy); }