aboutsummaryrefslogtreecommitdiff
path: root/src/http/parser.rs
blob: 259ddf209c202f4bff900dcb852b6767bc513e82 (plain)
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
use async_std::io::Read;

use super::rule::{HTTPMessage, HeaderField, FieldName, FieldValue, FieldContent, RequestLine, Method, OriginForm, HTTPVersion, AbsolutePath, Query, Segment};
use super::flatten::{flatten, HTTPRequest};
use crate::peekable_bufreader::PeekableBufReader;

const HTAB: u8 = 0x09;
const SPACE: u8 = 0x20;
const PERCENT: u8 = 0x25;
const SLASH: u8 = 0x2F;
const DOT: u8 = 0x2E;
const COLON: u8 = 0x3A;
const QUESTION_MARK: u8 = 0x3F;
const ATSIGN: u8 = 0x40;

enum ErrorType {
    Missing,
    Malformed,
}

pub struct Parser<T>
    where T: Read + Unpin {
    source: PeekableBufReader<T>,
}

impl<T> Parser<T>
    where T: Read + Unpin {
    pub fn new(source: PeekableBufReader<T>) -> Self {
        Self {
            source,
        }
    }

    pub async fn parse(self) -> Option<HTTPRequest> {
        flatten(self.http_message().await?)
    }

    /*
    * RFC 7230, Page 19
    */
    async fn http_message(mut self) -> Option<HTTPMessage> {
        let start_line = self.start_line().await?;
        let mut header_fields = Vec::new();
        loop {
            if let Some(_) = self.consume_carriage_return().await {
                break;
            }
            header_fields.push(self.header_field().await?);
            self.consume_carriage_return().await?;
        }
        // GET Requests don't have a message body, and we only really deal with GET requests.
        // There's no need to examine the headers and attempt to read a message body.
        Some(HTTPMessage {
            request_line: start_line,
            header_fields,
        })
    }

    /*
    * RFC 7230, Page 23
    */
    async fn header_field(&mut self) -> Option<HeaderField> {
        let name = self.field_name().await?;
        self.consume_char(&COLON).await?;
        self.consume_optional_whitespace().await;
        let value = self.field_value().await?;
        self.consume_optional_whitespace().await;
        Some(HeaderField {
            name,
            value,
        })
    }

    /*
    * RFC 7230, Page 23
    */
    async fn field_name(&mut self) -> Option<FieldName> {
        Some(FieldName {
            lexeme: self.logical_token().await?,
        })
    }

    /*
    * RFC 7230, Page 23
    * obs-fold is deprecated except within message/http media. This isn't going to come up for us,
    * so we deviate from the grammar slightly.
    */
    async fn field_value(&mut self) -> Option<FieldValue> {
        let mut content = Vec::new();
        loop {
            // Look, no obs-fold!
            if let Some(value) = self.field_content().await {
                // Let's do some pre-emptive flattening here.
                content.push(value.first_char);
                if let Some(second_char) = value.second_char {
                    content.push(SPACE);
                    content.push(second_char);
                }
            } else {
                break;
            }
        }
        Some(FieldValue {
            content,
        })
    }

    /*
    * RFC 7230, Page 23
    */
    async fn field_content(&mut self) -> Option<FieldContent> {
        let first_char = self.field_vchar().await?;
        let second_char = if let Some(_) = self.consume_required_whitespace().await {
            Some(self.field_vchar().await?)
        } else {
            None
        };
        Some(FieldContent{
            first_char,
            second_char,
        })
    }

    async fn field_vchar(&mut self) -> Option<u8> {
        let next_char = self.source.peek().await?;
        if Self::is_visible_char(next_char) || Self::is_obs_text_char(next_char) {
            return self.source.next().await;
        }
        None
    }

    /*
    * RFC 7230, Page 21
    * This is a server, so the start-line is exclusively a request-line.
    */
    async fn start_line(&mut self) -> Option<RequestLine> {
        self.request_line().await
    }
    
    /*
    * RFC 7230, Page 21
    */
    async fn request_line(&mut self) -> Option<RequestLine> {
        let method = self.method().await?;
        self.consume_char(&SPACE).await?;
        let request_target = self.request_target().await?;
        self.consume_char(&SPACE).await?;
        let http_version = self.http_version().await?;
        self.consume_carriage_return().await?;
        Some(RequestLine {
            method,
            request_target,
            http_version,
        })
    }

    /*
    * RFC 7230, Page 41
    * We only serve some static content; therefore we only need support origin-form.
    */
    async fn request_target(&mut self) -> Option<OriginForm> {
        self.origin_form().await
    }

    /*
    * RFC 7230, Page 42
    */
    async fn origin_form(&mut self) -> Option<OriginForm> {
        let absolute_path = self.absolute_path().await?;
        let query = if let Some(_) = self.consume_char(&QUESTION_MARK).await {
            Some(self.query().await?)
        } else {
            None
        };
        Some(OriginForm {
            absolute_path,
            query,
        })
    }

    /*
    * RFC 7230, Page 16
    */
    async fn absolute_path(&mut self) -> Option<AbsolutePath> {
        let mut segments = Vec::new();
        self.consume_char(&SLASH).await?;
        segments.push(self.segment().await?);
        loop {
            if let None = self.consume_char(&SLASH).await {
                break;
            }
            if let Some(segment) = self.segment().await {
                segments.push(segment);
            } else {
                return None;
            }
        }
        Some(AbsolutePath {
            segments,
        })
    }

    /*
    * RFC 3986, Page 23
    */
    async fn segment(&mut self) -> Option<Segment> {
        let mut segment = Vec::new();
        while self.source.peek().await.is_some() {
            match self.consume_path_character().await {
                Ok(character) => segment.push(character as char),
                Err(ErrorType::Missing) => break,
                Err(ErrorType::Malformed) => return None,
            }
        }
        Some(Segment{
            lexeme: segment.into_iter().collect(),
        })
    }

    /*
    * RFC 3986, Page 50
    */
    async fn query(&mut self) -> Option<Query> {
        let mut query = Vec::new();
        while self.source.peek().await.is_some() {
            match self.consume_query_character().await {
                Ok(character) => query.push(character as char),
                Err(ErrorType::Missing) => break,
                Err(ErrorType::Malformed) => return None,
            }
        }
        Some(Query{
            lexeme: query.into_iter().collect(),
        })
    }

    /*
    * RFC 7230, Page 14
    */
    async fn http_version(&mut self) -> Option<HTTPVersion> {
        self.consume_logical_token("HTTP").await?;
        self.consume_char(&SLASH).await?;
        let major = Self::ascii_digit_to_value(&self.consume_digit().await?);
        self.consume_char(&DOT).await?;
        let minor = Self::ascii_digit_to_value(&self.consume_digit().await?);
        Some(HTTPVersion{
            major,
            minor,
        })
    }
    
    /*
    * RFC 7230, Page 21
    */
    async fn method(&mut self) -> Option<Method> {
        Method::from_string(&self.logical_token().await?)
    }

    /*
    * RFC 7230, Page 27
    */
    async fn logical_token(&mut self) -> Option<String> {
        let mut logical_token = Vec::new();
        if !Self::is_logical_token_char(self.source.peek().await?) {
            return None;
        }
        while self.source.peek().await.is_some() && Self::is_logical_token_char(self.source.peek().await.unwrap()) {
            logical_token.push(self.source.next().await.unwrap() as char);
        }
        Some(logical_token.into_iter().collect())
    }

    async fn consume_char(&mut self, character: &u8) -> Option<u8> {
        let next_char = self.source.peek().await?;
        if *next_char == *character {
            return self.source.next().await;
        }
        None
    }
    
    async fn consume_logical_token(&mut self, value: &str) -> Option<String> {
        let logical_token = self.logical_token().await?;
        if logical_token == value {
            return Some(logical_token);
        }
        None
    }

    /*
    * RFC 3986, Page 23
    */
    async fn consume_path_character(&mut self) -> Result<u8, ErrorType> {
        match self.consume_unreserved_character().await {
            Some(character) => return Ok(character),
            _ => {}
        }
        match self.consume_sub_delim_character().await {
            Some(character) => return Ok(character),
            _ => {}
        }
        match self.consume_char(&COLON).await {
            Some(character) => return Ok(character),
            _ => {}
        }
        match self.consume_char(&ATSIGN).await {
            Some(character) => return Ok(character),
            _ => {}
        }
        self.consume_percent_encoded().await
    }

    /*
    * RFC 3986, Page 50
    */
    async fn consume_query_character(&mut self) -> Result<u8, ErrorType> {
        match self.consume_unreserved_character().await {
            Some(character) => return Ok(character),
            _ => {}
        }
        match self.consume_sub_delim_character().await {
            Some(character) => return Ok(character),
            _ => {}
        }
        match self.consume_char(&COLON).await {
            Some(character) => return Ok(character),
            _ => {}
        }
        match self.consume_char(&ATSIGN).await {
            Some(character) => return Ok(character),
            _ => {}
        }
        match self.consume_char(&SLASH).await {
            Some(character) => return Ok(character),
            _ => {}
        }
        match self.consume_char(&QUESTION_MARK).await {
            Some(character) => return Ok(character),
            _ => {}
        }
        self.consume_percent_encoded().await
    }

    /*
    * RFC 5234, Page 5
    */
    async fn consume_carriage_return(&mut self) -> Option<()> {
        self.consume_char(&0x0D).await?;
        self.consume_char(&0x0A).await?;
        Some(())
    }

    async fn consume_optional_whitespace(&mut self) {
        loop {
            if let None = self.consume_char(&SPACE).await {
                if let None = self.consume_char(&HTAB).await {
                    break;
                }
            }
        }
    }

    async fn consume_required_whitespace(&mut self) -> Option<()> {
        if let None = self.consume_char(&SPACE).await {
            if let None = self.consume_char(&HTAB).await {
                return None;
            }
        }
        loop {
            if let None = self.consume_char(&SPACE).await {
                if let None = self.consume_char(&HTAB).await {
                    break;
                }
            }
        }
        Some(())
    }

    /*
    * RFC 5234, Page 14
    */
    async fn consume_digit(&mut self) -> Option<u8> {
        let next_char = self.source.peek().await?;
        if Self::is_digit_char(next_char) {
            return self.source.next().await
        }
        None
    }

    async fn consume_unreserved_character(&mut self) -> Option<u8> {
        let next_char = self.source.peek().await?;
        if Self::is_unreserved_char(next_char) {
            return self.source.next().await
        }
        None
    }

    async fn consume_sub_delim_character(&mut self) -> Option<u8> {
        let next_char = self.source.peek().await?;
        if Self::is_sub_delim_char(next_char) {
            return self.source.next().await
        }
        None
    }

    async fn consume_percent_encoded(&mut self) -> Result<u8, ErrorType> {
        self.consume_char(&PERCENT).await.ok_or(ErrorType::Missing)?;
        let high_word = self.consume_hex_digit().await.ok_or(ErrorType::Malformed)?;
        let low_word = self.consume_hex_digit().await.ok_or(ErrorType::Malformed)?;
        Self::hex_digits_to_byte(high_word, low_word).ok_or(ErrorType::Malformed)
    }

    async fn consume_hex_digit(&mut self) -> Option<u8> {
        let next_char = self.source.peek().await?;
        if Self::is_hex_digit_char(next_char) {
            return self.source.next().await
        }
        None
    }

    fn ascii_digit_to_value(character: &u8) -> u32 {
        *character as u32 - 0x30
    }

    fn hex_digits_to_byte(high_word: u8, low_word: u8) -> Option<u8> {
        u8::from_str_radix(&((high_word as char).to_string() + &(low_word as char).to_string()), 16).ok()
    }

    /*
    * RFC 7230, Page 27
    */
    fn is_logical_token_char(character: &u8) -> bool {
        *character == 0x21 || // !
        *character == 0x23 || // #
        *character == 0x24 || // $
        *character == 0x25 || // %
        *character == 0x26 || // &
        *character == 0x27 || // '
        *character == 0x2A || // *
        *character == 0x2B || // +
        *character == 0x2D || // -
        *character == 0x2E || // .
        *character == 0x5E || // ^
        *character == 0x5F || // _
        *character == 0x60 || // `
        *character == 0x7C || // |
        *character == 0x7E || // ~
        Self::is_digit_char(character) ||
        Self::is_alpha_char(character)
    }

    /*
    * RFC 3986, Page 13
    */
    fn is_unreserved_char(character: &u8) -> bool {
        Self::is_alpha_char(character) ||
        Self::is_digit_char(character) ||
        *character == 0x2D || // -
        *character == 0x2E || // .
        *character == 0x5F || // _
        *character == 0x7E    // ~
    }

    /*
    * RFC 3986, Page 13
    */
    fn is_sub_delim_char(character: &u8) -> bool {
        *character == 0x21 || // !
        *character == 0x24 || // $
        *character == 0x26 || // &
        *character == 0x27 || // '
        *character == 0x28 || // (
        *character == 0x29 || // )
        *character == 0x2A || // *
        *character == 0x2B || // +
        *character == 0x2C || // ,
        *character == 0x3B || // ;
        *character == 0x3D    // =
    }

    /*
    * RFC 5234, Page 13
    */
    fn is_alpha_char(character: &u8) -> bool {
        (*character >= 0x41 && *character <= 0x5A) || (*character >= 0x61 && *character <= 0x7A)
    }

    /*
    * RFC 5234, Page 14
    */
    fn is_digit_char(character: &u8) -> bool {
        *character >= 0x30 && *character <= 0x39
    }

    /*
    * RFC 5234, Page 14
    */
    fn is_hex_digit_char(character: &u8) -> bool {
        Self::is_digit_char(character) ||
        *character == 0x41 || // A
        *character == 0x42 || // B
        *character == 0x43 || // C
        *character == 0x44 || // D
        *character == 0x45 || // E
        *character == 0x46 || // F
        *character == 0x61 || // a
        *character == 0x62 || // b
        *character == 0x63 || // c
        *character == 0x64 || // d
        *character == 0x65 || // e
        *character == 0x66    // f
    }

    /*
    * RFC 5234, Page 14
    */
    fn is_visible_char(character: &u8) -> bool {
        *character >= 0x21 && *character <= 0x7E
    }

    fn is_obs_text_char(character: &u8) -> bool {
        *character >= 0x80
    }
}