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
extern crate libc;

mod ffi;

use std::marker::Send;

use ffi::*;


struct ParserContext<'a, H: ParserHandler + 'a> {
    parser: &'a mut Parser,
    handler: &'a mut H,
}

#[inline]
unsafe fn unwrap_context<'a, H: ParserHandler>(http: *mut HttpParser) -> &'a mut ParserContext<'a, H> {
    &mut *((*http).data as *mut ParserContext<H>)
}

macro_rules! notify_fn_wrapper {
    ( $callback:ident ) => ({
        extern "C" fn $callback<H: ParserHandler>(http: *mut HttpParser) -> libc::c_int {
            let context = unsafe { unwrap_context::<H>(http) };
            if context.handler.$callback(context.parser) { 0 } else { 1 }
        };

        $callback::<H>
    });
}

macro_rules! data_fn_wrapper {
    ( $callback:ident ) => ({
        extern "C" fn $callback<H: ParserHandler>(http: *mut HttpParser, data: *const u32, size: libc::size_t) -> libc::c_int {
            let slice = unsafe { std::slice::from_raw_parts(data as *const u8, size as usize) };
            let context = unsafe { unwrap_context::<H>(http) };
            if context.handler.$callback(context.parser, slice) { 0 } else { 1 }
        };

        $callback::<H>
    });
}

impl HttpParserSettings {
    fn new<H: ParserHandler>() -> HttpParserSettings {
        HttpParserSettings {
            on_url: data_fn_wrapper!(on_url),
            on_message_begin: notify_fn_wrapper!(on_message_begin),
            on_status: data_fn_wrapper!(on_status),
            on_header_field: data_fn_wrapper!(on_header_field),
            on_header_value: data_fn_wrapper!(on_header_value),
            on_headers_complete: notify_fn_wrapper!(on_headers_complete),
            on_body: data_fn_wrapper!(on_body),
            on_message_complete: notify_fn_wrapper!(on_message_complete),
            on_chunk_header: notify_fn_wrapper!(on_chunk_header),
            on_chunk_complete: notify_fn_wrapper!(on_chunk_complete),
        }
    }
}

// High level Rust interface

/// Used to define a set of callbacks in your code.
/// They would be called by the parser whenever new data is available.
/// You should bear in mind that the data might get in your callbacks in a partial form.
///
/// Return `bool` as a result of each function call - either
/// `true` for the "OK, go on" status, or `false` when you want to stop
/// the parser after the function call has ended.
///
/// All callbacks provide a default no-op implementation (i.e. they just return `true`).
///
#[allow(unused_variables)]
pub trait ParserHandler: Sized {
    /// Called when the URL part of a request becomes available.
    /// E.g. for `GET /forty-two HTTP/1.1` it will be called with `"/forty_two"` argument.
    ///
    /// It's not called in the response mode.
    fn on_url(&mut self, &mut Parser, &[u8]) -> bool {
        true
    }

    /// Called when a response status becomes available.
    ///
    /// It's not called in the request mode.
    fn on_status(&mut self, &mut Parser, &[u8]) -> bool {
        true
    }

    /// Called for each HTTP header key part.
    fn on_header_field(&mut self, &mut Parser, &[u8]) -> bool {
        true
    }

    /// Called for each HTTP header value part.
    fn on_header_value(&mut self, &mut Parser, &[u8]) -> bool {
        true
    }

    /// Called with body text as an argument when the new part becomes available.
    fn on_body(&mut self, &mut Parser, &[u8]) -> bool {
        true
    }

    /// Notified when all available headers have been processed.
    fn on_headers_complete(&mut self, &mut Parser) -> bool {
        true
    }

    /// Notified when the parser receives first bytes to parse.
    fn on_message_begin(&mut self, &mut Parser) -> bool {
        true
    }

    /// Notified when the parser has finished its job.
    fn on_message_complete(&mut self, &mut Parser) -> bool {
        true
    }

    fn on_chunk_header(&mut self, &mut Parser) -> bool {
        true
    }
    fn on_chunk_complete(&mut self, &mut Parser) -> bool {
        true
    }
}

fn http_method_name(method_code: u8) -> &'static str {
    unsafe {
        let method_str = http_method_str(method_code);
        let buf = std::ffi::CStr::from_ptr(method_str);
        return std::str::from_utf8(buf.to_bytes()).unwrap();
    }
}

fn _http_errno_name(errno: u8) -> &'static str {
    unsafe {
        let err_str = http_errno_name(errno);
        let buf = std::ffi::CStr::from_ptr(err_str);
        return std::str::from_utf8(buf.to_bytes()).unwrap();
    }
}

fn _http_errno_description(errno: u8) -> &'static str {
    unsafe {
        let err_str = http_errno_description(errno);
        let buf = std::ffi::CStr::from_ptr(err_str);
        return std::str::from_utf8(buf.to_bytes()).unwrap();
    }
}

/// The main parser interface.
///
/// # Example
/// ```
/// use http_muncher::{Parser, ParserHandler};
/// use std::str;
///
/// struct MyHandler;
/// impl ParserHandler for MyHandler {
///    fn on_header_field(&mut self, parser: &mut Parser, header: &[u8]) -> bool {
///        println!("{}: ", str::from_utf8(header).unwrap());
///        true
///    }
///    fn on_header_value(&mut self, parser: &mut Parser, value: &[u8]) -> bool {
///        println!("\t {:?}", str::from_utf8(value).unwrap());
///        true
///    }
/// }
///
/// let http_request = b"GET / HTTP/1.0\r\n\
///                      Content-Length: 0\r\n\r\n";
///
/// let mut parser = Parser::request();
/// parser.parse(&mut MyHandler {}, http_request);
/// ```
#[allow(dead_code)]
pub struct Parser {
    state: HttpParser,
    parser_type: ParserType,
    flags: u32,
}

unsafe impl Send for Parser {}

impl Parser {
    /// Creates a new parser instance for an HTTP response.
    pub fn response() -> Parser {
        Parser {
            parser_type: ParserType::HttpResponse,
            state: HttpParser::new(ParserType::HttpResponse),
            flags: 0,
        }
    }

    /// Creates a new parser instance for an HTTP request.
    pub fn request() -> Parser {
        Parser {
            parser_type: ParserType::HttpRequest,
            state: HttpParser::new(ParserType::HttpRequest),
            flags: 0,
        }
    }

    /// Creates a new parser instance to handle both HTTP requests and responses.
    pub fn request_and_response() -> Parser {
        Parser {
            parser_type: ParserType::HttpBoth,
            state: HttpParser::new(ParserType::HttpBoth),
            flags: 0,
        }
    }

    /// Parses the provided `data` and returns a number of bytes read.
    pub fn parse<H: ParserHandler>(&mut self, handler: &mut H, data: &[u8]) -> usize {
        unsafe {
            let mut context = ParserContext {
                parser: self,
                handler: handler,
            };

            context.parser.state.data = &mut context as *mut _ as *mut libc::c_void;

            let size =
                http_parser_execute(&mut context.parser.state as *mut _,
                                    &HttpParserSettings::new::<H>() as *const _,
                                    data.as_ptr(),
                                    data.len() as libc::size_t) as usize;

            context.parser.flags = http_get_struct_flags(&context.parser.state as *const _);

            size
        }
    }

    /// Returns an HTTP request or response version.
    pub fn http_version(&self) -> (u16, u16) {
        (self.state.http_major, self.state.http_minor)
    }

    /// Returns an HTTP response status code (think *404*).
    pub fn status_code(&self) -> u16 {
        return (self.flags & 0xFFFF) as u16;
    }

    /// Returns an HTTP method static string (`GET`, `POST`, and so on).
    pub fn http_method(&self) -> &'static str {
        let method_code = ((self.flags >> 16) & 0xFF) as u8;
        return http_method_name(method_code);
    }

    fn http_errnum(&self) -> u8 {
        return ((self.flags >> 24) & 0x7F) as u8;
    }

    /// Checks if the last `parse` call was finished successfully.
    /// Returns `true` if it wasn't.
    pub fn has_error(&self) -> bool {
        self.http_errnum() != 0x00
    }

    /// In case of a parsing error returns its mnemonic name.
    pub fn error(&self) -> &'static str {
        _http_errno_name(self.http_errnum())
    }

    /// In case of a parsing error returns its description.
    pub fn error_description(&self) -> &'static str {
        _http_errno_description(self.http_errnum())
    }

    /// Checks if an upgrade protocol (e.g. WebSocket) was requested.
    pub fn is_upgrade(&self) -> bool {
        return ((self.flags >> 31) & 0x01) == 1;
    }

    /// Checks if it was the final body chunk.
    pub fn is_final_chunk(&self) -> bool {
        return self.state.http_body_is_final() == 1;
    }

    /// If `should_keep_alive()` in the `on_headers_complete` or `on_message_complete` callback
    /// returns 0, then this should be the last message on the connection.
    /// If you are the server, respond with the "Connection: close" header.
    /// If you are the client, close the connection.
    pub fn should_keep_alive(&self) -> bool {
        self.state.http_should_keep_alive() == 1
    }

    pub fn pause(&mut self) {
        self.state.http_parser_pause(1);
    }

    pub fn unpause(&mut self) {
        self.state.http_parser_pause(0);
    }
}

impl std::fmt::Debug for Parser {
    fn fmt(&self, fmt: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
        let (version_major, version_minor) = self.http_version();
        return write!(fmt,
                      "status_code: {}\nmethod: {}\nerror: {}, {}\nupgrade: {}\nhttp_version: \
                       {}.{}",
                      self.status_code(),
                      self.http_method(),
                      self.error(),
                      self.error_description(),
                      self.is_upgrade(),
                      version_major,
                      version_minor);
    }
}

/// Returns a version of the underlying `http-parser` library.
pub fn version() -> (u32, u32, u32) {
    let version = unsafe { http_parser_version() };

    let major = (version >> 16) & 255;
    let minor = (version >> 8) & 255;
    let patch = version & 255;

    (major, minor, patch)
}

#[cfg(test)]
mod tests {
    use super::{version, ParserHandler, Parser};

    #[test]
    fn test_version() {
        assert_eq!((2, 7, 1), version());
    }

    #[test]
    fn test_request_parser() {
        struct TestRequestParser;

        impl ParserHandler for TestRequestParser {
            fn on_url(&mut self, _: &mut Parser, url: &[u8]) -> bool {
                assert_eq!(b"/say_hello", url);
                true
            }

            fn on_header_field(&mut self, _: &mut Parser, hdr: &[u8]) -> bool {
                assert!(hdr == b"Host" || hdr == b"Content-Length");
                true
            }

            fn on_header_value(&mut self, _: &mut Parser, val: &[u8]) -> bool {
                assert!(val == b"localhost.localdomain" || val == b"11");
                true
            }

            fn on_body(&mut self, parser: &mut Parser, body: &[u8]) -> bool {
                assert_eq!((1, 1), parser.http_version());
                assert_eq!(body, b"Hello world");
                true
            }
        }

        let req = b"POST /say_hello HTTP/1.1\r\nContent-Length: 11\r\nHost: localhost.localdomain\r\n\r\nHello world";

        let mut handler = TestRequestParser;

        let mut parser = Parser::request();
        let parsed = parser.parse(&mut handler, req);

        assert!(parsed > 0);
        assert!(!parser.has_error());
        assert_eq!("POST", parser.http_method());
    }

    #[test]
    fn test_response_parser() {
        struct TestResponseParser;

        impl ParserHandler for TestResponseParser {
            fn on_status(&mut self, _: &mut Parser, status: &[u8]) -> bool {
                assert_eq!(b"OK", status);
                true
            }

            fn on_header_field(&mut self, _: &mut Parser, hdr: &[u8]) -> bool {
                assert_eq!(b"Host", hdr);
                true
            }

            fn on_header_value(&mut self, _: &mut Parser, val: &[u8]) -> bool {
                assert_eq!(b"localhost.localdomain", val);
                true
            }
        }

        let req = b"HTTP/1.1 200 OK\r\nHost: localhost.localdomain\r\n\r\n";

        let mut handler = TestResponseParser;

        let mut parser = Parser::response();
        let parsed = parser.parse(&mut handler, req);

        assert!(parsed > 0);
        assert!(!parser.has_error());
        assert_eq!((1, 1), parser.http_version());
        assert_eq!(200, parser.status_code());
    }

    #[test]
    fn test_ws_upgrade() {
        struct DummyHandler;

        impl ParserHandler for DummyHandler {};

        let req = b"GET / HTTP/1.1\r\nConnection: Upgrade\r\nUpgrade: websocket\r\n\r\n";

        let mut handler = DummyHandler;

        let mut parser = Parser::request();
        parser.parse(&mut handler, req);

        assert_eq!(parser.is_upgrade(), true);
    }

    #[test]
    fn test_error_status() {
        struct DummyHandler {
            url_parsed: bool,
        }

        impl ParserHandler for DummyHandler {
            fn on_url(&mut self, _: &mut Parser, _: &[u8]) -> bool {
                self.url_parsed = true;
                false
            }

            fn on_header_field(&mut self, _: &mut Parser, _: &[u8]) -> bool {
                panic!("This callback shouldn't be executed!");
            }
        }

        let req = b"GET / HTTP/1.1\r\nHeader: hello\r\n\r\n";

        let mut handler = DummyHandler { url_parsed: false };

        let mut parser = Parser::request();
        parser.parse(&mut handler, req);

        assert!(handler.url_parsed);
    }

    #[test]
    fn test_streaming() {
        struct DummyHandler;

        impl ParserHandler for DummyHandler {};

        let req = b"GET / HTTP/1.1\r\nHeader: hello\r\n\r\n";

        let mut handler = DummyHandler;
        let mut parser = Parser::request();

        parser.parse(&mut handler, &req[0..10]);

        assert_eq!(parser.http_version(), (0, 0));
        assert!(!parser.has_error());

        parser.parse(&mut handler, &req[10..]);

        assert_eq!(parser.http_version(), (1, 1));
    }

    #[test]
    fn test_catch_error() {
        struct DummyHandler;

        impl ParserHandler for DummyHandler {};

        let req = b"UNKNOWN_METHOD / HTTP/3.0\r\nAnswer: 42\r\n\r\n";

        let mut handler = DummyHandler;
        let mut parser = Parser::request();

        parser.parse(&mut handler, req);

        assert!(parser.has_error());
        assert_eq!(parser.error(), "HPE_INVALID_METHOD");
    }
}