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
impl Parser
Sourcepub fn request() -> Parser
pub fn request() -> Parser
Creates a new parser instance for an HTTP request.
Examples found in repository?
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}
Sourcepub fn request_and_response() -> Parser
pub fn request_and_response() -> Parser
Creates a new parser instance to handle both HTTP requests and responses.
Sourcepub fn parse<H: ParserHandler>(&mut self, handler: &mut H, data: &[u8]) -> usize
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?
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}
Sourcepub fn http_version(&self) -> (u16, u16)
pub fn http_version(&self) -> (u16, u16)
Returns an HTTP request or response version.
Examples found in repository?
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}
Sourcepub fn status_code(&self) -> u16
pub fn status_code(&self) -> u16
Returns an HTTP response status code (think 404).
Sourcepub fn http_method(&self) -> &'static str
pub fn http_method(&self) -> &'static str
Returns an HTTP method static string (GET
, POST
, and so on).
Sourcepub fn has_error(&self) -> bool
pub fn has_error(&self) -> bool
Checks if the last parse
call was finished successfully.
Returns true
if it wasn’t.
Sourcepub fn error_description(&self) -> &'static str
pub fn error_description(&self) -> &'static str
In case of a parsing error returns its description.
Sourcepub fn is_upgrade(&self) -> bool
pub fn is_upgrade(&self) -> bool
Checks if an upgrade protocol (e.g. WebSocket) was requested.
Sourcepub fn is_final_chunk(&self) -> bool
pub fn is_final_chunk(&self) -> bool
Checks if it was the final body chunk.
Sourcepub fn should_keep_alive(&self) -> bool
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.