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
#[allow(unused_imports, deprecated)]
use std::ascii::AsciiExt;
use error::ParserErrorRef;
use self::utils::parse_ascii_char;
pub use ::spec::{
Spec,
StrictSpec,
MimeSpec, HttpSpec,
Obs, Modern, Internationalized, Ascii,
ObsNormalSwitch, InternationalizedSwitch,
AnySpec
};
mod utils;
mod impl_spec;
mod parse_cfws;
#[derive(Debug, Clone, PartialEq)]
pub struct ParamIndices {
pub start: usize,
pub eq_idx: usize,
pub end: usize
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct ParseResult<'a> {
pub(crate) input: &'a str,
pub(crate) slash_idx: usize,
pub(crate) end_of_type_idx: usize,
pub(crate) params: Vec<ParamIndices>
}
impl<'a> ParseResult<'a> {
pub(crate) fn repr_len(&self) -> usize {
self.params
.last()
.map(|param| param.end)
.unwrap_or(self.end_of_type_idx)
}
}
pub(crate) fn validate<S: Spec>(input: &str) -> bool {
parse::<S>(input).is_ok()
}
pub(crate) fn parse<'a, S: Spec>(input: &'a str) -> Result<ParseResult, ParserErrorRef<'a>> {
let (slash_idx, end_of_type_idx) = parse_media_type_head::<S>(input)?;
let params = parse_media_type_params::<S>(input, end_of_type_idx)?;
Ok(ParseResult { input, slash_idx, end_of_type_idx, params })
}
fn parse_media_type_head<S: Spec>(input: &str) -> Result<(usize, usize), ParserErrorRef> {
let slash_idx = S::parse_token(input)?;
let start_of_subtype = parse_ascii_char(input, slash_idx, b'/')?;
let end_of_type_idx = at_pos!(start_of_subtype do S::parse_token | input);
Ok((slash_idx, end_of_type_idx))
}
fn parse_media_type_params<S: Spec>(input: &str, offset: usize)
-> Result<Vec<ParamIndices>, ParserErrorRef>
{
let mut out = Vec::new();
let mut offset = offset;
loop {
let sc_idx = at_pos!(offset do S::parse_space | input );
if sc_idx == input.len() { break }
let after_sc_idx = parse_ascii_char(input, sc_idx, b';')?;
let param_name_start = at_pos!(after_sc_idx do S::parse_space | input);
let param_eq_idx = at_pos!(param_name_start do S::parse_token | input);
let param_value_start = parse_ascii_char(input, param_eq_idx, b'=')?;
let param_end_idx;
if input.as_bytes().get(param_value_start) == Some(&b'"') {
param_end_idx = at_pos!(param_value_start do S::parse_quoted_string | input);
} else {
param_end_idx = at_pos!(param_value_start do S::parse_unquoted_value | input);
}
out.push(ParamIndices {
start: param_name_start,
eq_idx: param_eq_idx,
end: param_end_idx
});
offset = param_end_idx;
}
Ok(out)
}
#[cfg(test)]
mod test {
use ::spec::{HttpSpec, Obs};
use super::{parse, ParseResult, ParamIndices};
#[cfg(all(feature="inner-bench", test))]
use super::parse_media_type_head;
#[cfg(all(feature="inner-bench", test))]
use ::test::Bencher;
#[test]
fn parse_charset_utf8() {
let pres: ParseResult = assert_ok!(parse::<HttpSpec<Obs>>("text/plain; charset=utf-8"));
assert_eq!(pres.slash_idx, 4);
assert_eq!(pres.end_of_type_idx, 10);
assert_eq!(pres.params, vec![ParamIndices {
start: 12,
eq_idx: 19,
end: 25
}]);
}
#[cfg(all(feature="inner-bench", test))]
#[bench]
fn parse_head(b: &mut Bencher) {
let raw = "type/subtype";
b.bytes = raw.as_bytes().len() as u64;
b.iter(|| {
parse_media_type_head::<HttpSpec<Obs>>("type/subtype")
})
}
}