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
pub mod header;
pub mod request;
pub mod response;
use log::trace;
use percent_encoding::percent_decode;
use std;
#[derive(Clone, PartialEq, Debug)]
pub struct PercentDecoded {
val: String,
}
impl PercentDecoded {
pub(crate) fn new(raw: &str) -> Option<Self> {
match percent_decode(raw.as_bytes()).decode_utf8() {
Ok(pd) => {
trace!(" percent_decode: {}, src: {}", pd, raw);
Some(PercentDecoded {
val: pd.into_owned(),
})
}
Err(_) => {
trace!(" percent_decode: error, src: {}", raw);
None
}
}
}
}
impl AsRef<str> for PercentDecoded {
fn as_ref(&self) -> &str {
&self.val
}
}
fn form_url_decode(raw: &str) -> Result<String, std::str::Utf8Error> {
match percent_decode(raw.replace("+", " ").as_bytes()).decode_utf8() {
Ok(pd) => {
trace!(" form_url_decode: {}, src: {}", pd, raw);
Ok(pd.into_owned())
}
Err(e) => {
trace!(" form_url_decode: error, src: {}", raw);
Err(e)
}
}
}
#[derive(PartialEq, Eq, Hash, Debug)]
pub struct FormUrlDecoded {
val: String,
}
impl FormUrlDecoded {
pub(crate) fn new(raw: &str) -> Option<Self> {
match form_url_decode(raw) {
Ok(val) => Some(FormUrlDecoded { val }),
Err(_) => None,
}
}
}
impl AsRef<str> for FormUrlDecoded {
fn as_ref(&self) -> &str {
&self.val
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn ensure_valid_percent_decode() {
let pd = PercentDecoded::new("%41+%42%2B%63%20%64").unwrap();
assert_eq!("A+B+c d", pd.as_ref());
}
#[test]
fn ensure_valid_www_form_url_encoded_value() {
let f = FormUrlDecoded::new("%41+%42%2B%63%20%64").unwrap();
assert_eq!("A B+c d", f.as_ref());
}
}