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
use crate::payload::{Opcode, WsPayload};
use crate::WsError;
use std::convert::TryFrom;

#[derive(Debug)]
pub enum Message {
    Text(String),
    Binary(Vec<u8>),
    Ping(String),
    Pong(String),
    Close,
}
impl From<Message> for Vec<u8> {
    fn from(message: Message) -> Self {
        let s = match message {
            Message::Text(s) => s,
            _ => "".to_owned(),
        };
        let mut buffer: Vec<u8> = Vec::with_capacity(s.len() + 2);
        buffer.push(129);
        match s.len() as u64 {
            size @ 0..=125 => {
                buffer.push(size as u8);
            }
            size if size > u32::MAX as u64 => {
                let bytes: [u8; 8] = (size as u64).to_be_bytes();
                buffer.push(127);
                buffer.extend_from_slice(&bytes);
            }
            size if size <= u32::MAX as u64 => {
                let bytes: [u8; 2] = (size as u16).to_be_bytes();
                buffer.push(126);
                buffer.extend_from_slice(&bytes);
            }
            _ => panic!("Don't know what to do here..."),
        }
        buffer.extend_from_slice(s.as_bytes());
        buffer
    }
}
#[inline(always)]
/// This masks the payload byte by byte and does a bitwise exclusive on index % 4 of mask
pub fn mask_payload<'a, 'b>(incoming: &'a mut &'b mut [u8], mask: [u8; 4]) -> &'a [u8] {
    let data: &'b mut [u8] = std::mem::take(incoming);
    for i in 0..data.len() {
        data[i] ^= mask[i % 4];
    }
    data
}
impl TryFrom<Vec<u8>> for Message {
    type Error = WsError;

    fn try_from(payload: Vec<u8>) -> Result<Self, Self::Error> {
        let dataframe = WsPayload::new(&payload);
        let opcode: Opcode = dataframe.get_opcode().into();

        match (opcode, dataframe.get_payload()) {
            (Opcode::Text, Some(payload)) => {
                let mut payload = payload.to_owned();
                let masked_payload =
                    mask_payload(&mut &mut *payload, dataframe.get_masking_key()).to_vec();

                match String::from_utf8(masked_payload) {
                    Ok(message) => Ok(Message::Text(message)),
                    Err(_) => Err(WsError::InvalidPayload),
                }
            }
            (Opcode::Ping, Some(payload)) => {
                let mut payload = payload.to_owned();
                let masked_payload =
                    mask_payload(&mut &mut *payload, dataframe.get_masking_key()).to_vec();

                match String::from_utf8(masked_payload) {
                    Ok(message) => Ok(Message::Pong(message)),
                    Err(_) => Err(WsError::InvalidPayload),
                }
            }
            /*            Opcode::Continuation => {}
            Opcode::Close => {}
            Opcode::Ping => {}
            Opcode::Pong => {}
            Opcode::Unknown => {}*/
            _ => Err(WsError::InvalidPayload),
        }
    }
}
impl Message {
    pub fn is_closed(&self) -> bool {
        match self {
            Message::Close => true,
            _ => false,
        }
    }
}