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
use std::convert::TryInto;
use bytes::{ BytesMut, Buf, BufMut };
use tokio_util::codec::{ Decoder, Encoder };
use crate::{
Message,
Opcode,
WebsocketError,
WebsocketResult,
};
#[derive(Debug, Default)]
pub struct WebsocketCodec {
fin: bool,
opcode: Opcode,
payload: BytesMut,
}
impl WebsocketCodec {
#[inline(always)]
fn next_frame(&mut self, buf: &mut BytesMut) -> Option<()> {
if buf.len() < 6 {
return None
}
self.fin = (0x80 & buf[0]) >> 7 != 0;
self._set_opcode(0x0F & buf[0]);
if self.opcode == Opcode::Text {
let (payload_len, payload_idx, mask_key) = self._payload_meta(buf);
if buf[payload_idx..].len() < payload_len {
return None
}
self._unmask(buf, payload_len, payload_idx, mask_key);
self.payload.extend_from_slice(&buf[payload_idx..payload_idx + payload_len]);
buf.advance(payload_idx + payload_len);
return Some(())
}
if
self.opcode == Opcode::Ping
|| self.opcode == Opcode::Pong
|| self.opcode == Opcode::Close
{
buf.advance(buf.len());
return Some(())
}
unimplemented!()
}
#[inline(always)]
fn _unmask(&mut self, buf: &mut BytesMut, len: usize, idx: usize, key: [u8; 4]) {
if (0x80 & buf[1]) >> 7 != 0 {
for i in idx..(idx + len) {
buf[i] = buf[i] ^ key[(i - idx) % 4];
}
}
}
#[inline(always)]
fn _payload_meta(&self, buf: &BytesMut) -> (usize, usize, [u8; 4]) {
let len = (0x7F & buf[1]) as usize;
if len == 127 {
return (
usize::from_be_bytes(buf[2..10].try_into().unwrap()),
14,
buf[10..14].try_into().unwrap()
)
}
if len == 126 {
return (
u16::from_be_bytes(buf[2..4].try_into().unwrap()) as usize,
8,
buf[4..8].try_into().unwrap()
)
}
(len, 6, buf[2..6].try_into().unwrap())
}
#[inline(always)]
fn _set_opcode(&mut self, opcode: u8) {
if opcode == 1 { self.opcode = Opcode::Text; }
else if opcode == 0x9 { self.opcode = Opcode::Ping; }
else if opcode == 0xA { self.opcode = Opcode::Pong; }
else if opcode == 0x8 { self.opcode = Opcode::Close; }
else { self.opcode = Opcode::Unknown; }
}
}
impl Decoder for WebsocketCodec {
type Item = Message;
type Error = WebsocketError;
fn decode(&mut self, buf: &mut BytesMut) -> WebsocketResult<Option<Self::Item>> {
while let Some(_) = self.next_frame(buf) {
if self.fin == true && (
self.opcode == Opcode::Text
|| self.opcode == Opcode::Ping
|| self.opcode == Opcode::Pong
|| self.opcode == Opcode::Close
) {
return Ok(Some(Message {
opcode: self.opcode,
payload: self.payload.to_bytes(),
}))
}
}
Ok(None)
}
}
impl Encoder<Message> for WebsocketCodec {
type Error = WebsocketError;
fn encode(&mut self, msg: Message, dst: &mut BytesMut) -> WebsocketResult<()> {
if msg.opcode == Opcode::Pong {
dst.extend_from_slice(&[138, 0]);
}
if msg.opcode == Opcode::Text {
dst.put_u8(0x81);
if msg.payload.len() <= 125 {
dst.put_u8(msg.payload.len() as u8);
} else if msg.payload.len() == 126 {
dst.put_u8(126);
dst.put_u16(msg.payload.len() as u16);
} else {
dst.put_u8(127);
dst.put_u64(msg.payload.len() as u64);
}
dst.put_slice(&msg.payload[..]);
}
Ok(())
}
}
#[cfg(test)]
mod tests {
extern crate test;
use super::*;
use bytes::BytesMut;
use std::str;
use test::Bencher;
macro_rules! frame {
($buf:expr, $payload:expr, $opcode:expr) => {
let mut buf: BytesMut = $buf.as_bytes()
.chunks(2)
.map(|s| u8::from_str_radix(unsafe { str::from_utf8_unchecked(s) }, 16).unwrap())
.collect();
let mut wsc = WebsocketCodec::default();
if let Some(_) = wsc.next_frame(&mut buf) {
let payload = unsafe { str::from_utf8_unchecked(&wsc.payload) };
assert!(payload == $payload);
assert!(wsc.opcode == $opcode);
} else {
assert!(false, "failed to extract frame from '{:x}'")
}
}
}
#[test]
fn text_frame_with_no_payload() {
frame!("8180e0350bbc", "", Opcode::Text);
}
#[test]
fn text_frame_with_payload_len_eq_1() {
frame!("8181f3dba99092", "a", Opcode::Text);
}
#[test]
fn text_frame_with_hello_world_payload() {
frame!("818bb013fc40d876902cdf338b2fc27f98", "hello world", Opcode::Text);
}
#[test]
fn text_frame_payload_125() {
frame!(
"81fd16f9d90b5795b52b6291b87f369eb562628dbc7965d9b0783697b67f369eb66772d99f6a7f8bf96265d9bf646395f52b7797bd2b7096ac673690aa2b7098b0792cd99164609cab2b6291ab64639eb12b6291bc2b7096be2b7797bd2b7090b57f7e80f96a7f8bf72b36adb16e659cf97d7f96b56e788df96f7395b06c7e8daa2b7e",
"All that glitters is not gold Fair is foul, and foul is fair: Hover through the fog and filthy air. These violent delights h",
Opcode::Text
);
}
#[bench]
fn bench_text_frame_payload_125(b: &mut Bencher) {
b.iter(|| text_frame_payload_125())
}
#[test]
fn text_frame_long_payload() {
frame!(
"81fe020d6742ba33262ed613132adb474725d65a1336df411462d340472cd5474725d55f0348fc520e309a5a1462dc5c122e9613062cde13012dcf5f472bc9130123d3415d62f25c1127c813132ac85c1225d213132adf13012ddd13062cde13012bd6470f3b9a520e309439332adf400262cc5a082edf5d1362de560b2bdd5b13319a5b0634df13112bd55f022cce13022cde40496c94392f27d65f472bc913022fca471e62db5d0362db5f0b62ce5b0262de56112bd6404723c856472adf41026cb0711e62ce5b0262ca410e21d15a09259a5c0162d74a4736d2460a20c91f4711d55e0236d25a09259a440e21d1560362ce5b0e319a44063b9a50082fdf404962f543022c96130b2dd958146e9a640f2ddf4502309a58092dd9581463b0670f279a5f0626c313032dce5b4732c85c1327c9474736d55c472fcf500f6e9a5e0236d25a0929c91d6d00c856112bce4a472bc913132adf13142dcf5f472ddc13102bce1d6d0bdc130a37c95a0462d8564736d2564724d55c0362d555472ed545026e9a430b23c313082c9439292dcd130e319a470f279a440e2cce561562d555472dcf414726d340042dd447022cce1d6d05d55c0362d45a002ace1f4725d55c0362d45a002ace124732db41132bd454472bc9131437d95b4731cd5602369a400830c85c106e9a670f23ce132e62c95b062ed6131423c313002dd557472cd3540f369a470e2ed6130e369a510262d75c1530d54449",
"All that glitters is not gold
Fair is foul, and foul is fair: Hover through the fog and filthy air.
These violent delights have violent ends...
Hell is empty and all the devils are here.
By the pricking of my thumbs, Something wicked this way comes. Open, locks, Whoever knocks!
The lady doth protest too much, methinks.
Brevity is the soul of wit.
If music be the food of love, play on.
Now is the winter of our discontent.
Good night, good night! parting is such sweet sorrow, That I shall say good night till it be morrow.",
Opcode::Text
);
}
#[bench]
fn bench_text_frame_long_payload(b: &mut Bencher) {
b.iter(|| text_frame_long_payload())
}
}