1use base64::prelude::*;
2
3#[derive(Debug, Eq, PartialEq, Clone, Copy)]
7pub enum OpCode {
8 Continue,
10
11 Text,
13
14 Binary,
16
17 Close,
19
20 Ping,
22
23 Pong,
25
26 Bad,
28}
29
30impl_more::impl_display_enum! {
31 OpCode:
32 Continue => "CONTINUE",
33 Text => "TEXT",
34 Binary => "BINARY",
35 Close => "CLOSE",
36 Ping => "PING",
37 Pong => "PONG",
38 Bad => "BAD",
39}
40
41impl From<OpCode> for u8 {
42 fn from(op: OpCode) -> u8 {
43 use self::OpCode::*;
44
45 match op {
46 Continue => 0,
47 Text => 1,
48 Binary => 2,
49 Close => 8,
50 Ping => 9,
51 Pong => 10,
52 Bad => {
53 tracing::error!("Attempted to convert invalid opcode to u8. This is a bug.");
54 8 }
56 }
57 }
58}
59
60impl From<u8> for OpCode {
61 fn from(byte: u8) -> OpCode {
62 use self::OpCode::*;
63
64 match byte {
65 0 => Continue,
66 1 => Text,
67 2 => Binary,
68 8 => Close,
69 9 => Ping,
70 10 => Pong,
71 _ => Bad,
72 }
73 }
74}
75
76#[derive(Debug, Eq, PartialEq, Clone, Copy)]
78pub enum CloseCode {
79 Normal,
82
83 Away,
86
87 Protocol,
89
90 Unsupported,
94
95 Abnormal,
100
101 Invalid,
105
106 Policy,
111
112 Size,
115
116 Extension,
122
123 Error,
126
127 Restart,
130
131 Again,
135
136 #[doc(hidden)]
137 Tls,
138
139 #[doc(hidden)]
140 Other(u16),
141}
142
143impl From<CloseCode> for u16 {
144 fn from(code: CloseCode) -> u16 {
145 use self::CloseCode::*;
146
147 match code {
148 Normal => 1000,
149 Away => 1001,
150 Protocol => 1002,
151 Unsupported => 1003,
152 Abnormal => 1006,
153 Invalid => 1007,
154 Policy => 1008,
155 Size => 1009,
156 Extension => 1010,
157 Error => 1011,
158 Restart => 1012,
159 Again => 1013,
160 Tls => 1015,
161 Other(code) => code,
162 }
163 }
164}
165
166impl From<u16> for CloseCode {
167 fn from(code: u16) -> CloseCode {
168 use self::CloseCode::*;
169
170 match code {
171 1000 => Normal,
172 1001 => Away,
173 1002 => Protocol,
174 1003 => Unsupported,
175 1006 => Abnormal,
176 1007 => Invalid,
177 1008 => Policy,
178 1009 => Size,
179 1010 => Extension,
180 1011 => Error,
181 1012 => Restart,
182 1013 => Again,
183 1015 => Tls,
184 _ => Other(code),
185 }
186 }
187}
188
189#[derive(Debug, Eq, PartialEq, Clone)]
190pub struct CloseReason {
192 pub code: CloseCode,
194
195 pub description: Option<String>,
197}
198
199impl From<CloseCode> for CloseReason {
200 fn from(code: CloseCode) -> Self {
201 CloseReason {
202 code,
203 description: None,
204 }
205 }
206}
207
208impl<T: Into<String>> From<(CloseCode, T)> for CloseReason {
209 fn from(info: (CloseCode, T)) -> Self {
210 CloseReason {
211 code: info.0,
212 description: Some(info.1.into()),
213 }
214 }
215}
216
217static WS_GUID: &[u8] = b"258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
220
221const WS_ACCEPT_LEN: usize = 28;
223
224pub fn hash_key(key: &[u8]) -> [u8; WS_ACCEPT_LEN] {
228 let hash = {
229 use sha1::Digest as _;
230
231 let mut hasher = sha1::Sha1::new();
232
233 hasher.update(key);
234 hasher.update(WS_GUID);
235
236 hasher.finalize()
237 };
238
239 let mut hash_b64 = [0; WS_ACCEPT_LEN];
240 let n = BASE64_STANDARD.encode_slice(hash, &mut hash_b64).unwrap();
241 assert_eq!(n, WS_ACCEPT_LEN);
242
243 hash_b64
244}
245
246#[cfg(test)]
247mod test {
248 #![allow(unused_imports, unused_variables, dead_code)]
249 use super::*;
250
251 macro_rules! opcode_into {
252 ($from:expr => $opcode:pat) => {
253 match OpCode::from($from) {
254 e @ $opcode => {}
255 e => unreachable!("{:?}", e),
256 }
257 };
258 }
259
260 macro_rules! opcode_from {
261 ($from:expr => $opcode:pat) => {
262 let res: u8 = $from.into();
263 match res {
264 e @ $opcode => {}
265 e => unreachable!("{:?}", e),
266 }
267 };
268 }
269
270 #[test]
271 fn test_to_opcode() {
272 opcode_into!(0 => OpCode::Continue);
273 opcode_into!(1 => OpCode::Text);
274 opcode_into!(2 => OpCode::Binary);
275 opcode_into!(8 => OpCode::Close);
276 opcode_into!(9 => OpCode::Ping);
277 opcode_into!(10 => OpCode::Pong);
278 opcode_into!(99 => OpCode::Bad);
279 }
280
281 #[test]
282 fn test_from_opcode() {
283 opcode_from!(OpCode::Continue => 0);
284 opcode_from!(OpCode::Text => 1);
285 opcode_from!(OpCode::Binary => 2);
286 opcode_from!(OpCode::Close => 8);
287 opcode_from!(OpCode::Ping => 9);
288 opcode_from!(OpCode::Pong => 10);
289 }
290
291 #[test]
292 #[should_panic]
293 fn test_from_opcode_debug() {
294 opcode_from!(OpCode::Bad => 99);
295 }
296
297 #[test]
298 fn test_from_opcode_display() {
299 assert_eq!(format!("{}", OpCode::Continue), "CONTINUE");
300 assert_eq!(format!("{}", OpCode::Text), "TEXT");
301 assert_eq!(format!("{}", OpCode::Binary), "BINARY");
302 assert_eq!(format!("{}", OpCode::Close), "CLOSE");
303 assert_eq!(format!("{}", OpCode::Ping), "PING");
304 assert_eq!(format!("{}", OpCode::Pong), "PONG");
305 assert_eq!(format!("{}", OpCode::Bad), "BAD");
306 }
307
308 #[test]
309 fn test_hash_key() {
310 let hash = hash_key(b"hello actix-web");
311 assert_eq!(&hash, b"cR1dlyUUJKp0s/Bel25u5TgvC3E=");
312 }
313
314 #[test]
315 fn close_code_from_u16() {
316 assert_eq!(CloseCode::from(1000u16), CloseCode::Normal);
317 assert_eq!(CloseCode::from(1001u16), CloseCode::Away);
318 assert_eq!(CloseCode::from(1002u16), CloseCode::Protocol);
319 assert_eq!(CloseCode::from(1003u16), CloseCode::Unsupported);
320 assert_eq!(CloseCode::from(1006u16), CloseCode::Abnormal);
321 assert_eq!(CloseCode::from(1007u16), CloseCode::Invalid);
322 assert_eq!(CloseCode::from(1008u16), CloseCode::Policy);
323 assert_eq!(CloseCode::from(1009u16), CloseCode::Size);
324 assert_eq!(CloseCode::from(1010u16), CloseCode::Extension);
325 assert_eq!(CloseCode::from(1011u16), CloseCode::Error);
326 assert_eq!(CloseCode::from(1012u16), CloseCode::Restart);
327 assert_eq!(CloseCode::from(1013u16), CloseCode::Again);
328 assert_eq!(CloseCode::from(1015u16), CloseCode::Tls);
329 assert_eq!(CloseCode::from(2000u16), CloseCode::Other(2000));
330 }
331
332 #[test]
333 fn close_code_into_u16() {
334 assert_eq!(1000u16, Into::<u16>::into(CloseCode::Normal));
335 assert_eq!(1001u16, Into::<u16>::into(CloseCode::Away));
336 assert_eq!(1002u16, Into::<u16>::into(CloseCode::Protocol));
337 assert_eq!(1003u16, Into::<u16>::into(CloseCode::Unsupported));
338 assert_eq!(1006u16, Into::<u16>::into(CloseCode::Abnormal));
339 assert_eq!(1007u16, Into::<u16>::into(CloseCode::Invalid));
340 assert_eq!(1008u16, Into::<u16>::into(CloseCode::Policy));
341 assert_eq!(1009u16, Into::<u16>::into(CloseCode::Size));
342 assert_eq!(1010u16, Into::<u16>::into(CloseCode::Extension));
343 assert_eq!(1011u16, Into::<u16>::into(CloseCode::Error));
344 assert_eq!(1012u16, Into::<u16>::into(CloseCode::Restart));
345 assert_eq!(1013u16, Into::<u16>::into(CloseCode::Again));
346 assert_eq!(1015u16, Into::<u16>::into(CloseCode::Tls));
347 assert_eq!(2000u16, Into::<u16>::into(CloseCode::Other(2000)));
348 }
349}