opendal/raw/http_util/
uri.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use crate::*;
19use percent_encoding::percent_decode_str;
20use percent_encoding::utf8_percent_encode;
21use percent_encoding::AsciiSet;
22use percent_encoding::NON_ALPHANUMERIC;
23
24/// Parse http uri invalid error in to opendal::Error.
25pub fn new_http_uri_invalid_error(err: http::uri::InvalidUri) -> Error {
26    Error::new(ErrorKind::Unexpected, "parse http uri").set_source(err)
27}
28
29/// PATH_ENCODE_SET is the encode set for http url path.
30///
31/// This set follows [encodeURIComponent](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent) which will encode all non-ASCII characters except `A-Z a-z 0-9 - _ . ! ~ * ' ( )`
32///
33/// There is a special case for `/` in path: we will allow `/` in path as
34/// required by storage services like s3.
35static PATH_ENCODE_SET: AsciiSet = NON_ALPHANUMERIC
36    .remove(b'/')
37    .remove(b'-')
38    .remove(b'_')
39    .remove(b'.')
40    .remove(b'!')
41    .remove(b'~')
42    .remove(b'*')
43    .remove(b'\'')
44    .remove(b'(')
45    .remove(b')');
46
47/// percent_encode_path will do percent encoding for http encode path.
48///
49/// Follows [encodeURIComponent](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent) which will encode all non-ASCII characters except `A-Z a-z 0-9 - _ . ! ~ * ' ( )`
50///
51/// There is a special case for `/` in path: we will allow `/` in path as
52/// required by storage services like s3.
53pub fn percent_encode_path(path: &str) -> String {
54    utf8_percent_encode(path, &PATH_ENCODE_SET).to_string()
55}
56
57/// percent_decode_path will do percent decoding for http decode path.
58///
59/// If the input is not percent encoded or not valid utf8, return the input.
60pub fn percent_decode_path(path: &str) -> String {
61    match percent_decode_str(path).decode_utf8() {
62        Ok(v) => v.to_string(),
63        Err(_) => path.to_string(),
64    }
65}
66
67/// QueryPairsWriter is used to write query pairs to a url.
68pub struct QueryPairsWriter {
69    base: String,
70    has_query: bool,
71}
72
73impl QueryPairsWriter {
74    /// Create a new QueryPairsWriter with the given base.
75    pub fn new(s: &str) -> Self {
76        // 256 is the average size we observed of a url
77        // in production.
78        //
79        // We eagerly allocate the string to avoid multiple
80        // allocations.
81        let mut base = String::with_capacity(256);
82        base.push_str(s);
83
84        Self {
85            base,
86            has_query: false,
87        }
88    }
89
90    /// Push a new pair of key and value to the url.
91    ///
92    /// The input key and value must already been percent
93    /// encoded correctly.
94    pub fn push(mut self, key: &str, value: &str) -> Self {
95        if self.has_query {
96            self.base.push('&');
97        } else {
98            self.base.push('?');
99            self.has_query = true;
100        }
101
102        // Append the key and value to the base string
103        self.base.push_str(key);
104        if !value.is_empty() {
105            self.base.push('=');
106            self.base.push_str(value);
107        }
108
109        self
110    }
111
112    /// Finish the url and return it.
113    pub fn finish(self) -> String {
114        self.base
115    }
116}
117
118#[cfg(test)]
119mod tests {
120    use super::*;
121
122    #[test]
123    fn test_percent_encode_path() {
124        let cases = vec![
125            (
126                "Reserved Characters",
127                ";,/?:@&=+$",
128                "%3B%2C/%3F%3A%40%26%3D%2B%24",
129            ),
130            ("Unescaped Characters", "-_.!~*'()", "-_.!~*'()"),
131            ("Number Sign", "#", "%23"),
132            (
133                "Alphanumeric Characters + Space",
134                "ABC abc 123",
135                "ABC%20abc%20123",
136            ),
137            (
138                "Unicode",
139                "你好,世界!❤",
140                "%E4%BD%A0%E5%A5%BD%EF%BC%8C%E4%B8%96%E7%95%8C%EF%BC%81%E2%9D%A4",
141            ),
142        ];
143
144        for (name, input, expected) in cases {
145            let actual = percent_encode_path(input);
146
147            assert_eq!(actual, expected, "{name}");
148        }
149    }
150
151    #[test]
152    fn test_percent_decode_path() {
153        let cases = vec![
154            (
155                "Reserved Characters",
156                "%3B%2C/%3F%3A%40%26%3D%2B%24",
157                ";,/?:@&=+$",
158            ),
159            ("Unescaped Characters", "-_.!~*'()", "-_.!~*'()"),
160            ("Number Sign", "%23", "#"),
161            (
162                "Alphanumeric Characters + Space",
163                "ABC%20abc%20123",
164                "ABC abc 123",
165            ),
166            (
167                "Unicode Characters",
168                "%E4%BD%A0%E5%A5%BD%EF%BC%8C%E4%B8%96%E7%95%8C%EF%BC%81%E2%9D%A4",
169                "你好,世界!❤",
170            ),
171            (
172                "Double Encoded Characters",
173                "Double%2520Encoded",
174                "Double%20Encoded",
175            ),
176            (
177                "Not Percent Encoded Characters",
178                "/not percent encoded/path;,/?:@&=+$-",
179                "/not percent encoded/path;,/?:@&=+$-",
180            ),
181        ];
182
183        for (name, input, expected) in cases {
184            let actual = percent_decode_path(input);
185
186            assert_eq!(actual, expected, "{name}");
187        }
188    }
189}