forked from rust-bakery/nom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.rs
199 lines (163 loc) · 4.35 KB
/
http.rs
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
#![cfg_attr(rustfmt, rustfmt_skip)]
#[global_allocator]
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
use criterion::*;
use nom::{IResult, bytes::complete::{tag, take_while1}, character::complete::{line_ending, char}, multi::many1};
#[cfg_attr(rustfmt, rustfmt_skip)]
#[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]>,
}
#[cfg_attr(rustfmt, rustfmt_skip)]
#[cfg_attr(feature = "cargo-clippy", allow(match_same_arms))]
fn is_token(c: u8) -> bool {
match c {
128..=255 => false,
0..=31 => false,
b'(' => false,
b')' => false,
b'<' => false,
b'>' => false,
b'@' => false,
b',' => false,
b';' => false,
b':' => false,
b'\\' => false,
b'"' => false,
b'/' => false,
b'[' => false,
b']' => false,
b'?' => false,
b'=' => false,
b'{' => false,
b'}' => false,
b' ' => false,
_ => true,
}
}
fn not_line_ending(c: u8) -> bool {
c != b'\r' && c != b'\n'
}
fn is_space(c: u8) -> bool {
c == b' '
}
fn is_not_space(c: u8) -> bool {
c != b' '
}
fn is_horizontal_space(c: u8) -> bool {
c == b' ' || c == b'\t'
}
fn is_version(c: u8) -> bool {
c >= b'0' && c <= b'9' || c == b'.'
}
fn request_line(input: &[u8]) -> IResult<&[u8], Request<'_>> {
let (input, method) = take_while1(is_token)(input)?;
let (input, _) = take_while1(is_space)(input)?;
let (input, uri) = take_while1(is_not_space)(input)?;
let (input, _) = take_while1(is_space)(input)?;
let (input, version) = http_version(input)?;
let (input, _) = line_ending(input)?;
Ok((input, Request {method, uri, version}))
}
fn http_version(input: &[u8]) -> IResult<&[u8], &[u8]> {
let (input, _) = tag("HTTP/")(input)?;
let (input, version) = take_while1(is_version)(input)?;
Ok((input, version))
}
fn message_header_value(input: &[u8]) -> IResult<&[u8], &[u8]> {
let (input, _) = take_while1(is_horizontal_space)(input)?;
let (input, data) = take_while1(not_line_ending)(input)?;
let (input, _) = line_ending(input)?;
Ok((input, data))
}
fn message_header(input: &[u8]) -> IResult<&[u8], Header<'_>> {
let (input, name) = take_while1(is_token)(input)?;
let (input, _) = char(':')(input)?;
let (input, value) = many1(message_header_value)(input)?;
Ok((input, Header{ name, value }))
}
fn request(input: &[u8]) -> IResult<&[u8], (Request<'_>, Vec<Header<'_>>)> {
let (input, req) = request_line(input)?;
let (input, h) = many1(message_header)(input)?;
let (input, _) = line_ending(input)?;
Ok((input, (req, h)))
}
fn parse(data: &[u8]) -> Option<Vec<(Request<'_>, Vec<Header<'_>>)>> {
let mut buf = &data[..];
let mut v = Vec::new();
loop {
match request(buf) {
Ok((b, r)) => {
buf = b;
v.push(r);
if b.is_empty() {
//println!("{}", i);
break;
}
}
Err(e) => {
println!("error: {:?}", e);
return None;
},
}
}
Some(v)
}
/*
#[bench]
fn small_test(b: &mut Bencher) {
let data = include_bytes!("../../http-requests.txt");
b.iter(||{
parse(data)
});
}
#[bench]
fn bigger_test(b: &mut Bencher) {
let data = include_bytes!("../../bigger.txt");
b.iter(||{
parse(data)
});
}
*/
fn one_test(c: &mut Criterion) {
let data = &b"GET / HTTP/1.1
Host: www.reddit.com
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:15.0) Gecko/20100101 Firefox/15.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
"[..];
let mut http_group = c.benchmark_group("http");
http_group.throughput(Throughput::Bytes(data.len() as u64));
http_group.bench_with_input(
BenchmarkId::new("parse", data.len()),
data,
|b, data| {
b.iter(|| parse(data).unwrap());
});
http_group.finish();
}
/*
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")).expect("Failed to open file");
let _ = file.read_to_end(&mut contents).unwrap();
}
let buf = &contents[..];
loop {
parse(buf);
}
}
*/
criterion_group!(http, one_test);
criterion_main!(http);