actix-ws 0.4.0

WebSockets for Actix Web, without actors
Documentation
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
//! Typed WebSocket messages via pluggable codecs.
//!
//! This module provides a small framework for doing that. Concrete codecs can be
//! implemented by user code or enabled via crate features.
//!
//! # Feature Flags
//!
//! - `serde-json`: enables the `JsonCodec` type (requires `serde` + `serde_json`).

use std::{
    fmt,
    future::poll_fn,
    marker::PhantomData,
    pin::Pin,
    task::{Context, Poll},
};

use actix_http::ws::{CloseReason, ProtocolError};
use actix_web::web::Bytes;
use bytestring::ByteString;
use futures_core::Stream;

use crate::{AggregatedMessage, AggregatedMessageStream, Closed, MessageStream, Session};

#[cfg(feature = "serde-json")]
mod json;

#[cfg(feature = "serde-json")]
#[cfg_attr(docsrs, doc(cfg(feature = "serde-json")))]
pub use self::json::JsonCodec;

/// A codec that can translate between typed values and WebSocket messages.
pub trait MessageCodec<T> {
    /// Codec-specific error type.
    type Error;

    /// Encodes a value into a WebSocket text or binary message.
    fn encode(&self, item: &T) -> Result<EncodedMessage, Self::Error>;

    /// Decodes an incoming WebSocket message into a typed value or a control message.
    fn decode(&self, msg: AggregatedMessage) -> Result<CodecMessage<T>, Self::Error>;
}

/// WebSocket messages that can be sent by a codec.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum EncodedMessage {
    /// Text message.
    Text(ByteString),

    /// Binary message.
    Binary(Bytes),
}

/// Typed message yielded by a [`CodecMessageStream`].
#[derive(Debug)]
pub enum CodecMessage<T> {
    /// Successfully decoded application message.
    Item(T),

    /// Ping message.
    Ping(Bytes),

    /// Pong message.
    Pong(Bytes),

    /// Close message with optional reason.
    Close(Option<CloseReason>),
}

/// Errors returned by [`CodecSession::send()`].
#[derive(Debug)]
pub enum CodecSendError<E> {
    /// The session is closed.
    Closed(Closed),

    /// The codec failed to encode the outgoing value.
    Codec(E),
}

impl<E> fmt::Display for CodecSendError<E>
where
    E: fmt::Display,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            CodecSendError::Closed(_) => f.write_str("session is closed"),
            CodecSendError::Codec(err) => write!(f, "codec error: {err}"),
        }
    }
}

impl<E> std::error::Error for CodecSendError<E>
where
    E: std::error::Error + 'static,
{
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            CodecSendError::Closed(err) => Some(err),
            CodecSendError::Codec(err) => Some(err),
        }
    }
}

/// Errors returned by [`CodecMessageStream`].
#[derive(Debug)]
pub enum CodecStreamError<E> {
    /// The WebSocket stream failed to decode frames.
    Protocol(ProtocolError),

    /// The codec failed to decode an application message.
    Codec(E),
}

impl<E> fmt::Display for CodecStreamError<E>
where
    E: fmt::Display,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            CodecStreamError::Protocol(err) => write!(f, "protocol error: {err}"),
            CodecStreamError::Codec(err) => write!(f, "codec error: {err}"),
        }
    }
}

impl<E> std::error::Error for CodecStreamError<E>
where
    E: std::error::Error + 'static,
{
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            CodecStreamError::Protocol(err) => Some(err),
            CodecStreamError::Codec(err) => Some(err),
        }
    }
}

/// A [`Session`] wrapper that can send typed messages using a codec.
pub struct CodecSession<T, C> {
    session: Session,
    codec: C,
    _phantom: PhantomData<fn() -> T>,
}

impl<T, C> CodecSession<T, C>
where
    C: MessageCodec<T>,
{
    /// Constructs a new codec session wrapper.
    pub fn new(session: Session, codec: C) -> Self {
        Self {
            session,
            codec,
            _phantom: PhantomData,
        }
    }

    /// Returns a reference to the underlying session.
    pub fn session(&self) -> &Session {
        &self.session
    }

    /// Returns a mutable reference to the underlying session.
    pub fn session_mut(&mut self) -> &mut Session {
        &mut self.session
    }

    /// Returns a reference to the underlying codec.
    pub fn codec(&self) -> &C {
        &self.codec
    }

    /// Returns a mutable reference to the underlying codec.
    pub fn codec_mut(&mut self) -> &mut C {
        &mut self.codec
    }

    /// Consumes this wrapper and returns the underlying [`Session`].
    pub fn into_inner(self) -> Session {
        self.session
    }

    /// Encodes `item` and sends it as a WebSocket message.
    ///
    /// This method only sends text or binary frames. Use the underlying [`Session`] for control
    /// frames (ping/pong/close).
    pub async fn send(&mut self, item: &T) -> Result<(), CodecSendError<C::Error>> {
        let msg = self.codec.encode(item).map_err(CodecSendError::Codec)?;

        match msg {
            EncodedMessage::Text(text) => self
                .session
                .text(text)
                .await
                .map_err(CodecSendError::Closed),

            EncodedMessage::Binary(bin) => self
                .session
                .binary(bin)
                .await
                .map_err(CodecSendError::Closed),
        }
    }

    /// Sends a close frame, consuming the codec session.
    pub async fn close(self, reason: Option<CloseReason>) -> Result<(), Closed> {
        self.session.close(reason).await
    }
}

/// A [`Stream`] of typed messages decoded from an [`AggregatedMessageStream`].
pub struct CodecMessageStream<T, C> {
    stream: AggregatedMessageStream,
    codec: C,
    _phantom: PhantomData<fn() -> T>,
}

impl<T, C> CodecMessageStream<T, C>
where
    C: MessageCodec<T>,
{
    /// Constructs a new codec message stream wrapper.
    pub fn new(stream: AggregatedMessageStream, codec: C) -> Self {
        Self {
            stream,
            codec,
            _phantom: PhantomData,
        }
    }

    /// Returns a reference to the underlying codec.
    pub fn codec(&self) -> &C {
        &self.codec
    }

    /// Returns a mutable reference to the underlying codec.
    pub fn codec_mut(&mut self) -> &mut C {
        &mut self.codec
    }

    /// Consumes this wrapper and returns the underlying stream.
    pub fn into_inner(self) -> AggregatedMessageStream {
        self.stream
    }

    /// Waits for the next item from the codec message stream.
    ///
    /// This is a convenience for calling the [`Stream`](Stream::poll_next()) implementation.
    #[must_use]
    pub async fn recv(&mut self) -> Option<<Self as Stream>::Item> {
        // `CodecMessageStream` is not necessarily `Unpin` (depends on codec type) but it is safe
        // to pin it for the duration of this future since it is borrowed for the await.
        poll_fn(|cx| unsafe { Pin::new_unchecked(&mut *self) }.poll_next(cx)).await
    }
}

impl<T, C> Stream for CodecMessageStream<T, C>
where
    C: MessageCodec<T>,
{
    type Item = Result<CodecMessage<T>, CodecStreamError<C::Error>>;

    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        // SAFETY: We will not move out of any fields. `AggregatedMessageStream` is polled by
        // pinning its field, and the codec is only accessed by reference.
        let this = unsafe { self.get_unchecked_mut() };

        let msg = match Pin::new(&mut this.stream).poll_next(cx) {
            Poll::Ready(Some(Ok(msg))) => msg,
            Poll::Ready(Some(Err(err))) => {
                return Poll::Ready(Some(Err(CodecStreamError::Protocol(err))));
            }
            Poll::Ready(None) => return Poll::Ready(None),
            Poll::Pending => return Poll::Pending,
        };

        match this.codec.decode(msg) {
            Ok(item) => Poll::Ready(Some(Ok(item))),
            Err(err) => Poll::Ready(Some(Err(CodecStreamError::Codec(err)))),
        }
    }
}

impl MessageStream {
    /// Wraps this message stream with `codec`, aggregating continuation frames before decoding.
    #[must_use]
    pub fn with_codec<T, C>(self, codec: C) -> CodecMessageStream<T, C>
    where
        C: MessageCodec<T>,
    {
        self.aggregate_continuations().with_codec(codec)
    }
}

impl AggregatedMessageStream {
    /// Wraps this aggregated message stream with `codec`.
    #[must_use]
    pub fn with_codec<T, C>(self, codec: C) -> CodecMessageStream<T, C>
    where
        C: MessageCodec<T>,
    {
        CodecMessageStream::new(self, codec)
    }
}

impl Session {
    /// Wraps this session with `codec` so it can send typed messages.
    #[must_use]
    pub fn with_codec<T, C>(self, codec: C) -> CodecSession<T, C>
    where
        C: MessageCodec<T>,
    {
        CodecSession::new(self, codec)
    }
}

#[cfg(all(test, feature = "serde-json"))]
mod tests {
    use actix_http::ws::Message;
    use actix_web::web::Bytes;
    use serde::{Deserialize, Serialize};

    use super::{CodecMessage, EncodedMessage};
    use crate::{codec::CodecStreamError, stream::tests::payload_pair, Session};

    #[derive(Debug, Serialize, Deserialize, PartialEq)]
    struct TestMsg {
        a: u32,
    }

    #[tokio::test]
    async fn json_session_encodes_text_frames_by_default() {
        let (tx, mut rx) = tokio::sync::mpsc::channel(1);
        let session = Session::new(tx);

        let mut session = session.with_codec::<TestMsg, _>(crate::codec::JsonCodec::default());
        session.send(&TestMsg { a: 123 }).await.unwrap();

        match rx.recv().await.unwrap() {
            Message::Text(text) => {
                let s: &str = text.as_ref();
                assert_eq!(s, r#"{"a":123}"#);
            }
            other => panic!("expected text frame, got: {other:?}"),
        }
    }

    #[tokio::test]
    async fn json_session_can_encode_binary_frames() {
        let (tx, mut rx) = tokio::sync::mpsc::channel(1);
        let session = Session::new(tx);

        let mut session =
            session.with_codec::<TestMsg, _>(crate::codec::JsonCodec::default().binary());
        session.send(&TestMsg { a: 123 }).await.unwrap();

        match rx.recv().await.unwrap() {
            Message::Binary(bytes) => assert_eq!(bytes, Bytes::from_static(br#"{"a":123}"#)),
            other => panic!("expected binary frame, got: {other:?}"),
        }
    }

    #[tokio::test]
    async fn json_stream_decodes_text_and_binary_frames() {
        let (mut tx, rx) = payload_pair(8);
        let mut stream = crate::MessageStream::new(rx)
            .with_codec::<TestMsg, _>(crate::codec::JsonCodec::default());

        tx.send(Message::Text(r#"{"a":1}"#.into())).await;
        match stream.recv().await.unwrap().unwrap() {
            CodecMessage::Item(TestMsg { a }) => assert_eq!(a, 1),
            other => panic!("expected decoded item, got: {other:?}"),
        }

        tx.send(Message::Binary(Bytes::from_static(br#"{"a":2}"#)))
            .await;
        match stream.recv().await.unwrap().unwrap() {
            CodecMessage::Item(TestMsg { a }) => assert_eq!(a, 2),
            other => panic!("expected decoded item, got: {other:?}"),
        }
    }

    #[tokio::test]
    async fn json_stream_passes_through_control_frames() {
        let (mut tx, rx) = payload_pair(8);
        let mut stream = crate::MessageStream::new(rx)
            .with_codec::<TestMsg, _>(crate::codec::JsonCodec::default());

        tx.send(Message::Ping(Bytes::from_static(b"hi"))).await;
        match stream.recv().await.unwrap().unwrap() {
            CodecMessage::Ping(bytes) => assert_eq!(bytes, Bytes::from_static(b"hi")),
            other => panic!("expected ping, got: {other:?}"),
        }
    }

    #[tokio::test]
    async fn json_stream_yields_codec_error_on_invalid_payload_and_continues() {
        let (mut tx, rx) = payload_pair(8);
        let mut stream = crate::MessageStream::new(rx)
            .with_codec::<TestMsg, _>(crate::codec::JsonCodec::default());

        tx.send(Message::Text("not json".into())).await;
        match stream.recv().await.unwrap() {
            Err(CodecStreamError::Codec(_)) => {}
            other => panic!("expected codec error, got: {other:?}"),
        }

        tx.send(Message::Text(r#"{"a":9}"#.into())).await;
        match stream.recv().await.unwrap().unwrap() {
            CodecMessage::Item(TestMsg { a }) => assert_eq!(a, 9),
            other => panic!("expected decoded item, got: {other:?}"),
        }
    }

    #[test]
    fn encoded_message_is_lightweight() {
        let _ = EncodedMessage::Text("hello".into());
        let _ = EncodedMessage::Binary(Bytes::from_static(b"hello"));
    }
}