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
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
use result::{WebSocketError, WebSocketResult};
use std::io::{Read, Write};
bitflags! {
pub struct DataFrameFlags: u8 {
const FIN = 0x80;
const RSV1 = 0x40;
const RSV2 = 0x20;
const RSV3 = 0x10;
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct DataFrameHeader {
pub flags: DataFrameFlags,
pub opcode: u8,
pub mask: Option<[u8; 4]>,
pub len: u64,
}
pub fn write_header(writer: &mut Write, header: DataFrameHeader) -> WebSocketResult<()> {
if header.opcode > 0xF {
return Err(WebSocketError::DataFrameError("Invalid data frame opcode"));
}
if header.opcode >= 8 && header.len >= 126 {
return Err(WebSocketError::DataFrameError(
"Control frame length too long",
));
}
writer.write_u8((header.flags.bits) | header.opcode)?;
writer.write_u8(
if header.mask.is_some() { 0x80 } else { 0x00 } |
if header.len <= 125 { header.len as u8 }
else if header.len <= 65535 { 126 }
else { 127 },
)?;
if header.len >= 126 && header.len <= 65535 {
writer.write_u16::<BigEndian>(header.len as u16)?;
} else if header.len > 65535 {
writer.write_u64::<BigEndian>(header.len)?;
}
if let Some(mask) = header.mask {
writer.write_all(&mask)?
}
Ok(())
}
pub fn read_header<R>(reader: &mut R) -> WebSocketResult<DataFrameHeader>
where
R: Read,
{
let byte0 = reader.read_u8()?;
let byte1 = reader.read_u8()?;
let flags = DataFrameFlags::from_bits_truncate(byte0);
let opcode = byte0 & 0x0F;
let len = match byte1 & 0x7F {
0...125 => u64::from(byte1 & 0x7F),
126 => {
let len = u64::from(reader.read_u16::<BigEndian>()?);
if len <= 125 {
return Err(WebSocketError::DataFrameError("Invalid data frame length"));
}
len
}
127 => {
let len = reader.read_u64::<BigEndian>()?;
if len <= 65535 {
return Err(WebSocketError::DataFrameError("Invalid data frame length"));
}
len
}
_ => unreachable!(),
};
if opcode >= 8 {
if len >= 126 {
return Err(WebSocketError::DataFrameError(
"Control frame length too long",
));
}
if !flags.contains(DataFrameFlags::FIN) {
return Err(WebSocketError::ProtocolError(
"Illegal fragmented control frame",
));
}
}
let mask = if byte1 & 0x80 == 0x80 {
Some([
reader.read_u8()?,
reader.read_u8()?,
reader.read_u8()?,
reader.read_u8()?,
])
} else {
None
};
Ok(DataFrameHeader {
flags,
opcode,
mask,
len,
})
}
#[cfg(all(feature = "nightly", test))]
mod tests {
use super::*;
use test;
#[test]
fn test_read_header_simple() {
let header = [0x81, 0x2B];
let obtained = read_header(&mut &header[..]).unwrap();
let expected = DataFrameHeader {
flags: DataFrameFlags::FIN,
opcode: 1,
mask: None,
len: 43,
};
assert_eq!(obtained, expected);
}
#[test]
fn test_write_header_simple() {
let header = DataFrameHeader {
flags: DataFrameFlags::FIN,
opcode: 1,
mask: None,
len: 43,
};
let expected = [0x81, 0x2B];
let mut obtained = Vec::with_capacity(2);
write_header(&mut obtained, header).unwrap();
assert_eq!(&obtained[..], &expected[..]);
}
#[test]
fn test_read_header_complex() {
let header = [0x42, 0xFE, 0x02, 0x00, 0x02, 0x04, 0x08, 0x10];
let obtained = read_header(&mut &header[..]).unwrap();
let expected = DataFrameHeader {
flags: DataFrameFlags::RSV1,
opcode: 2,
mask: Some([2, 4, 8, 16]),
len: 512,
};
assert_eq!(obtained, expected);
}
#[test]
fn test_write_header_complex() {
let header = DataFrameHeader {
flags: DataFrameFlags::RSV1,
opcode: 2,
mask: Some([2, 4, 8, 16]),
len: 512,
};
let expected = [0x42, 0xFE, 0x02, 0x00, 0x02, 0x04, 0x08, 0x10];
let mut obtained = Vec::with_capacity(8);
write_header(&mut obtained, header).unwrap();
assert_eq!(&obtained[..], &expected[..]);
}
#[bench]
fn bench_read_header(b: &mut test::Bencher) {
let header = vec![0x42u8, 0xFE, 0x02, 0x00, 0x02, 0x04, 0x08, 0x10];
b.iter(|| {
read_header(&mut &header[..]).unwrap();
});
}
#[bench]
fn bench_write_header(b: &mut test::Bencher) {
let header = DataFrameHeader {
flags: DataFrameFlags::RSV1,
opcode: 2,
mask: Some([2, 4, 8, 16]),
len: 512,
};
let mut writer = Vec::with_capacity(8);
b.iter(|| {
write_header(&mut writer, header).unwrap();
});
}
}