Last active
August 29, 2015 14:27
-
-
Save m4rw3r/b2f7b86723a84934f362 to your computer and use it in GitHub Desktop.
Version of the attoparsec benchmark for parsing HTTP header dumps writtien in rust using my experimental parser combinator.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate parser; | |
use parser::{Parser, Error}; | |
use std::fs::File; | |
use std::env; | |
#[derive(Debug)] | |
struct Request<'a> { | |
method: &'a [u8], | |
uri: &'a [u8], | |
version: &'a [u8], | |
} | |
#[derive(Debug)] | |
struct Header<'a> { | |
name: &'a [u8], | |
value: Vec<&'a [u8]>, | |
} | |
fn is_token(c: u8) -> bool { | |
c < 128 && c > 31 && b"()<>@,;:\\\"/[]?={} \t".iter().position(|&i| i == c).is_none() | |
} | |
fn is_horizontal_space(c: u8) -> bool { | |
c == b' ' || c == b'\t' | |
} | |
fn is_end_of_line(c: u8) -> bool { | |
c == b'\r' || c == b'\n' | |
} | |
fn end_of_line<'a>(p: Parser<'a, u8, (), Error<u8>>) -> Parser<'a, u8, u8, Error<u8>> { | |
p.char(b'\r').then(|p| p.char(b'\n')) | |
.or(|p| p.char(b'\n')) | |
} | |
fn http_version<'a>(p: Parser<'a, u8, (), Error<u8>>) -> Parser<'a, u8, &'a [u8], Error<u8>> { | |
p.skip_while1(|c| b"HTP/".iter().position(|&i| i == c).is_some()) | |
.then(|p| p.take_while1(|c| c >= b'0' && c <= b'9' || c == b'.')) | |
} | |
fn request_line<'a>(p: Parser<'a, u8, (), Error<u8>>) -> Parser<'a, u8, Request<'a>, Error<u8>> { | |
p.take_while1(is_token).skip_while1(|c| c == b' ').bind(|method, p| | |
p.take_while1(|c| c != b' ').skip_while1(|c| c == b' ').bind(|uri, p| | |
http_version(p).bind(|version, p| | |
p.skip(end_of_line).ret::<Request, Error<u8>>(Request { | |
method: method, | |
uri: uri, | |
version: version, | |
})))) | |
} | |
fn message_header<'a>(p: Parser<'a, u8, (), Error<u8>>) -> Parser<'a, u8, Header, Error<u8>> { | |
p.take_while1(is_token).skip(|p| p.satisfy(|c| c == b':')).bind(|name, p| { | |
p.many(|p| p.skip_while1(is_horizontal_space).then(|p| p.take_till(is_end_of_line).skip(end_of_line))).bind(|lines, p|{ | |
p.ret::<_, Error<u8>>(Header{ | |
name: name, | |
value: lines, | |
})})}) | |
} | |
fn request<'a>(p: Parser<'a, u8, (), Error<u8>>) -> Parser<'a, u8, (Request, Vec<Header>), Error<u8>> { | |
p.then(request_line) | |
.bind(|r, p| p.many(message_header).map(|h| (r, h))) | |
.bind(|r, p| p.then(end_of_line).then(|p| p.ret(r))) | |
} | |
fn main() { | |
let mut contents: Vec<u8> = Vec::new(); | |
{ | |
use std::io::Read; | |
let mut file = File::open(env::args().nth(1).expect("File to read")).ok().expect("Failed to open file"); | |
let _ = file.read_to_end(&mut contents).unwrap(); | |
} | |
let i = parser::Iter::new(contents.as_ref(), request); | |
// println!("data: {:?}", String::from_utf8_lossy(&contents[contents.len() - 21362..])); | |
// let r: Vec<(Request, Vec<Header>)> = p.many(request).unwrap(); | |
println!("num: {}", i.count()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment