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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
//! WebSocket stream for aggregating continuation frames.

use std::{
    future::poll_fn,
    io, mem,
    pin::Pin,
    task::{ready, Context, Poll},
};

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

use crate::MessageStream;

pub(crate) enum ContinuationKind {
    Text,
    Binary,
}

/// WebSocket message with any continuations aggregated together.
#[derive(Debug, PartialEq, Eq)]
pub enum AggregatedMessage {
    /// Text message.
    Text(ByteString),

    /// Binary message.
    Binary(Bytes),

    /// Ping message.
    Ping(Bytes),

    /// Pong message.
    Pong(Bytes),

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

/// Stream of messages from a WebSocket client, with continuations aggregated.
pub struct AggregatedMessageStream {
    stream: MessageStream,
    current_size: usize,
    max_size: usize,
    continuations: Vec<Bytes>,
    continuation_kind: ContinuationKind,
    overflowed: bool,
}

impl AggregatedMessageStream {
    #[must_use]
    pub(crate) fn new(stream: MessageStream) -> Self {
        AggregatedMessageStream {
            stream,
            current_size: 0,
            max_size: 1024 * 1024,
            continuations: Vec::new(),
            continuation_kind: ContinuationKind::Binary,
            overflowed: false,
        }
    }

    /// Sets the maximum allowed size for aggregated continuations, in bytes.
    ///
    /// By default, up to 1 MiB is allowed.
    ///
    /// ```no_run
    /// # use actix_ws::AggregatedMessageStream;
    /// # async fn test(stream: AggregatedMessageStream) {
    /// // increase the allowed size from 1MB to 8MB
    /// let mut stream = stream.max_continuation_size(8 * 1024 * 1024);
    ///
    /// while let Some(Ok(msg)) = stream.recv().await {
    ///     // handle message
    /// }
    /// # }
    /// ```
    #[must_use]
    pub fn max_continuation_size(mut self, max_size: usize) -> Self {
        self.max_size = max_size;
        self
    }

    /// Waits for the next item from the aggregated message stream.
    ///
    /// This is a convenience for calling the [`Stream`](Stream::poll_next()) implementation.
    ///
    /// ```no_run
    /// # use actix_ws::AggregatedMessageStream;
    /// # async fn test(mut stream: AggregatedMessageStream) {
    /// while let Some(Ok(msg)) = stream.recv().await {
    ///     // handle message
    /// }
    /// # }
    /// ```
    #[must_use]
    pub async fn recv(&mut self) -> Option<<Self as Stream>::Item> {
        poll_fn(|cx| Pin::new(&mut *self).poll_next(cx)).await
    }
}

fn size_error() -> Poll<Option<Result<AggregatedMessage, ProtocolError>>> {
    Poll::Ready(Some(Err(ProtocolError::Io(io::Error::other(
        "Exceeded maximum continuation size",
    )))))
}

impl Stream for AggregatedMessageStream {
    type Item = Result<AggregatedMessage, ProtocolError>;

    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        let this = self.get_mut();

        loop {
            let Some(msg) = ready!(Pin::new(&mut this.stream).poll_next(cx)?) else {
                return Poll::Ready(None);
            };

            match msg {
                Message::Continuation(item) => match item {
                    Item::FirstText(bytes) => {
                        if this.overflowed {
                            continue;
                        }

                        this.continuation_kind = ContinuationKind::Text;
                        this.current_size += bytes.len();

                        if this.current_size > this.max_size {
                            this.current_size = 0;
                            this.continuations.clear();
                            this.overflowed = true;
                            return size_error();
                        }

                        // Avoid unbounded growth when receiving unlimited empty continuation frames.
                        if !bytes.is_empty() {
                            this.continuations.push(bytes);
                        }

                        continue;
                    }

                    Item::FirstBinary(bytes) => {
                        if this.overflowed {
                            continue;
                        }

                        this.continuation_kind = ContinuationKind::Binary;
                        this.current_size += bytes.len();

                        if this.current_size > this.max_size {
                            this.current_size = 0;
                            this.continuations.clear();
                            this.overflowed = true;
                            return size_error();
                        }

                        // Avoid unbounded growth when receiving unlimited empty continuation frames.
                        if !bytes.is_empty() {
                            this.continuations.push(bytes);
                        }

                        continue;
                    }

                    Item::Continue(bytes) => {
                        if this.overflowed {
                            continue;
                        }

                        this.current_size += bytes.len();

                        if this.current_size > this.max_size {
                            this.current_size = 0;
                            this.continuations.clear();
                            this.overflowed = true;
                            return size_error();
                        }

                        // Avoid unbounded growth when receiving unlimited empty continuation frames.
                        if !bytes.is_empty() {
                            this.continuations.push(bytes);
                        }

                        continue;
                    }

                    Item::Last(bytes) => {
                        if this.overflowed {
                            this.current_size = 0;
                            this.continuations.clear();
                            this.overflowed = false;
                            continue;
                        }

                        this.current_size += bytes.len();

                        if this.current_size > this.max_size {
                            // reset current_size, as this is the last message for
                            // the current continuation
                            this.current_size = 0;
                            this.continuations.clear();

                            return size_error();
                        }

                        // Avoid unbounded growth when receiving unlimited empty continuation frames.
                        if !bytes.is_empty() {
                            this.continuations.push(bytes);
                        }
                        let bytes = collect(&mut this.continuations, this.current_size);

                        this.current_size = 0;

                        match this.continuation_kind {
                            ContinuationKind::Text => {
                                return Poll::Ready(Some(match ByteString::try_from(bytes) {
                                    Ok(bytestring) => Ok(AggregatedMessage::Text(bytestring)),
                                    Err(err) => Err(ProtocolError::Io(io::Error::new(
                                        io::ErrorKind::InvalidData,
                                        err.to_string(),
                                    ))),
                                }))
                            }
                            ContinuationKind::Binary => {
                                return Poll::Ready(Some(Ok(AggregatedMessage::Binary(bytes))))
                            }
                        }
                    }
                },

                Message::Text(text) => return Poll::Ready(Some(Ok(AggregatedMessage::Text(text)))),
                Message::Binary(binary) => {
                    return Poll::Ready(Some(Ok(AggregatedMessage::Binary(binary))))
                }
                Message::Ping(ping) => return Poll::Ready(Some(Ok(AggregatedMessage::Ping(ping)))),
                Message::Pong(pong) => return Poll::Ready(Some(Ok(AggregatedMessage::Pong(pong)))),
                Message::Close(close) => {
                    return Poll::Ready(Some(Ok(AggregatedMessage::Close(close))))
                }

                Message::Nop => unreachable!("MessageStream should not produce no-ops"),
            }
        }
    }
}

fn collect(continuations: &mut Vec<Bytes>, total_len: usize) -> Bytes {
    let continuations = mem::take(continuations);
    let mut buf = BytesMut::with_capacity(total_len);

    for chunk in continuations {
        buf.extend_from_slice(&chunk);
    }

    buf.freeze()
}

#[cfg(test)]
mod tests {
    use std::{future::Future, task::Poll};

    use futures_core::Stream;

    use super::{AggregatedMessage, Bytes, Item, Message, MessageStream};
    use crate::stream::tests::payload_pair;

    #[tokio::test]
    async fn aggregates_continuations() {
        std::future::poll_fn(move |cx| {
            let (mut tx, rx) = payload_pair(8);
            let message_stream = MessageStream::new(rx).aggregate_continuations();
            let mut stream = std::pin::pin!(message_stream);

            let messages = [
                Message::Continuation(Item::FirstText(Bytes::from(b"first".to_vec()))),
                Message::Continuation(Item::Continue(Bytes::from(b"second".to_vec()))),
                Message::Continuation(Item::Last(Bytes::from(b"third".to_vec()))),
            ];

            let len = messages.len();

            for (idx, msg) in messages.into_iter().enumerate() {
                let poll = stream.as_mut().poll_next(cx);
                assert!(
                    poll.is_pending(),
                    "Stream should be pending when no messages are present {poll:?}"
                );

                let fut = tx.send(msg);
                let fut = std::pin::pin!(fut);

                assert!(fut.poll(cx).is_ready(), "Sending should not yield");

                if idx == len - 1 {
                    assert!(
                        stream.as_mut().poll_next(cx).is_ready(),
                        "Stream should be ready"
                    );
                } else {
                    assert!(
                        stream.as_mut().poll_next(cx).is_pending(),
                        "Stream shouldn't be ready until continuations complete"
                    );
                }
            }

            assert!(
                stream.as_mut().poll_next(cx).is_pending(),
                "Stream should be pending after processing messages"
            );

            Poll::Ready(())
        })
        .await
    }

    #[tokio::test]
    async fn aggregates_consecutive_continuations() {
        std::future::poll_fn(move |cx| {
            let (mut tx, rx) = payload_pair(8);
            let message_stream = MessageStream::new(rx).aggregate_continuations();
            let mut stream = std::pin::pin!(message_stream);

            let messages = vec![
                Message::Continuation(Item::FirstText(Bytes::from(b"first".to_vec()))),
                Message::Continuation(Item::Continue(Bytes::from(b"second".to_vec()))),
                Message::Continuation(Item::Last(Bytes::from(b"third".to_vec()))),
            ];

            let poll = stream.as_mut().poll_next(cx);
            assert!(
                poll.is_pending(),
                "Stream should be pending when no messages are present {poll:?}"
            );

            let fut = tx.send_many(messages);
            let fut = std::pin::pin!(fut);

            assert!(fut.poll(cx).is_ready(), "Sending should not yield");

            assert!(
                stream.as_mut().poll_next(cx).is_ready(),
                "Stream should be ready when all continuations have been sent"
            );

            assert!(
                stream.as_mut().poll_next(cx).is_pending(),
                "Stream should be pending after processing messages"
            );

            Poll::Ready(())
        })
        .await
    }

    #[tokio::test]
    async fn ignores_empty_continuation_chunks() {
        std::future::poll_fn(move |cx| {
            let (mut tx, rx) = payload_pair(8);
            let message_stream = MessageStream::new(rx).aggregate_continuations();
            let mut stream = std::pin::pin!(message_stream);

            let poll = stream.as_mut().poll_next(cx);
            assert!(
                poll.is_pending(),
                "Stream should be pending when no messages are present {poll:?}"
            );

            // start continuation with empty chunk, then send a bunch of empty continuation chunks;
            // they should not be buffered (would otherwise cause unbounded `Vec` growth).
            let messages = std::iter::once(Message::Continuation(Item::FirstText(Bytes::new())))
                .chain((0..128).map(|_| Message::Continuation(Item::Continue(Bytes::new()))))
                .collect::<Vec<_>>();

            {
                let fut = tx.send_many(messages);
                let fut = std::pin::pin!(fut);
                assert!(fut.poll(cx).is_ready(), "Sending should not yield");
            }

            assert!(
                stream.as_mut().poll_next(cx).is_pending(),
                "Stream shouldn't be ready until continuations complete"
            );
            assert_eq!(stream.as_mut().get_mut().continuations.len(), 0);

            // end continuation; this should yield an empty text message.
            {
                let fut = tx.send(Message::Continuation(Item::Last(Bytes::new())));
                let fut = std::pin::pin!(fut);
                assert!(fut.poll(cx).is_ready(), "Sending should not yield");
            }

            match stream.as_mut().poll_next(cx) {
                Poll::Ready(Some(Ok(AggregatedMessage::Text(text)))) => assert!(text.is_empty()),
                poll => panic!("expected empty text message; got {poll:?}"),
            }

            assert_eq!(stream.as_mut().get_mut().continuations.len(), 0);

            Poll::Ready(())
        })
        .await
    }

    #[tokio::test]
    async fn stream_closes() {
        std::future::poll_fn(move |cx| {
            let (tx, rx) = payload_pair(8);
            drop(tx);
            let message_stream = MessageStream::new(rx).aggregate_continuations();
            let mut stream = std::pin::pin!(message_stream);

            let poll = stream.as_mut().poll_next(cx);
            assert!(
                matches!(poll, Poll::Ready(None)),
                "Stream should be ready when all continuations have been sent"
            );

            Poll::Ready(())
        })
        .await
    }

    #[tokio::test]
    async fn continuation_overflow_errors_once_and_recovers() {
        std::future::poll_fn(move |cx| {
            let (mut tx, rx) = payload_pair(8);
            let message_stream = MessageStream::new(rx)
                .aggregate_continuations()
                .max_continuation_size(4);
            let mut stream = std::pin::pin!(message_stream);

            let poll = stream.as_mut().poll_next(cx);
            assert!(
                poll.is_pending(),
                "Stream should be pending when no messages are present {poll:?}"
            );

            let messages = vec![
                Message::Continuation(Item::FirstText(Bytes::from(b"1234".to_vec()))),
                Message::Continuation(Item::Continue(Bytes::from(b"5".to_vec()))),
                Message::Ping(Bytes::from(b"p".to_vec())),
                Message::Continuation(Item::Last(Bytes::from(b"6".to_vec()))),
                Message::Text("ok".into()),
            ];

            {
                let fut = tx.send_many(messages);
                let fut = std::pin::pin!(fut);
                assert!(fut.poll(cx).is_ready(), "Sending should not yield");
            }

            assert!(
                matches!(stream.as_mut().poll_next(cx), Poll::Ready(Some(Err(_)))),
                "expected one overflow error"
            );

            assert!(
                matches!(
                    stream.as_mut().poll_next(cx),
                    Poll::Ready(Some(Ok(AggregatedMessage::Ping(_))))
                ),
                "expected ping frame after overflow"
            );

            assert!(
                matches!(
                    stream.as_mut().poll_next(cx),
                    Poll::Ready(Some(Ok(AggregatedMessage::Text(text)))) if &text[..] == "ok"
                ),
                "expected text message after overflow continuation is terminated"
            );

            assert!(
                stream.as_mut().poll_next(cx).is_pending(),
                "Stream should be pending after processing messages"
            );

            Poll::Ready(())
        })
        .await
    }
}