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
use parser::HttpParser;

/// `ParseAction` defines the potential actions that could be returned by any callback function.
/// The parser uses it to determine consequent behavior.
#[derive(Clone)]
pub enum ParseAction {
    /// No special actions. Keep the normal execution.
    None,
    /// Skip body
    SkipBody,
}

/// Result of a callback function.
pub type CallbackResult = Result<ParseAction, String>;

/// It defines the callback functions that would be called by parser.
///
/// # Example
///
/// ```
/// # use http_parser::*;
/// #
/// struct Callback;
///
/// impl HttpParserCallback for Callback {
///     fn on_message_begin(&mut self, parser: &mut HttpParser) -> CallbackResult {
///         println!("Message begin");
///         Ok(ParseAction::None)
///     }
///
///     // Override other functions as you wish
/// }
///
/// let mut cb = Callback;
/// ```
pub trait HttpParserCallback {
    /// Function called when starting parsing a new HTTP request or response.
    #[allow(unused_variables)]
    fn on_message_begin(&mut self, parser: &mut HttpParser) -> CallbackResult {
        Ok(ParseAction::None)
    }

    /// Function called when a URL is parsed.
    #[allow(unused_variables)]
    fn on_url(&mut self, parser: &mut HttpParser, data: &[u8],) -> CallbackResult {
        Ok(ParseAction::None)
    }

    /// Function called when a status is parsed.
    #[allow(unused_variables)]
    fn on_status(&mut self, parser: &mut HttpParser, data: &[u8]) -> CallbackResult {
        Ok(ParseAction::None)
    }

    /// Function called when a header field is parsed.
    #[allow(unused_variables)]
    fn on_header_field(&mut self, parser: &mut HttpParser, data: &[u8]) -> CallbackResult {
        Ok(ParseAction::None)
    }

    /// Function called when a header value is parsed.
    #[allow(unused_variables)]
    fn on_header_value(&mut self, parser: &mut HttpParser, data: &[u8]) -> CallbackResult {
        Ok(ParseAction::None)
    }

    /// Function called when all headers are parsed.
    #[allow(unused_variables)]
    fn on_headers_complete(&mut self, parser: &mut HttpParser) -> CallbackResult {
        Ok(ParseAction::None)
    }

    /// Function called when the body is parsed.
    #[allow(unused_variables)]
    fn on_body(&mut self, parser: &mut HttpParser, data: &[u8]) -> CallbackResult {
        Ok(ParseAction::None)
    }

    /// Function called when finishing parsing a HTTP request or response.
    #[allow(unused_variables)]
    fn on_message_complete(&mut self, parser: &mut HttpParser) -> CallbackResult {
        Ok(ParseAction::None)
    }
}