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
use liquid_value::Value;
use url::percent_encoding;
use url::percent_encoding::EncodeSet;
use compiler::FilterResult;
use super::{check_args_len, invalid_input};
#[derive(Clone)]
struct UrlEncodeSet(String);
impl UrlEncodeSet {
fn safe_bytes(&self) -> &[u8] {
let &UrlEncodeSet(ref safe) = self;
safe.as_bytes()
}
}
impl EncodeSet for UrlEncodeSet {
fn contains(&self, byte: u8) -> bool {
let is_digit = 48 <= byte && byte <= 57;
let is_upper = 65 <= byte && byte <= 90;
let is_lower = 97 <= byte && byte <= 122;
let is_special = byte == 45 || byte == 46 || byte == 95;
if is_digit || is_upper || is_lower || is_special {
false
} else {
!self.safe_bytes().contains(&byte)
}
}
}
pub fn url_encode(input: &Value, args: &[Value]) -> FilterResult {
check_args_len(args, 0, 0)?;
lazy_static! {
static ref URL_ENCODE_SET: UrlEncodeSet = UrlEncodeSet("".to_owned());
}
let s = input.to_str();
let result: String =
percent_encoding::utf8_percent_encode(s.as_ref(), URL_ENCODE_SET.clone()).collect();
Ok(Value::scalar(result))
}
pub fn url_decode(input: &Value, args: &[Value]) -> FilterResult {
check_args_len(args, 0, 0)?;
let s = input.to_str();
let result = percent_encoding::percent_decode(s.as_bytes())
.decode_utf8()
.map_err(|_| invalid_input("Malformed UTF-8"))?
.into_owned();
Ok(Value::scalar(result))
}
#[cfg(test)]
mod tests {
use super::*;
macro_rules! unit {
($a:ident, $b:expr) => {{
unit!($a, $b, &[])
}};
($a:ident, $b:expr, $c:expr) => {{
$a(&$b, $c).unwrap()
}};
}
macro_rules! tos {
($a:expr) => {{
Value::scalar($a.to_owned())
}};
}
#[test]
fn unit_url_encode() {
assert_eq!(unit!(url_encode, tos!("foo bar")), tos!("foo%20bar"));
assert_eq!(
unit!(url_encode, tos!("foo+1@example.com")),
tos!("foo%2B1%40example.com")
);
}
#[test]
fn unit_url_decode() {
assert_eq!(unit!(url_decode, tos!("foo%20bar")), tos!("foo bar"));
assert_eq!(
unit!(url_decode, tos!("foo%2B1%40example.com")),
tos!("foo+1@example.com")
);
}
}