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
//! This crate provides barebone, slice-based parsers for extracting [request
//! line](https://tools.ietf.org/html/rfc7230#section-3.1.1) components and [header
//! fields](https://tools.ietf.org/html/rfc7230#section-3.2) from HTTP requests.
//!
//! In general, components are extracted along defined delimiters, but further processing
//! and syntax validation is left to higher layers.
//!
//! ## Example
//!
//! ```rust
//! use uhttp_request::{RequestLine, Headers};
//!
//! let req = b"GET /abc?k=v HTTP/1.1\r\nHost: example.com\r\nAccept: text/*\r\n\r\nbody";
//!
//! let (reqline, rest) = RequestLine::new(req).unwrap();
//! assert_eq!(reqline.method, "GET");
//! assert_eq!(reqline.target, "/abc?k=v");
//! assert_eq!(reqline.version, "HTTP/1.1");
//!
//! let mut headers = Headers::new(rest);
//!
//! let h = headers.next().unwrap().unwrap();
//! assert_eq!(h.name, "Host");
//! assert_eq!(h.val, b" example.com");
//!
//! let h = headers.next().unwrap().unwrap();
//! assert_eq!(h.name, "Accept");
//! assert_eq!(h.val, b" text/*");
//!
//! assert!(headers.next().is_none());
//!
//! let rest = headers.into_inner();
//! assert_eq!(rest, b"body");
//! ```

#![feature(field_init_shorthand)]
#![feature(conservative_impl_trait)]

extern crate memchr;

use memchr::memchr;

/// Errors that may occur when processing request header.
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum Error {
    /// Unexpected EOF.
    Partial,
    /// Malformed syntax.
    Syntax,
}

/// Specialized result using custom `Error`.
pub type Result<T> = std::result::Result<T, Error>;

/// A "Request-Line" [RFC7230§3.1.1] that begins an HTTP request.
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub struct RequestLine<'a> {
    /// Request method on target resource.
    ///
    /// This is guaranteed to be free of spaces but is not guaranteed to be free of other
    /// whitespace or otherwise syntactically correct.
    pub method: &'a str,

    /// Target resource of request.
    ///
    /// This is guaranteed to be free of spaces but is not guaranteed to be free of other
    /// whitespace or otherwise syntactically correct.
    pub target: &'a str,

    /// HTTP protocol version of request.
    ///
    /// This is guaranteed to be free of spaces but is not guaranteed to be free of other
    /// whitespace or otherwise syntactically correct.
    pub version: &'a str,
}

impl<'a> RequestLine<'a> {
    /// Try to parse the given bytes into `RequestLine` components.
    ///
    /// On success, return `Ok((rl, rest))`, where `rl` is the `RequestLine` and `rest` is
    /// a slice that begins directly after the Request-Line terminating CRLF.
    pub fn new(buf: &'a [u8]) -> Result<(Self, &'a [u8])> {
        // Ignore leading empty lines [RFC7230§3.5].
        let start = skip_empty_lines(buf)?;

        // Retrieve contents of initial line and split by spaces.
        let (line, rest) = next_line(start)?;
        let line = std::str::from_utf8(line).map_err(|_| Error::Syntax)?;

        let mut chunks = line.split(' ');
        let method = chunks.next().ok_or(Error::Syntax)?;
        let target = chunks.next().ok_or(Error::Syntax)?;
        let version = chunks.next().ok_or(Error::Syntax)?;

        if chunks.next().is_some() {
            return Err(Error::Syntax);
        }

        Ok((RequestLine { method, target, version }, rest))
    }
}

/// An HTTP request header field [RFC7230§3.2].
#[derive(Copy, Clone, Eq, PartialEq, Debug, Hash)]
pub struct Header<'a> {
    /// Header name, with surrounding whitespace trimmed.
    ///
    /// This is not guaranteed to be free of internal whitespace or otherwise
    /// syntactically correct.
    pub name: &'a str,

    /// Raw header value.
    pub val: &'a [u8],
}

/// Iterator over all header fields in a request.
pub struct Headers<'a>(&'a [u8]);

impl<'a> Headers<'a> {
    /// Create a new `Headers` iterator over the given bytes, which must begin directly
    /// after the Request-Line CRLF.
    pub fn new(s: &'a [u8]) -> Self {
        Headers(s)
    }

    /// Retrieve the remaining bytes that haven't been processed.
    ///
    /// If called after the last yielded header, this slice will contain the beginning of
    /// the request body.
    pub fn into_inner(self) -> &'a [u8] { self.0 }
}

impl<'a> Iterator for Headers<'a> {
    type Item = Result<Header<'a>>;

    fn next(&mut self) -> Option<Self::Item> {
        let (line, rest) = match next_line(self.0) {
            Ok(x) => x,
            Err(e) => return Some(Err(e)),
        };

        self.0 = rest;

        // Headers are terminated by an empty line [RFC7230§3].
        if line.is_empty() {
            return None;
        }

        let (name, val) = match memchr(b':', line) {
            Some(idx) => line.split_at(idx),
            None => return Some(Err(Error::Syntax)),
        };

        let name = match std::str::from_utf8(name) {
            Ok(s) => s.trim(),
            Err(_) => return Some(Err(Error::Syntax)),
        };

        // Name must be nonempty [RFC7230§3.2].
        if name.is_empty() {
            return Some(Err(Error::Syntax));
        }

        // Skip past ':'.
        let val = &val[1..];

        Some(Ok(Header { name, val }))
    }
}

/// Consume CRLFs until the first non-CRLF character, returning a slice beginning at that
/// character.
fn skip_empty_lines<'a>(mut bytes: &'a [u8]) -> Result<&'a [u8]> {
    loop {
        match check_crlf(bytes) {
            Ok(rest) => bytes = rest,
            Err(Error::Partial) => return Err(Error::Partial),
            Err(Error::Syntax) => return Ok(bytes),
        }
    }
}

/// Retrieve the next chunk in the request, up to and not including the nearest CRLF.
fn next_line<'a>(bytes: &'a [u8]) -> Result<(&'a [u8], &'a [u8])> {
    let (line, rest) = match memchr(b'\r', bytes) {
        Some(idx) => bytes.split_at(idx),
        None => return Err(Error::Partial),
    };

    let rest = check_crlf(rest)?;

    Ok((line, rest))
}

/// Check if the given slice begins with CRLF and, if it does, return the slice
/// immediately after.
fn check_crlf<'a>(bytes: &'a [u8]) -> Result<&'a [u8]> {
    if bytes.len() < 2 {
        Err(Error::Partial)
    } else if bytes.starts_with(&b"\r\n"[..]) {
        // Skip over CRLF.
        Ok(&bytes[2..])
    } else {
        Err(Error::Syntax)
    }
}

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

    #[test]
    fn test_request_line() {
        // Some testcases from httparse, MIT Copyright (c) 2015 Sean McArthur.

        let (req, rest) = RequestLine::new(b"GET / HTTP/1.1\r\n\r\n").unwrap();

        assert_eq!(req.method, "GET");
        assert_eq!(req.target, "/");
        assert_eq!(req.version, "HTTP/1.1");

        assert_eq!(rest, &b"\r\n"[..]);

        let (req, rest) = RequestLine::new(
            b"GET / HTTP/1.1\r\nHost: foo.com\r\nCookie: \r\n\r\n"
        ).unwrap();

        assert_eq!(req.method, "GET");
        assert_eq!(req.target, "/");
        assert_eq!(req.version, "HTTP/1.1");
        assert_eq!(rest, &b"Host: foo.com\r\nCookie: \r\n\r\n"[..]);

        let (req, rest) = RequestLine::new(
            b"GET / HTTP/1.1\r\nA: A\r\nB: B\r\nC: C\r\nD: D\r\n\r\n"
        ).unwrap();

        assert_eq!(req.method, "GET");
        assert_eq!(req.target, "/");
        assert_eq!(req.version, "HTTP/1.1");
        assert_eq!(rest, &b"A: A\r\nB: B\r\nC: C\r\nD: D\r\n\r\n"[..]);

        let (req, rest) = RequestLine::new(
            b"GET / HTTP/1.1\r\nHost: foo.com\r\nUser-Agent: \xe3\x81\xb2\xe3/1.0\r\n\r\n",
        ).unwrap();

        assert_eq!(req.method, "GET");
        assert_eq!(req.target, "/");
        assert_eq!(req.version, "HTTP/1.1");
        assert_eq!(rest, &b"Host: foo.com\r\nUser-Agent: \xe3\x81\xb2\xe3/1.0\r\n\r\n"[..]);

        let (req, rest) = RequestLine::new(b"GET / HTTP/1.1\r\n\r").unwrap();

        assert_eq!(req.method, "GET");
        assert_eq!(req.target, "/");
        assert_eq!(req.version, "HTTP/1.1");
        assert_eq!(rest, &b"\r"[..]);

        assert_eq!(RequestLine::new(&b"GET / HTTP/1.1\nHost: foo.bar\n\n"[..]),
            Err(Error::Partial));

        let (req, rest) = RequestLine::new(&b"\r\n\r\nGET / HTTP/1.1\r\n\r\n"[..]).unwrap();

        assert_eq!(req.method, "GET");
        assert_eq!(req.target, "/");
        assert_eq!(req.version, "HTTP/1.1");
        assert_eq!(rest, &b"\r\n"[..]);

        assert_eq!(RequestLine::new(&b"\n\nGET / HTTP/1.1\n\n"[..]), Err(Error::Partial));

        assert_eq!(RequestLine::new(&b"GET\n/ HTTP/1.1\r\nHost: foo.bar\r\n\r\n"[..]),
            Err(Error::Syntax));

        let (req, rest) = RequestLine::new(b"\n\n\nGET / HTTP/1.1\r\n\n").unwrap();

        assert_eq!(req.method, "\n\n\nGET");
        assert_eq!(req.target, "/");
        assert_eq!(req.version, "HTTP/1.1");
        assert_eq!(rest, &b"\n"[..]);

        let (req, rest) = RequestLine::new(b"\r\n\nGET / H\tTTP/1.1\r\n\n").unwrap();

        assert_eq!(req.method, "\nGET");
        assert_eq!(req.target, "/");
        assert_eq!(req.version, "H\tTTP/1.1");
        assert_eq!(rest, &b"\n"[..]);

        assert_eq!(RequestLine::new(b"\r\n\rGET / HTTP/1.1\r\n\n"), Err(Error::Syntax));
        assert_eq!(RequestLine::new(b"GET /some path/ HTTP/1.1\r\n\r"), Err(Error::Syntax));
        assert_eq!(RequestLine::new(b"GET\r\n"), Err(Error::Syntax));
        assert_eq!(RequestLine::new(b"GET /\r\n"), Err(Error::Syntax));
        assert_eq!(RequestLine::new(b"GET / HTTP/1.1 \r\n"), Err(Error::Syntax));
        assert_eq!(RequestLine::new(b"GET /  \r\n"), Err(Error::Syntax));
        assert_eq!(RequestLine::new(b"GET / HTTP/1.1"), Err(Error::Partial));
        assert_eq!(RequestLine::new(b"GET / HTTP/1.1\r"), Err(Error::Partial));
        assert_eq!(RequestLine::new(b"GET / HTTP/1.1\n"), Err(Error::Partial));
    }

    #[test]
    fn test_headers() {
        let mut h = Headers::new(
            b"Content-Type: text/html\r\nContent-Length: 1337\r\n\r\nbody text"
        );
        let n = h.next().unwrap().unwrap();
        assert_eq!(n.name, "Content-Type");
        assert_eq!(n.val, b" text/html");
        let n = h.next().unwrap().unwrap();
        assert_eq!(n.name, "Content-Length");
        assert_eq!(n.val, b" 1337");
        assert!(h.next().is_none());
        assert_eq!(h.into_inner(), b"body text");

        let mut h = Headers::new(
            b"  Content-Type \t\t: text/html\r\n\r\nbody text"
        );
        let n = h.next().unwrap().unwrap();
        assert_eq!(n.name, "Content-Type");
        assert_eq!(n.val, b" text/html");
        assert!(h.next().is_none());
        assert_eq!(h.into_inner(), b"body text");

        let mut h = Headers::new(
            b"\tContent-Type \t\t: text/html\r\n\r\n"
        );
        let n = h.next().unwrap().unwrap();
        assert_eq!(n.name, "Content-Type");
        assert_eq!(n.val, b" text/html");
        assert!(h.next().is_none());
        assert_eq!(h.into_inner(), b"");

        let mut h = Headers::new(
            b"  Content-Type \t\t: text/html\n"
        );
        let n = h.next().unwrap();
        assert_eq!(n, Err(Error::Partial));
    }

    #[test]
    fn test_skip_empty_lines() {
        assert_eq!(skip_empty_lines(b"GET"), Ok(&b"GET"[..]));
        assert_eq!(skip_empty_lines(b"\r\n\r\nGET"), Ok(&b"GET"[..]));
        assert_eq!(skip_empty_lines(b"\r\n\rGET"), Ok(&b"\rGET"[..]));
        assert_eq!(skip_empty_lines(b"\nGET"), Ok(&b"\nGET"[..]));
    }

    #[test]
    fn test_next_line() {
        assert_eq!(next_line(b"abc\r\ndef"), Ok((&b"abc"[..], &b"def"[..])));
        assert_eq!(next_line(b"abc def\r\nghi"), Ok((&b"abc def"[..], &b"ghi"[..])));
        assert_eq!(next_line(b"abc\r\n"), Ok((&b"abc"[..], &b""[..])));
        assert_eq!(next_line(b"abc"), Err(Error::Partial));
        assert_eq!(next_line(b"abc\n"), Err(Error::Partial));
        assert_eq!(next_line(b"\r\ndef"), Ok((&b""[..], &b"def"[..])));
        assert_eq!(next_line(b""), Err(Error::Partial));
    }

    #[test]
    fn test_check_crlf() {
        assert_eq!(check_crlf(b"\r\nabc"), Ok(&b"abc"[..]));
        assert_eq!(check_crlf(b"\r"), Err(Error::Partial));
        assert_eq!(check_crlf(b""), Err(Error::Partial));
        assert_eq!(check_crlf(b"\n"), Err(Error::Partial));
        assert_eq!(check_crlf(b"\nabc"), Err(Error::Syntax));
        assert_eq!(check_crlf(b"abc\r\n"), Err(Error::Syntax));
    }
}