|
|
|
@ -1,23 +1,22 @@ |
|
|
|
|
//! Connection helper.
|
|
|
|
|
use tokio_io::{AsyncRead, AsyncWrite}; |
|
|
|
|
use tokio_net::tcp::TcpStream; |
|
|
|
|
|
|
|
|
|
use tungstenite::client::url_mode; |
|
|
|
|
use tungstenite::handshake::client::Response; |
|
|
|
|
use tungstenite::Error; |
|
|
|
|
|
|
|
|
|
use futures::io::{AsyncRead, AsyncWrite}; |
|
|
|
|
|
|
|
|
|
use super::{client_async, Request, WebSocketStream}; |
|
|
|
|
|
|
|
|
|
#[cfg(feature = "tls")] |
|
|
|
|
pub(crate) mod encryption { |
|
|
|
|
use native_tls::TlsConnector; |
|
|
|
|
use tokio_tls::{TlsConnector as TokioTlsConnector, TlsStream}; |
|
|
|
|
|
|
|
|
|
use tokio_io::{AsyncRead, AsyncWrite}; |
|
|
|
|
use async_tls::TlsConnector as AsyncTlsConnector; |
|
|
|
|
use async_tls::client::TlsStream; |
|
|
|
|
|
|
|
|
|
use tungstenite::stream::Mode; |
|
|
|
|
use tungstenite::Error; |
|
|
|
|
|
|
|
|
|
use futures::io::{AsyncRead, AsyncWrite}; |
|
|
|
|
|
|
|
|
|
use crate::stream::Stream as StreamSwitcher; |
|
|
|
|
|
|
|
|
|
/// A stream that might be protected with TLS.
|
|
|
|
@ -36,12 +35,10 @@ pub(crate) mod encryption { |
|
|
|
|
match mode { |
|
|
|
|
Mode::Plain => Ok(StreamSwitcher::Plain(socket)), |
|
|
|
|
Mode::Tls => { |
|
|
|
|
let try_connector = TlsConnector::new(); |
|
|
|
|
let connector = try_connector.map_err(Error::Tls)?; |
|
|
|
|
let stream = TokioTlsConnector::from(connector); |
|
|
|
|
let connected = stream.connect(&domain, socket).await; |
|
|
|
|
let stream = AsyncTlsConnector::new(); |
|
|
|
|
let connected = stream.connect(&domain, socket)?.await; |
|
|
|
|
match connected { |
|
|
|
|
Err(e) => Err(Error::Tls(e)), |
|
|
|
|
Err(e) => Err(Error::Io(e)), |
|
|
|
|
Ok(s) => Ok(StreamSwitcher::Tls(s)), |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -55,7 +52,7 @@ pub use self::encryption::MaybeTlsStream; |
|
|
|
|
#[cfg(not(feature = "tls"))] |
|
|
|
|
pub(crate) mod encryption { |
|
|
|
|
use futures::{future, Future}; |
|
|
|
|
use tokio_io::{AsyncRead, AsyncWrite}; |
|
|
|
|
use futures::io::{AsyncRead, AsyncWrite}; |
|
|
|
|
|
|
|
|
|
use tungstenite::stream::Mode; |
|
|
|
|
use tungstenite::Error; |
|
|
|
@ -110,6 +107,11 @@ where |
|
|
|
|
client_async(request, stream).await |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[cfg(feature = "async_std_runtime")] |
|
|
|
|
pub(crate) mod async_std_runtime { |
|
|
|
|
use super::*; |
|
|
|
|
use async_std::net::TcpStream; |
|
|
|
|
|
|
|
|
|
/// Connect to a given URL.
|
|
|
|
|
pub async fn connect_async<R>( |
|
|
|
|
request: R, |
|
|
|
@ -129,3 +131,7 @@ where |
|
|
|
|
let socket = try_socket.map_err(Error::Io)?; |
|
|
|
|
client_async_tls(request, socket).await |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[cfg(feature = "async_std_runtime")] |
|
|
|
|
pub use async_std_runtime::connect_async; |
|
|
|
|