1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
pub mod builder;
pub mod frame;
mod handshake;
mod parsed_addr;
mod stream;

use futures::FutureExt;
use tokio::io::{AsyncWriteExt, BufStream};
use rand_chacha::ChaCha20Rng;

use crate::error::WebSocketError;
use builder::WebSocketBuilder;
use frame::Frame;
use stream::Stream;

#[derive(Debug)]
enum FrameType {
    Text,
    Binary,
    Control,
}

/// Manages the WebSocket connection; used to connect, send data, and receive data.
///
/// Connect directly with [`WebSocket::connect()`]...
///
/// ```
/// # use websockets::WebSocket;
/// # #[tokio::main]
/// # async fn main() {
/// let mut ws = WebSocket::connect("wss://echo.websocket.org/")
///     .await
///     .unwrap();
/// # }
/// ```
///
/// ...or customize the handshake using a [`WebSocketBuilder`] obtained from [`WebSocket::builder()`].
///
/// ```
/// # use websockets::WebSocket;
/// # #[tokio::main]
/// # async fn main() {
/// let mut ws = WebSocket::builder()
///     .add_subprotocol("wamp")
///     .connect("wss://echo.websocket.org")
///     .await
///     .unwrap();
/// # }
/// ```
///
/// Use the `WebSocket::send*` methods to send frames...
///
/// ```
/// # use websockets::WebSocket;
/// # #[tokio::main]
/// # async fn main() {
/// # let mut ws = WebSocket::connect("wss://echo.websocket.org")
/// #     .await
/// #     .unwrap();
/// ws.send_text("foo".to_string(), false, true).await.unwrap();
/// # }
/// ```
///
/// ...and [`WebSocket::receive()`] to receive frames.
///
/// ```
/// # use websockets::WebSocket;
/// # #[tokio::main]
/// # async fn main() {
/// # let mut ws = WebSocket::connect("wss://echo.websocket.org")
/// #     .await
/// #     .unwrap();
/// # ws.send_text("foo".to_string(), false, true).await.unwrap();
/// let received_frame = ws.receive().await.unwrap();
/// let received_msg = received_frame.as_text().unwrap().0.clone();
/// assert_eq!(received_msg, "foo".to_string()); // echo.websocket.org echoes text frames
/// # }
/// ```
///
/// Finally, close the connection with [`WebSocket::close()`].
///
/// ```
/// # use websockets::WebSocket;
/// # #[tokio::main]
/// # async fn main() {
/// #     let mut ws = WebSocket::connect("wss://echo.websocket.org")
/// #         .await
/// #         .unwrap();
/// let status_code = ws.close(Some((1000, String::new())))
///     .await
///     .unwrap()
///     .as_close()
///     .unwrap()
///     .0;
/// assert_eq!(status_code, 1000);
/// # }
/// ```
#[derive(Debug)]
pub struct WebSocket {
    stream: BufStream<Stream>,
    shutdown: bool,
    rng: ChaCha20Rng,
    last_frame_type: FrameType,
    accepted_subprotocol: Option<String>,
    handshake_response_headers: Option<Vec<(String, String)>>,
}

impl WebSocket {
    /// Constructs a [`WebSocketBuilder`], which can be used to customize
    /// the WebSocket handshake.
    pub fn builder() -> WebSocketBuilder {
        WebSocketBuilder::new()
    }

    /// Connects to a URL (and performs the WebSocket handshake).
    pub async fn connect(url: &str) -> Result<Self, WebSocketError> {
        WebSocketBuilder::new().connect(url).await
    }

    /// Sends an already constructed [`Frame`] over the WebSocket connection.
    pub async fn send(&mut self, frame: Frame) -> Result<(), WebSocketError> {
        if self.shutdown {
            return Err(WebSocketError::WebSocketClosedError);
        }
        frame.send(self).await?;
        Ok(())
    }

    /// Checks whether a frame is ready to be received.
    pub fn ready_to_receive(&mut self) -> Result<bool, WebSocketError> {
        let tcp_stream = self.stream.get_mut().get_mut();
        match tcp_stream.peek(&mut vec![0]).now_or_never() {
            Some(Ok(bytes_read)) => Ok(bytes_read > 0),
            Some(Err(e)) => Err(WebSocketError::ReadError(e)),
            None => Ok(false),
        }
    }

    /// Receives a [`Frame`] over the WebSocket connection.
    ///
    /// If the received frame is a Ping frame, a Pong frame will be sent.
    /// If the received frame is a Close frame, an echoed Close frame
    /// will be sent and the WebSocket will close.
    /// 
    /// This method may block until receiving is ready. To check whether
    /// data is available to be received, use [`ready_to_receive()`](WebSocket::ready_to_receive()).
    pub async fn receive(&mut self) -> Result<Frame, WebSocketError> {
        if self.shutdown {
            return Err(WebSocketError::WebSocketClosedError);
        }
        let frame = Frame::read_from_websocket(self).await?;
        // remember last data frame type in case we get continuation frames (https://tools.ietf.org/html/rfc6455#section-5.2)
        match frame {
            Frame::Text { .. } => self.last_frame_type = FrameType::Text,
            Frame::Binary { .. } => self.last_frame_type = FrameType::Binary,
            _ => (),
        };
        // handle incoming frames
        match &frame {
            // echo ping frame (https://tools.ietf.org/html/rfc6455#section-5.5.2)
            Frame::Ping { payload } => {
                let pong = Frame::Pong {
                    payload: payload.clone(),
                };
                self.send(pong).await?;
            }
            // echo close frame and shutdown (https://tools.ietf.org/html/rfc6455#section-1.4)
            Frame::Close { payload } => {
                let close = Frame::Close {
                    payload: payload
                        .as_ref()
                        .map(|(status_code, _reason)| (status_code.clone(), String::new())),
                };
                self.send(close).await?;
                self.shutdown().await?;
            }
            _ => (),
        }
        Ok(frame)
    }

    /// Sends a Text frame over the WebSocket connection, constructed
    /// from passed arguments.
    pub async fn send_text(
        &mut self,
        payload: String,
        continuation: bool,
        fin: bool,
    ) -> Result<(), WebSocketError> {
        // https://tools.ietf.org/html/rfc6455#section-5.6
        self.send(Frame::Text {
            payload,
            continuation,
            fin,
        })
        .await
    }

    /// Sends a Binary frame over the WebSocket connection, constructed
    /// from passed arguments.
    pub async fn send_binary(
        &mut self,
        payload: Vec<u8>,
        continuation: bool,
        fin: bool,
    ) -> Result<(), WebSocketError> {
        // https://tools.ietf.org/html/rfc6455#section-5.6
        self.send(Frame::Binary {
            payload,
            continuation,
            fin,
        })
        .await
    }

    /// Sends a Close frame over the WebSocket connection, constructed
    /// from passed arguments, and closes the WebSocket connection.
    /// This method will attempt to wait for an echoed Close frame,
    /// which is returned.
    pub async fn close(&mut self, payload: Option<(u16, String)>) -> Result<Frame, WebSocketError> {
        // https://tools.ietf.org/html/rfc6455#section-5.5.1
        if self.shutdown {
            Err(WebSocketError::WebSocketClosedError)
        } else {
            self.send(Frame::Close { payload }).await?;
            let resp = self.receive().await?;
            self.shutdown().await?;
            Ok(resp)
        }
    }

    /// Sends a Ping frame over the WebSocket connection, constructed
    /// from passed arguments.
    pub async fn send_ping(&mut self, payload: Option<Vec<u8>>) -> Result<(), WebSocketError> {
        // https://tools.ietf.org/html/rfc6455#section-5.5.2
        self.send(Frame::Ping { payload }).await
    }

    /// Sends a Pong frame over the WebSocket connection, constructed
    /// from passed arguments.
    pub async fn send_pong(&mut self, payload: Option<Vec<u8>>) -> Result<(), WebSocketError> {
        // https://tools.ietf.org/html/rfc6455#section-5.5.3
        self.send(Frame::Pong { payload }).await
    }

    /// Shuts down the WebSocket connection **without sending a Close frame**.
    /// It is recommended to use the [`close()`](WebSocket::close()) method instead.
    pub async fn shutdown(&mut self) -> Result<(), WebSocketError> {
        self.shutdown = true;
        self.stream
            .shutdown()
            .await
            .map_err(|e| WebSocketError::ShutdownError(e))
    }

    /// Returns the subprotocol that was accepted by the server during the handshake,
    /// if any.
    pub fn accepted_subprotocol(&self) -> &Option<String> {
        // https://tools.ietf.org/html/rfc6455#section-1.9
        &self.accepted_subprotocol
    }

    /// Returns the headers that were returned by the server during the handshake.
    pub fn handshake_response_headers(&self) -> &Option<Vec<(String, String)>> {
        // https://tools.ietf.org/html/rfc6455#section-4.2.2
        &self.handshake_response_headers
    }
}