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
use bytes::{Bytes, BytesMut};
use tracing::error;

use super::{
    error::ProtocolError,
    frame::Parser,
    proto::{CloseReason, OpCode},
};

/// A WebSocket message.
#[derive(Debug, Eq, PartialEq)]
pub enum Message {
    /// Text message.
    Text(Bytes),
    /// Binary message.
    Binary(Bytes),
    /// Continuation.
    Continuation(Item),
    /// Ping message.
    Ping(Bytes),
    /// Pong message.
    Pong(Bytes),
    /// Close message with optional reason.
    Close(Option<CloseReason>),
    /// No-op. Useful for low-level services.
    Nop,
}

/// A WebSocket continuation item.
#[derive(Debug, Eq, PartialEq)]
pub enum Item {
    FirstText(Bytes),
    FirstBinary(Bytes),
    Continue(Bytes),
    Last(Bytes),
}

/// WebSocket protocol codec.
#[derive(Debug, Copy, Clone)]
pub struct Codec {
    flags: Flags,
    capacity: usize,
    max_size: usize,
}

#[derive(Debug, Copy, Clone)]
struct Flags(u8);

impl Flags {
    const SERVER: u8 = 0b0001;
    const CONTINUATION: u8 = 0b0010;
    const CLOSED: u8 = 0b0100;

    #[inline(always)]
    fn remove(&mut self, other: u8) {
        self.0 &= !other;
    }

    #[inline(always)]
    fn insert(&mut self, other: u8) {
        self.0 |= other;
    }

    #[inline(always)]
    const fn contains(&self, other: u8) -> bool {
        (self.0 & other) == other
    }
}

impl Codec {
    /// Create new WebSocket frames decoder.
    pub const fn new() -> Codec {
        Codec {
            max_size: 65_536,
            capacity: 128,
            flags: Flags(Flags::SERVER),
        }
    }

    /// Set max frame size.
    ///
    /// By default max size is set to 64kB.
    pub fn set_max_size(mut self, size: usize) -> Self {
        self.max_size = size;
        self
    }

    pub const fn max_size(&self) -> usize {
        self.max_size
    }

    /// Set capacity for concurrent buffered outgoing message.
    ///
    /// By default capacity is set to 128.
    pub fn set_capacity(mut self, size: usize) -> Self {
        self.capacity = size;
        self
    }

    pub const fn capacity(&self) -> usize {
        self.capacity
    }

    /// Set decoder to client mode.
    ///
    /// By default decoder works in server mode.
    pub fn client_mode(mut self) -> Self {
        self.flags.remove(Flags::SERVER);
        self.flags.remove(Flags::CONTINUATION);
        self
    }

    #[doc(hidden)]
    pub fn duplicate(mut self) -> Self {
        self.flags.remove(Flags::CONTINUATION);
        self
    }
}

impl Codec {
    pub fn encode(&mut self, item: Message, dst: &mut BytesMut) -> Result<(), ProtocolError> {
        if self.flags.contains(Flags::CLOSED) {
            return Err(ProtocolError::Closed);
        }

        let mask = !self.flags.contains(Flags::SERVER);
        match item {
            Message::Text(bytes) => Parser::write_message(dst, bytes, OpCode::Text, true, mask),
            Message::Binary(bytes) => Parser::write_message(dst, bytes, OpCode::Binary, true, mask),
            Message::Ping(bytes) => Parser::write_message(dst, bytes, OpCode::Ping, true, mask),
            Message::Pong(bytes) => Parser::write_message(dst, bytes, OpCode::Pong, true, mask),
            Message::Close(reason) => {
                Parser::write_close(dst, reason, mask);
                self.flags.insert(Flags::CLOSED);
            }
            Message::Continuation(cont) => match cont {
                Item::Continue(_) | Item::Last(_) if !self.flags.contains(Flags::CONTINUATION) => {
                    return Err(ProtocolError::ContinuationNotStarted)
                }
                Item::FirstText(ref data) => {
                    self.try_start_continue()?;
                    Parser::write_message(dst, data, OpCode::Text, false, mask);
                }
                Item::FirstBinary(ref data) => {
                    self.try_start_continue()?;
                    Parser::write_message(dst, data, OpCode::Binary, false, mask);
                }
                Item::Continue(ref data) => Parser::write_message(dst, data, OpCode::Continue, false, mask),
                Item::Last(ref data) => {
                    self.flags.remove(Flags::CONTINUATION);
                    Parser::write_message(dst, data, OpCode::Continue, true, mask);
                }
            },
            Message::Nop => {}
        }

        Ok(())
    }

    pub fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Message>, ProtocolError> {
        match Parser::parse(src, self.flags.contains(Flags::SERVER), self.max_size)? {
            Some((finished, opcode, payload)) => match opcode {
                OpCode::Continue if !self.flags.contains(Flags::CONTINUATION) => {
                    Err(ProtocolError::ContinuationNotStarted)
                }
                OpCode::Continue => {
                    if finished {
                        self.flags.remove(Flags::CONTINUATION);
                    }
                    Ok(Some(Message::Continuation(Item::Continue(
                        payload.unwrap_or_else(Bytes::new),
                    ))))
                }
                OpCode::Binary if !finished => {
                    self.try_start_continue()?;
                    Ok(Some(Message::Continuation(Item::FirstBinary(
                        payload.unwrap_or_else(Bytes::new),
                    ))))
                }
                OpCode::Text if !finished => {
                    self.try_start_continue()?;
                    Ok(Some(Message::Continuation(Item::FirstText(
                        payload.unwrap_or_else(Bytes::new),
                    ))))
                }
                OpCode::Close if !finished => {
                    error!("Unfinished fragment {:?}", opcode);
                    Err(ProtocolError::ContinuationFragment(opcode))
                }
                OpCode::Binary => Ok(Some(Message::Binary(payload.unwrap_or_else(Bytes::new)))),
                OpCode::Text => Ok(Some(Message::Text(payload.unwrap_or_else(Bytes::new)))),
                OpCode::Close => Ok(Some(Message::Close(
                    payload.as_deref().and_then(Parser::parse_close_payload),
                ))),
                OpCode::Ping => Ok(Some(Message::Ping(payload.unwrap_or_else(Bytes::new)))),
                OpCode::Pong => Ok(Some(Message::Pong(payload.unwrap_or_else(Bytes::new)))),
                OpCode::Bad => Err(ProtocolError::BadOpCode),
            },
            None => Ok(None),
        }
    }

    fn try_start_continue(&mut self) -> Result<(), ProtocolError> {
        if !self.flags.contains(Flags::CONTINUATION) {
            self.flags.insert(Flags::CONTINUATION);
            Ok(())
        } else {
            Err(ProtocolError::ContinuationStarted)
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn flag() {
        let mut flags = Flags(Flags::SERVER);

        assert!(flags.contains(Flags::SERVER));
        assert!(!flags.contains(Flags::CONTINUATION));

        flags.remove(Flags::SERVER);
        assert!(!flags.contains(Flags::SERVER));
        assert!(!flags.contains(Flags::CONTINUATION));

        flags.insert(Flags::CONTINUATION);
        assert!(flags.contains(Flags::CONTINUATION));
        assert!(!flags.contains(Flags::SERVER));
    }
}