diff options
author | Alex Auvolat <lx@deuxfleurs.fr> | 2025-01-31 18:18:04 +0100 |
---|---|---|
committer | Alex Auvolat <lx@deuxfleurs.fr> | 2025-01-31 18:18:29 +0100 |
commit | 9fa20d45bebab2a3f66b9721c3643dbd607d944d (patch) | |
tree | 4c5cc3dee19f7cbd9e146a90ef5cbddb052716d5 /src/api/common/encoding.rs | |
parent | 9330fd79d3466051394f6d419a247d46da8f5151 (diff) | |
download | garage-9fa20d45bebab2a3f66b9721c3643dbd607d944d.tar.gz garage-9fa20d45bebab2a3f66b9721c3643dbd607d944d.zip |
wip: split garage_api into garage_api_{common,s3,k2v,admin}
Diffstat (limited to 'src/api/common/encoding.rs')
-rw-r--r-- | src/api/common/encoding.rs | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/src/api/common/encoding.rs b/src/api/common/encoding.rs new file mode 100644 index 00000000..e286a784 --- /dev/null +++ b/src/api/common/encoding.rs @@ -0,0 +1,22 @@ +//! Module containing various helpers for encoding + +/// Encode &str for use in a URI +pub fn uri_encode(string: &str, encode_slash: bool) -> String { + let mut result = String::with_capacity(string.len() * 2); + for c in string.chars() { + match c { + 'a'..='z' | 'A'..='Z' | '0'..='9' | '_' | '-' | '~' | '.' => result.push(c), + '/' if encode_slash => result.push_str("%2F"), + '/' if !encode_slash => result.push('/'), + _ => { + result.push_str( + &format!("{}", c) + .bytes() + .map(|b| format!("%{:02X}", b)) + .collect::<String>(), + ); + } + } + } + result +} |