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
use async_std::prelude::*;
use async_std::io::{Read};
use crate::{Error};

pub async fn read_first_line<I>(input: &mut I, data: (&mut Vec<u8>, &mut Vec<u8>, &mut Vec<u8>), limit: Option<usize>) -> Result<usize, Error>
    where
    I: Read + Unpin,
{
    let mut part = 0;
    let mut length = 0;
    let mut stage = 0; // 0..data, 1..\r, 2..\n

    loop {
        let mut bytes = [0u8];
        let size = match input.read(&mut bytes).await {
            Ok(size) => size,
            Err(_) => return Err(Error::UnableToRead),
        };
        length += size;

        if size == 0 {
            break;
        } else if limit.is_some() && limit.unwrap() < length { // method + url + version = 2065
            return Err(Error::InvalidInput);
        } else if bytes[0] == 32 { // space
            part += 1;
            continue;
        } else if bytes[0] == 13 { // \r
            stage = 1;
            continue;
        } else if bytes[0] == 10 { // \n
            if stage == 1 {
                break;
            } else {
                return Err(Error::InvalidInput);
            }
        }

        match part {
            0 => data.0.push(bytes[0]),
            1 => data.1.push(bytes[0]),
            _ => data.2.push(bytes[0]),
        };
    }

    Ok(length)
}

pub async fn read_header<I>(input: &mut I, data: (&mut Vec<u8>, &mut Vec<u8>), limit: Option<usize>) -> Result<usize, Error>
    where
    I: Read + Unpin,
{
    let mut length = 0;
    let mut stage = 0; // 0..name, 1..:, 2..space, 3..value, 4..\r, 5..\n

    loop {
        let mut bytes = [0u8];
        let size = match input.read(&mut bytes).await {
            Ok(size) => size,
            Err(_) => return Err(Error::UnableToRead),
        };
        length += size;

        if size == 0 {
            break;
        } else if limit.is_some() && limit.unwrap() < length {
            return Err(Error::LimitExceeded);
        } else if stage == 0 && bytes[0] == 58 { // first :
            stage = 1;
            continue;
        } else if stage == 1 && bytes[0] == 32 { // first space
            stage = 2;
            continue;
        } else if bytes[0] == 13 { // first/second \r
            if stage == 0 || stage == 2 {
                stage = 3;
                continue;
            } else {
                return Err(Error::InvalidInput);
            }
        } else if bytes[0] == 10 { // first/second \n
            if stage == 3 {
                break;
            } else {
                return Err(Error::InvalidInput);
            }
        }

        if stage == 0 {
            data.0.push(bytes[0]);
        } else if stage == 2 {
            data.1.push(bytes[0]);
        }
    }

    Ok(length)
}

pub async fn read_exact<I>(input: &mut I, data: &mut Vec<u8>, length: usize) -> Result<usize, Error>
    where
    I: Read + Unpin,
{
    let mut bytes = vec![0u8; length];

    match input.read_exact(&mut bytes).await {
        Ok(size) => size,
        Err(_) => return Err(Error::UnableToRead),
    };

    data.append(&mut bytes);

    Ok(length)
}
// 19;ex\r\n
// data\r\n
// \r\n
pub async fn read_chunk_line<I>(input: &mut I, data: (&mut Vec<u8>, &mut Vec<u8>), limit: Option<usize>) -> Result<usize, Error>
    where
    I: Read + Unpin,
{
    let mut length = 0;
    let mut stage = 0; // 0..number, 1..extension 2..\r, 3=\n

    loop {
        let mut bytes = [0u8];
        let size = match input.read(&mut bytes).await {
            Ok(size) => size,
            Err(_) => return Err(Error::UnableToRead),
        };
        length += size;

        if size == 0 { // end of data
            break;
        } else if limit.is_some() && limit.unwrap() < length {
            return Err(Error::LimitExceeded);
        } else if stage == 0 && bytes[0] == 59 { // char ;
            stage = 1;
            continue;
        } else if bytes[0] == 13 { // char \r
            if stage == 0 || stage == 1 {
                stage = 2;
                continue;
            } else {
                return Err(Error::InvalidInput);
            }
        } else if bytes[0] == 10 { // char \n
            break;
        }
        match stage {
            0 => data.0.push(bytes[0]),
            1 => data.1.push(bytes[0]),
            _ => (),
        };
    }

    Ok(length)
}

pub async fn read_chunks<I>(input: &mut I, data: &mut Vec<u8>, limits: (Option<usize>, Option<usize>)) -> Result<usize, Error>
    where
    I: Read + Unpin,
{
    let mut length = 0;
    let mut total = 0; // actual data size

    loop {
        let (mut size, mut ext) = (vec![], vec![]);
        length += read_chunk_line(input, (&mut size, &mut ext), limits.0).await?;
        let size = match String::from_utf8(size) {
            Ok(length) => match i64::from_str_radix(&length, 16) {
                Ok(length) => length as usize,
                Err(_) => return Err(Error::InvalidInput),
            },
            Err(_) => return Err(Error::InvalidInput),
        };
        total += size;
        if size == 0 {
            length += read_exact(input, &mut Vec::new(), 2).await?;
            break; // last chunk
        } else if limits.1.is_some() && total + size > limits.1.unwrap() {
            return Err(Error::LimitExceeded);
        } else {
            length += read_exact(input, data, size).await?;
            length += read_exact(input, &mut Vec::new(), 2).await?;
        }
    }

    Ok(length)
}

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

    #[async_std::test]
    async fn reads_first_line() {
        let (mut a, mut b, mut c) = (vec![], vec![], vec![]);
        let size = read_first_line(&mut "OPTIONS /path HTTP/1.1\r\n".as_bytes(), (&mut a, &mut b, &mut c), None).await.unwrap();
        assert_eq!(size, 24);
        assert_eq!(a, b"OPTIONS");
        assert_eq!(b, b"/path");
        assert_eq!(c, b"HTTP/1.1");
        let (mut a, mut b, mut c) = (vec![], vec![], vec![]);
        let exceeded = read_first_line(&mut "OPTI\r\n".as_bytes(), (&mut a, &mut b, &mut c), Some(1)).await;
        assert!(exceeded.is_err());
    }

    #[async_std::test]
    async fn reads_header() {
        let (mut name, mut value) = (vec![], vec![]);
        let size = read_header(&mut "Foo: foo\r\nBar: bar\r\n".as_bytes(), (&mut name, &mut value), None).await.unwrap();
        assert_eq!(size, 10);
        assert_eq!(name, b"Foo");
        assert_eq!(value, b"foo");
        let (mut name, mut value) = (vec![], vec![]);
        let size = read_header(&mut "\r\n".as_bytes(), (&mut name, &mut value), None).await.unwrap();
        assert_eq!(size, 2);
        assert_eq!(name, b"");
        assert_eq!(value, b"");
        let exceeded = read_header(&mut "Foo".as_bytes(), (&mut name, &mut value), Some(1)).await;
        assert!(exceeded.is_err());
    }

    #[async_std::test]
    async fn reads_exact() {
        let mut output = Vec::new();
        read_exact(&mut "0123456789".as_bytes(), &mut output, 5).await.unwrap();
        assert_eq!(String::from_utf8(output).unwrap(), "01234");
    }

    #[async_std::test]
    async fn reads_chunk_line() {
        let (mut number, mut ext) = (vec![], vec![]);
        let size = read_chunk_line(&mut "6;ex;ex\r\n".as_bytes(), (&mut number, &mut ext), None).await.unwrap();
        assert_eq!(size, 9);
        assert_eq!(String::from_utf8(number).unwrap(), "6");
        assert_eq!(String::from_utf8(ext).unwrap(), "ex;ex");
        let (mut number, mut ext) = (vec![], vec![]);
        let exceeded = read_chunk_line(&mut "6\r\n".as_bytes(), (&mut number, &mut ext), Some(1)).await;
        assert!(exceeded.is_err());
    }

    #[async_std::test]
    async fn reads_chunks() {
        let mut output = Vec::new();
        let size = read_chunks(&mut "6\r\nHello \r\n6;ex=fo\r\nWorld!\r\n0\r\n\r\nTrail: er\r\n\r\n".as_bytes(), &mut output, (None, None)).await.unwrap(); // with extension `ex=fo` and trailer `Trail: er`
        assert_eq!(size, 33);
        assert_eq!(String::from_utf8(output).unwrap(), "Hello World!");
        let mut output = Vec::new();
        let exceeded = read_chunks(&mut "6\r\nHello 0\r\n\r\n".as_bytes(), &mut output, (Some(1), None)).await;
        assert!(exceeded.is_err());
    }
}