aws_smithy_http/
urlencode.rs

1/*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6use percent_encoding::{AsciiSet, CONTROLS};
7
8/// base set of characters that must be URL encoded
9pub(crate) const BASE_SET: &AsciiSet = &CONTROLS
10    .add(b' ')
11    .add(b'/')
12    // RFC-3986 §3.3 allows sub-delims (defined in section2.2) to be in the path component.
13    // This includes both colon ':' and comma ',' characters.
14    // Smithy protocol tests & AWS services percent encode these expected values. Signing
15    // will fail if these values are not percent encoded
16    .add(b':')
17    .add(b',')
18    .add(b'?')
19    .add(b'#')
20    .add(b'[')
21    .add(b']')
22    .add(b'{')
23    .add(b'}')
24    .add(b'|')
25    .add(b'@')
26    .add(b'!')
27    .add(b'$')
28    .add(b'&')
29    .add(b'\'')
30    .add(b'(')
31    .add(b')')
32    .add(b'*')
33    .add(b'+')
34    .add(b';')
35    .add(b'=')
36    .add(b'%')
37    .add(b'<')
38    .add(b'>')
39    .add(b'"')
40    .add(b'^')
41    .add(b'`')
42    .add(b'\\');
43
44#[cfg(test)]
45mod test {
46    use crate::urlencode::BASE_SET;
47    use percent_encoding::utf8_percent_encode;
48
49    #[test]
50    fn set_includes_mandatory_characters() {
51        let chars = ":/?#[]@!$&'()*+,;=%";
52        let escaped = utf8_percent_encode(chars, BASE_SET).to_string();
53        assert_eq!(
54            escaped,
55            "%3A%2F%3F%23%5B%5D%40%21%24%26%27%28%29%2A%2B%2C%3B%3D%25"
56        );
57
58        // sanity check that every character is escaped
59        assert_eq!(escaped.len(), chars.len() * 3);
60    }
61}