Struct Parser

Source
pub struct Parser { /* private fields */ }
Expand description

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);

Implementations§

Source§

impl Parser

Source

pub fn response() -> Parser

Creates a new parser instance for an HTTP response.

Source

pub fn request() -> Parser

Creates a new parser instance for an HTTP request.

Examples found in repository?
examples/parser.rs (line 40)
37fn main() {
38    // Now we can create a parser instance with our callbacks handler:
39    let mut callbacks_handler = MyHandler {};
40    let mut parser = Parser::request();
41
42    // Let's define a mock HTTP request:
43    let http_request = "GET / HTTP/1.1\r\n\
44                        Content-Type: text/plain\r\n\
45                        Content-Length: 2\r\n\
46                        Hello: World\r\n\r\n\
47                        Hi";
48
49    // And now we're ready to go!
50    parser.parse(&mut callbacks_handler, http_request.as_bytes());
51
52    // Now that callbacks have been called, we can introspect
53    // the parsing results. For instance, print the HTTP version:
54    let (http_major, http_minor) = parser.http_version();
55    println!("\nHTTP v.{}.{}", http_major, http_minor);
56}
Source

pub fn request_and_response() -> Parser

Creates a new parser instance to handle both HTTP requests and responses.

Source

pub fn parse<H: ParserHandler>(&mut self, handler: &mut H, data: &[u8]) -> usize

Parses the provided data and returns a number of bytes read.

Examples found in repository?
examples/parser.rs (line 50)
37fn main() {
38    // Now we can create a parser instance with our callbacks handler:
39    let mut callbacks_handler = MyHandler {};
40    let mut parser = Parser::request();
41
42    // Let's define a mock HTTP request:
43    let http_request = "GET / HTTP/1.1\r\n\
44                        Content-Type: text/plain\r\n\
45                        Content-Length: 2\r\n\
46                        Hello: World\r\n\r\n\
47                        Hi";
48
49    // And now we're ready to go!
50    parser.parse(&mut callbacks_handler, http_request.as_bytes());
51
52    // Now that callbacks have been called, we can introspect
53    // the parsing results. For instance, print the HTTP version:
54    let (http_major, http_minor) = parser.http_version();
55    println!("\nHTTP v.{}.{}", http_major, http_minor);
56}
Source

pub fn http_version(&self) -> (u16, u16)

Returns an HTTP request or response version.

Examples found in repository?
examples/parser.rs (line 54)
37fn main() {
38    // Now we can create a parser instance with our callbacks handler:
39    let mut callbacks_handler = MyHandler {};
40    let mut parser = Parser::request();
41
42    // Let's define a mock HTTP request:
43    let http_request = "GET / HTTP/1.1\r\n\
44                        Content-Type: text/plain\r\n\
45                        Content-Length: 2\r\n\
46                        Hello: World\r\n\r\n\
47                        Hi";
48
49    // And now we're ready to go!
50    parser.parse(&mut callbacks_handler, http_request.as_bytes());
51
52    // Now that callbacks have been called, we can introspect
53    // the parsing results. For instance, print the HTTP version:
54    let (http_major, http_minor) = parser.http_version();
55    println!("\nHTTP v.{}.{}", http_major, http_minor);
56}
Source

pub fn status_code(&self) -> u16

Returns an HTTP response status code (think 404).

Source

pub fn http_method(&self) -> &'static str

Returns an HTTP method static string (GET, POST, and so on).

Source

pub fn has_error(&self) -> bool

Checks if the last parse call was finished successfully. Returns true if it wasn’t.

Source

pub fn error(&self) -> &'static str

In case of a parsing error returns its mnemonic name.

Source

pub fn error_description(&self) -> &'static str

In case of a parsing error returns its description.

Source

pub fn is_upgrade(&self) -> bool

Checks if an upgrade protocol (e.g. WebSocket) was requested.

Source

pub fn is_final_chunk(&self) -> bool

Checks if it was the final body chunk.

Source

pub fn should_keep_alive(&self) -> bool

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.

Source

pub fn pause(&mut self)

Source

pub fn unpause(&mut self)

Trait Implementations§

Source§

impl Debug for Parser

Source§

fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Send for Parser

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.