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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
use std::borrow::Cow;
use std::convert::{Infallible, TryFrom};
use std::error::Error;
use std::fmt::{self, Display, Formatter};
use std::hash::{Hash, Hasher};
use std::ops::Deref;
use std::str;
use crate::utility::{
get_percent_encoded_value, normalize_string, percent_encoded_equality, percent_encoded_hash,
UNRESERVED_CHAR_MAP,
};
#[rustfmt::skip]
const QUERY_CHAR_MAP: [u8; 256] = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, b'!', 0, 0, b'$', b'%', b'&',b'\'', b'(', b')', b'*', b'+', b',', b'-', b'.', b'/',
b'0', b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9', b':', b';', 0, b'=', 0, b'?',
b'@', b'A', b'B', b'C', b'D', b'E', b'F', b'G', b'H', b'I', b'J', b'K', b'L', b'M', b'N', b'O',
b'P', b'Q', b'R', b'S', b'T', b'U', b'V', b'W', b'X', b'Y', b'Z', 0, 0, 0, 0, b'_',
0, b'a', b'b', b'c', b'd', b'e', b'f', b'g', b'h', b'i', b'j', b'k', b'l', b'm', b'n', b'o',
b'p', b'q', b'r', b's', b't', b'u', b'v', b'w', b'x', b'y', b'z', 0, 0, 0, b'~', 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
];
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Query<'query> {
normalized: bool,
query: Cow<'query, str>,
}
impl Query<'_> {
pub fn as_borrowed(&self) -> Query {
use self::Cow::*;
let query = match &self.query {
Borrowed(borrowed) => *borrowed,
Owned(owned) => owned.as_str(),
};
Query {
normalized: self.normalized,
query: Cow::Borrowed(query),
}
}
pub fn as_str(&self) -> &str {
&self.query
}
pub fn into_owned(self) -> Query<'static> {
Query {
normalized: self.normalized,
query: Cow::from(self.query.into_owned()),
}
}
pub fn is_normalized(&self) -> bool {
self.normalized
}
pub fn normalize(&mut self) {
if !self.normalized {
unsafe { normalize_string(&mut self.query.to_mut(), true) };
self.normalized = true;
}
}
}
impl AsRef<[u8]> for Query<'_> {
fn as_ref(&self) -> &[u8] {
self.query.as_bytes()
}
}
impl AsRef<str> for Query<'_> {
fn as_ref(&self) -> &str {
&self.query
}
}
impl Deref for Query<'_> {
type Target = str;
fn deref(&self) -> &Self::Target {
&self.query
}
}
impl Display for Query<'_> {
fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
formatter.write_str(&self.query)
}
}
impl Eq for Query<'_> {}
impl<'query> From<Query<'query>> for String {
fn from(value: Query<'query>) -> Self {
value.to_string()
}
}
impl Hash for Query<'_> {
fn hash<H>(&self, state: &mut H)
where
H: Hasher,
{
percent_encoded_hash(self.query.as_bytes(), state, true);
}
}
impl PartialEq for Query<'_> {
fn eq(&self, other: &Query) -> bool {
*self == *other.as_bytes()
}
}
impl PartialEq<[u8]> for Query<'_> {
fn eq(&self, other: &[u8]) -> bool {
percent_encoded_equality(self.query.as_bytes(), other, true)
}
}
impl<'query> PartialEq<Query<'query>> for [u8] {
fn eq(&self, other: &Query<'query>) -> bool {
other == self
}
}
impl<'a> PartialEq<&'a [u8]> for Query<'_> {
fn eq(&self, other: &&'a [u8]) -> bool {
self == *other
}
}
impl<'a, 'query> PartialEq<Query<'query>> for &'a [u8] {
fn eq(&self, other: &Query<'query>) -> bool {
other == *self
}
}
impl PartialEq<str> for Query<'_> {
fn eq(&self, other: &str) -> bool {
self == other.as_bytes()
}
}
impl<'query> PartialEq<Query<'query>> for str {
fn eq(&self, other: &Query<'query>) -> bool {
other == self.as_bytes()
}
}
impl<'a> PartialEq<&'a str> for Query<'_> {
fn eq(&self, other: &&'a str) -> bool {
self == other.as_bytes()
}
}
impl<'a, 'query> PartialEq<Query<'query>> for &'a str {
fn eq(&self, other: &Query<'query>) -> bool {
other == self.as_bytes()
}
}
impl<'query> TryFrom<&'query [u8]> for Query<'query> {
type Error = QueryError;
fn try_from(value: &'query [u8]) -> Result<Self, Self::Error> {
let (query, rest) = parse_query(value)?;
if rest.is_empty() {
Ok(query)
} else {
Err(QueryError::InvalidCharacter)
}
}
}
impl<'query> TryFrom<&'query str> for Query<'query> {
type Error = QueryError;
fn try_from(value: &'query str) -> Result<Self, Self::Error> {
Query::try_from(value.as_bytes())
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub enum QueryError {
InvalidCharacter,
InvalidPercentEncoding,
}
impl Display for QueryError {
fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
use self::QueryError::*;
match self {
InvalidCharacter => write!(formatter, "invalid query character"),
InvalidPercentEncoding => write!(formatter, "invalid query percent encoding"),
}
}
}
impl Error for QueryError {}
impl From<Infallible> for QueryError {
fn from(_: Infallible) -> Self {
QueryError::InvalidCharacter
}
}
pub(crate) fn parse_query(value: &[u8]) -> Result<(Query, &[u8]), QueryError> {
let mut bytes = value.iter();
let mut end_index = 0;
let mut normalized = true;
while let Some(&byte) = bytes.next() {
match QUERY_CHAR_MAP[byte as usize] {
0 if byte == b'#' => break,
0 => return Err(QueryError::InvalidCharacter),
b'%' => match get_percent_encoded_value(bytes.next().cloned(), bytes.next().cloned()) {
Ok((hex_value, uppercase)) => {
if !uppercase || UNRESERVED_CHAR_MAP[hex_value as usize] != 0 {
normalized = false;
}
end_index += 3;
}
Err(_) => return Err(QueryError::InvalidPercentEncoding),
},
_ => end_index += 1,
}
}
let (value, rest) = value.split_at(end_index);
let query = Query {
normalized,
query: Cow::from(unsafe { str::from_utf8_unchecked(value) }),
};
Ok((query, rest))
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_query_normalize() {
fn test_case(value: &str, expected: &str) {
let mut query = Query::try_from(value).unwrap();
query.normalize();
assert_eq!(query, expected);
}
test_case("", "");
test_case("%ff", "%FF");
test_case("%41", "A");
}
#[test]
fn test_query_parse() {
use self::QueryError::*;
assert_eq!(Query::try_from("").unwrap(), "");
assert_eq!(Query::try_from("query").unwrap(), "query");
assert_eq!(Query::try_from("qUeRy").unwrap(), "qUeRy");
assert_eq!(Query::try_from("%ff%ff%ff%41").unwrap(), "%ff%ff%ff%41");
assert_eq!(Query::try_from(" "), Err(InvalidCharacter));
assert_eq!(Query::try_from("#"), Err(InvalidCharacter));
assert_eq!(Query::try_from("%"), Err(InvalidPercentEncoding));
assert_eq!(Query::try_from("%f"), Err(InvalidPercentEncoding));
assert_eq!(Query::try_from("%zz"), Err(InvalidPercentEncoding));
}
}