async_tungstenite/
async_tls.rs

1//! `async-tls` integration.
2use tungstenite::client::{uri_mode, IntoClientRequest};
3use tungstenite::handshake::client::{Request, Response};
4use tungstenite::protocol::WebSocketConfig;
5use tungstenite::Error;
6
7use futures_io::{AsyncRead, AsyncWrite};
8
9use super::{client_async_with_config, WebSocketStream};
10
11use async_tls::client::TlsStream;
12use async_tls::TlsConnector as AsyncTlsConnector;
13use real_async_tls as async_tls;
14
15use tungstenite::stream::Mode;
16
17use crate::domain;
18use crate::stream::Stream as StreamSwitcher;
19
20type MaybeTlsStream<S> = StreamSwitcher<S, TlsStream<S>>;
21
22pub(crate) type AutoStream<S> = MaybeTlsStream<S>;
23
24async fn wrap_stream<S>(
25    socket: S,
26    domain: String,
27    connector: Option<AsyncTlsConnector>,
28    mode: Mode,
29) -> Result<AutoStream<S>, Error>
30where
31    S: 'static + AsyncRead + AsyncWrite + Unpin,
32{
33    match mode {
34        Mode::Plain => Ok(StreamSwitcher::Plain(socket)),
35        Mode::Tls => {
36            let stream = {
37                let connector = connector.unwrap_or_else(AsyncTlsConnector::new);
38                connector.connect(&domain, socket).await?
39            };
40            Ok(StreamSwitcher::Tls(stream))
41        }
42    }
43}
44
45/// Type alias for the stream type of the `client_async()` functions.
46pub type ClientStream<S> = AutoStream<S>;
47
48/// Creates a WebSocket handshake from a request and a stream,
49/// upgrading the stream to TLS if required.
50pub async fn client_async_tls<R, S>(
51    request: R,
52    stream: S,
53) -> Result<(WebSocketStream<ClientStream<S>>, Response), Error>
54where
55    R: IntoClientRequest + Unpin,
56    S: 'static + AsyncRead + AsyncWrite + Unpin,
57    AutoStream<S>: Unpin,
58{
59    client_async_tls_with_connector_and_config(request, stream, None, None).await
60}
61
62/// Creates a WebSocket handshake from a request and a stream,
63/// upgrading the stream to TLS if required and using the given
64/// WebSocket configuration.
65pub async fn client_async_tls_with_config<R, S>(
66    request: R,
67    stream: S,
68    config: Option<WebSocketConfig>,
69) -> Result<(WebSocketStream<ClientStream<S>>, Response), Error>
70where
71    R: IntoClientRequest + Unpin,
72    S: 'static + AsyncRead + AsyncWrite + Unpin,
73    AutoStream<S>: Unpin,
74{
75    client_async_tls_with_connector_and_config(request, stream, None, config).await
76}
77
78/// Creates a WebSocket handshake from a request and a stream,
79/// upgrading the stream to TLS if required and using the given
80/// connector.
81pub async fn client_async_tls_with_connector<R, S>(
82    request: R,
83    stream: S,
84    connector: Option<AsyncTlsConnector>,
85) -> Result<(WebSocketStream<ClientStream<S>>, Response), Error>
86where
87    R: IntoClientRequest + Unpin,
88    S: 'static + AsyncRead + AsyncWrite + Unpin,
89    AutoStream<S>: Unpin,
90{
91    client_async_tls_with_connector_and_config(request, stream, connector, None).await
92}
93
94/// Creates a WebSocket handshake from a request and a stream,
95/// upgrading the stream to TLS if required and using the given
96/// connector and WebSocket configuration.
97pub async fn client_async_tls_with_connector_and_config<R, S>(
98    request: R,
99    stream: S,
100    connector: Option<AsyncTlsConnector>,
101    config: Option<WebSocketConfig>,
102) -> Result<(WebSocketStream<ClientStream<S>>, Response), Error>
103where
104    R: IntoClientRequest + Unpin,
105    S: 'static + AsyncRead + AsyncWrite + Unpin,
106    AutoStream<S>: Unpin,
107{
108    let request: Request = request.into_client_request()?;
109
110    let domain = domain(&request)?;
111
112    // Make sure we check domain and mode first. URL must be valid.
113    let mode = uri_mode(request.uri())?;
114
115    let stream = wrap_stream(stream, domain, connector, mode).await?;
116    client_async_with_config(request, stream, config).await
117}