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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
use crate::{
error::Result,
helpers::{ self, BufReadExt, MatchConfig::Trim }
};
use std::{
mem, slice, collections::BTreeMap, iter::FromIterator, ops::Deref,
io::{ BufRead, Write }
};
#[derive(Debug, Clone)]
pub enum RequestTarget {
Wildcard,
Absolute {
path: RequestTargetPath,
query: QueryString
}
}
impl RequestTarget {
pub const fn new_wildcard() -> Self {
Self::Wildcard
}
pub const fn new_absolute(path: RequestTargetPath, query: QueryString) -> Self {
Self::Absolute { path, query }
}
pub fn read<T>(source: &mut T) -> Result<Self> where T: BufRead {
let this = match source.peek_one()? {
Some(b'/') => {
let path = source.read_word("?", [Trim])?;
let query = source.read_all([])?;
Self::Absolute {
path: RequestTargetPath::read(&mut helpers::memreader(path))?,
query: QueryString::read(&mut helpers::memreader(query))?
}
},
Some(b'*') => Self::Wildcard,
first => return Err(einval!("Invalid request target: {:?}", first))
};
Ok(this)
}
pub fn write_all<T>(&self, output: &mut T) -> Result where T: Write {
match self {
Self::Absolute { path, query } => {
path.write_all(output)?;
query.write_all(output)?;
},
Self::Wildcard => write!(output, "*")?,
}
Ok(())
}
}
#[derive(Debug, Clone, Default)]
pub struct RequestTargetPath {
components: Vec<Vec<u8>>
}
impl RequestTargetPath {
pub const fn new() -> Self {
Self { components: Vec::new() }
}
pub fn push<T>(&mut self, component: T) where T: Into<Vec<u8>> {
self.components.push(component.into());
}
pub fn read<T>(source: &mut T) -> Result<Self> where T: BufRead {
let mut this = Self { components: Vec::new() };
let mut component = Vec::new();
while source.peek_one()?.is_some() {
let mut next = 0;
source.read_exact(slice::from_mut(&mut next))?;
match next {
b'/' if component.is_empty() => (),
b'/' => this.components.push(mem::take(&mut component)),
other => component.push(other)
}
}
Ok(this)
}
pub fn write_all<T>(&self, output: &mut T) -> Result where T: Write {
if self.components.is_empty() {
output.write_all(b"/")?;
return Ok(());
}
for component in self.components.iter() {
output.write_all(b"/")?;
output.write_all(component)?;
}
Ok(())
}
}
impl<T> FromIterator<T> for RequestTargetPath where T: Into<Vec<u8>> {
fn from_iter<I: IntoIterator<Item = T>>(components: I) -> Self {
let components = components.into_iter()
.map(|c| c.into())
.collect();
Self { components }
}
}
impl IntoIterator for RequestTargetPath {
type Item = <Vec<Vec<u8>> as IntoIterator>::Item;
type IntoIter = <Vec<Vec<u8>> as IntoIterator>::IntoIter;
fn into_iter(self) -> Self::IntoIter {
self.components.into_iter()
}
}
#[derive(Debug, Clone, Default)]
pub struct QueryString {
fields: BTreeMap<Vec<u8>, Vec<u8>>
}
impl QueryString {
pub fn new() -> Self {
Self { fields: BTreeMap::new() }
}
pub fn get<T>(&self, name: T) -> Option<&[u8]> where T: AsRef<[u8]> {
self.fields.get(name.as_ref()).map(|s| s.as_ref())
}
pub fn set<A, B>(&mut self, name: A, value: B) where A: Into<Vec<u8>>, B: Into<Vec<u8>> {
self.fields.insert(name.into(), value.into());
}
pub fn read<T>(source: &mut T) -> Result<Self> where T: BufRead {
let mut fields = BTreeMap::new();
while source.peek_one()?.is_some() {
let pair = source.read_word("&", [Trim])?;
let mut pair = helpers::memreader(pair);
let key = pair.read_word("=", [Trim])?;
if !key.is_empty() {
let value = pair.read_all([])?;
fields.insert(key, value);
}
}
Ok(Self { fields })
}
pub fn write_all<T>(&self, output: &mut T) -> Result where T: Write {
for (nth_pair, (key, value)) in self.fields.iter().enumerate() {
match nth_pair {
0 => output.write_all(b"?")?,
_ => output.write_all(b"&")?
}
output.write_all(key)?;
if !value.is_empty() {
output.write_all(b"=")?;
output.write_all(value)?;
}
}
Ok(())
}
}
impl Deref for QueryString {
type Target = BTreeMap<Vec<u8>, Vec<u8>>;
fn deref(&self) -> &Self::Target {
&self.fields
}
}
impl<K, V> FromIterator<(K, V)> for QueryString where K: Into<Vec<u8>>, V: Into<Vec<u8>> {
fn from_iter<T: IntoIterator<Item = (K, V)>>(pairs: T) -> Self {
let fields = pairs.into_iter()
.map(|(k, v)| (k.into(), v.into()))
.collect();
Self { fields }
}
}
impl IntoIterator for QueryString {
type Item = <BTreeMap<Vec<u8>, Vec<u8>> as IntoIterator>::Item;
type IntoIter = <BTreeMap<Vec<u8>, Vec<u8>> as IntoIterator>::IntoIter;
fn into_iter(self) -> Self::IntoIter {
self.fields.into_iter()
}
}