From 914c5218c73232fd21872ee0b7a1e21a97973ecd Mon Sep 17 00:00:00 2001 From: Maxime Augier Date: Mon, 12 Aug 2024 16:22:13 +0200 Subject: [PATCH] Fail before connecting if TLS support is not present --- src/client.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/client.rs b/src/client.rs index d3c4d602..40225af0 100644 --- a/src/client.rs +++ b/src/client.rs @@ -52,6 +52,12 @@ pub fn connect_with_config( ) -> Result<(WebSocket>, Response)> { let uri = request.uri(); let mode = uri_mode(uri)?; + + #[cfg(not(any(feature = "native-tls", feature = "__rustls-tls")))] + if let Mode::Tls = mode { + return Err(Error::Url(UrlError::TlsFeatureNotEnabled)); + } + let host = request.uri().host().ok_or(Error::Url(UrlError::NoHostName))?; let host = if host.starts_with('[') { &host[1..host.len() - 1] } else { host }; let port = uri.port_u16().unwrap_or(match mode { @@ -63,10 +69,7 @@ pub fn connect_with_config( NoDelay::set_nodelay(&mut stream, true)?; #[cfg(not(any(feature = "native-tls", feature = "__rustls-tls")))] - let client = match mode { - Mode::Plain => client_with_config(request, MaybeTlsStream::Plain(stream), config), - Mode::Tls => return Err(Error::Url(UrlError::TlsFeatureNotEnabled)), - }; + let client = client_with_config(request, MaybeTlsStream::Plain(stream), config); #[cfg(any(feature = "native-tls", feature = "__rustls-tls"))] let client = crate::tls::client_tls_with_config(request, stream, config, None);