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
use crate::packet::{Packet, PacketData, PacketType, ParsePacketError};
use nom::{
    branch::alt, bytes::complete::take, character::complete::char as nom_char,
    character::complete::digit1, multi::fold_many0, Err::Failure, IResult,
};

pub mod decoder {
    use super::*;

    /// Decode a packet with string data from a UTF-8 string.
    /// Each packet has a packet type (Open, Close, Message...) and a data section.
    /// `decode_packet` assumes the packet's data is a string.
    ///
    /// # Arguments
    /// * `input` - A slice containing the string-encoded packet
    ///
    /// # Example
    ///
    /// ```rust
    /// use engine_io_parser::packet::{Packet, PacketType};
    /// use engine_io_parser::string::decoder::*;
    ///
    /// assert_eq!(decode_packet("4Hello world!"), Ok(Packet {
    ///     packet_type: PacketType::Message,
    ///     data: "Hello world!".into(),
    /// }))
    /// ```
    pub fn decode_packet(input: &str) -> Result<Packet, ParsePacketError> {
        if let Ok((_, packet)) = parse_string_packet(input, None) {
            Ok(packet)
        } else {
            Err(ParsePacketError {
                message: "Unable to decode packet".to_owned(),
            })
        }
    }

    /// Decode a string-encoded payload containing multiple packets.
    /// Returns a Vec of parsed `Packet`s, with the appropriate `PacketData` type
    /// for each packet if successful.
    /// In an encoded payload, each packet is denoted with its length and its
    /// data type; either binary or string. This function returns string packet
    /// data as is, and parses binary packets from base64 to a byte array.
    ///
    /// # Arguments
    /// * `input` - a string containing an encoded payload.
    ///
    /// ```rust
    /// use engine_io_parser::packet::{Packet, PacketData, PacketType};
    /// use engine_io_parser::string::decoder::*;
    ///
    /// assert_eq!(
    ///     decode_payload("6:4hello2:4€"),
    ///     Ok(vec![
    ///         Packet {
    ///             packet_type: PacketType::Message,
    ///             data: "hello".into(),
    ///         },
    ///         Packet {
    ///             packet_type: PacketType::Message,
    ///             data: "€".into()
    ///         }
    ///     ]),
    /// );
    pub fn decode_payload(input: &str) -> Result<Vec<Packet>, ParsePacketError> {
        if let Ok((_, packets)) = parse_string_payload(input) {
            Ok(packets)
        } else {
            Err(ParsePacketError {
                message: "Unable to decode payload".to_owned(),
            })
        }
    }
}

pub mod encoder {
    use super::*;

    /// Encode a packet with string data to a UTF-8 string.
    ///
    /// # Arguments
    /// * `input` - A `Packet` with string data
    ///
    /// # Example
    ///
    /// ```rust
    /// use engine_io_parser::packet::{Packet, PacketData, PacketType};
    /// use engine_io_parser::string::encoder::*;
    ///
    /// assert_eq!(
    ///     encode_packet(&Packet {
    ///         packet_type: PacketType::Message,
    ///         data: "Hello world!".into(),
    ///     }),
    ///     "4Hello world!",
    /// );
    ///
    /// ```
    pub fn encode_packet(input: &Packet) -> String {
        serialize_packet(input)
    }

    /// Encode a packet with binary data to a UTF-8 string.
    /// Note: calling `encode_payload` with Packets containing binary data
    /// also results in base64-encoded output.
    ///
    /// # Arguments
    /// * `input` - A `Packet` with binary data
    ///
    /// # Example
    ///
    /// ```rust
    /// use engine_io_parser::packet::{Packet, PacketData, PacketType};
    /// use engine_io_parser::string::encoder::*;
    ///
    /// assert_eq!(
    ///     encode_base64_packet(&Packet {
    ///        packet_type: PacketType::Message,
    ///        data: vec![1u8, 2u8, 3u8, 4u8].into(),
    ///     }),
    ///     "4AQIDBA=="
    /// );
    /// ```
    pub fn encode_base64_packet(input: &Packet) -> String {
        serialize_packet(&input)
    }

    /// Encode a payload containing multiple packets with either binary or string
    /// data to a UTF-8 string.
    ///
    /// # Arguments
    /// * `input`  - A list of `Packet`s with either string or binary data each
    ///
    /// # Example
    ///
    /// ```rust
    /// use engine_io_parser::packet::{Packet, PacketData, PacketType};
    /// use engine_io_parser::string::encoder::*;
    ///
    ///  assert_eq!(
    ///     encode_payload(&[
    ///         Packet {
    ///             packet_type: PacketType::Message,
    ///             data: "€".into(),
    ///         },
    ///         Packet {
    ///             packet_type: PacketType::Message,
    ///             data: vec![1u8, 2u8, 3u8, 4u8].into(),
    ///         }
    ///     ]),
    ///     "2:4€10:b4AQIDBA=="
    /// );
    /// ```
    pub fn encode_payload(input: &[Packet]) -> String {
        serialize_payload(input)
    }
}

fn parse_string_packet<'a>(input: &'a str, data_length: Option<usize>) -> IResult<&'a str, Packet> {
    let (input, packet_type_index) = take_u8(input)?;
    let (input, data) = match data_length {
        Some(l) => take(l)(input)?,
        None => ("", input),
    };
    let packet_type = PacketType::parse_from_u8(packet_type_index, input)?;
    Ok((
        input,
        Packet {
            packet_type,
            data: PacketData::Plaintext(data.to_owned()),
        },
    ))
}

fn parse_string_payload(input: &str) -> IResult<&str, Vec<Packet>> {
    fold_many0(
        parse_string_packet_in_payload,
        Vec::new(),
        |mut acc: Vec<Packet>, item| {
            acc.push(item);
            acc
        },
    )(input)
}

fn parse_string_packet_in_payload(input: &str) -> IResult<&str, Packet> {
    // string packet length in a payload is the actual packet data character length + 1 (packet type)
    let (input, packet_length) = take_usize(input)?;
    // Take the colon which is the string separator
    let (input, _) = nom_char(':')(input)?;
    // packet length also contains the packet type, but we're sending the length of the data part.
    let data_length = (packet_length - 1) as usize;
    let (input, packet) = alt((
        parse_plaintext_string_packet(data_length),
        parse_base64_string_packet(data_length),
    ))(input)?;
    Ok((input, packet))
}

fn parse_plaintext_string_packet(data_length: usize) -> impl Fn(&str) -> IResult<&str, Packet> {
    move |input: &str| {
        let (input, packet) = parse_string_packet(input, Some(data_length))?;
        Ok((input, packet))
    }
}

fn parse_base64_string_packet(data_length: usize) -> impl Fn(&str) -> IResult<&str, Packet> {
    move |input: &str| {
        // base64-encoded data starts with b and counts for the data length
        let (input, _) = nom_char('b')(input)?;
        let data_length = data_length - 1;
        let (input, packet_type_index) = take_u8(input)?;
        let (input, data_b64) = take(data_length)(input)?;
        let packet_type = PacketType::parse_from_u8(packet_type_index, input)?;
        match base64::decode(data_b64) {
            Ok(vec) => Ok((
                input,
                Packet {
                    packet_type,
                    data: PacketData::from(vec),
                },
            )),
            Err(_) => Err(Failure((input, nom::error::ErrorKind::TakeWhile1))),
        }
    }
}

fn serialize_packet(packet: &Packet) -> String {
    let packet_type_int = packet.packet_type as u8;
    let data: String = match &packet.data {
        PacketData::Plaintext(data) => data.to_string(),
        PacketData::Binary(data) => base64::encode(data),
        PacketData::Empty => String::from(""),
    };
    packet_type_int.to_string() + &data
}

fn serialize_payload(packets: &[Packet]) -> String {
    packets.iter().fold(String::from(""), |mut acc, packet| {
        let serialized = serialize_packet(&packet);
        acc.push_str(
            &(match &packet.data {
                PacketData::Binary(_) => {
                    // Adding + 1 to the length because of the `b` character indicating base64-encoded data.
                    (serialized.chars().count() + 1).to_string() + ":b" + &serialized
                }
                PacketData::Plaintext(_) => {
                    serialized.chars().count().to_string() + ":" + &serialized
                }
                PacketData::Empty => String::from(""),
            }),
        );
        acc
    })
}

fn take_u8(input: &str) -> IResult<&str, u8> {
    let (input, num) = digit1(input)?;
    if let Ok(num) = num.parse::<u8>() {
        Ok((input, num))
    } else {
        Err(Failure((input, nom::error::ErrorKind::Digit)))
    }
}

fn take_usize(input: &str) -> IResult<&str, usize> {
    let (input, num) = digit1(input)?;
    if let Ok(num) = num.parse::<usize>() {
        Ok((input, num))
    } else {
        Err(Failure((input, nom::error::ErrorKind::Digit)))
    }
}

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

    #[test]
    fn test_take_u8() {
        assert_eq!(take_u8("0"), Ok(("", 0u8)));
        assert_eq!(take_u8("254"), Ok(("", 254u8)));
        assert_eq!(
            take_u8("hello"),
            Err(nom::Err::Error(("hello", nom::error::ErrorKind::Digit)))
        );
    }

    #[test]
    fn test_take_usize() {
        assert_eq!(take_usize("0"), Ok(("", 0)));
        assert_eq!(take_usize("424242"), Ok(("", 424242)));
        assert_eq!(take_usize("0h2"), Ok(("h2", 0)));
    }
}