aboutsummaryrefslogtreecommitdiff
path: root/src/api/encoding.rs
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2020-04-28 10:18:14 +0000
committerAlex Auvolat <alex@adnab.me>2020-04-28 10:18:14 +0000
commit0957d0fdfadb27e49c24d63994f52197a9c9cd1c (patch)
tree35b048599d44de01650d99ccc2de942498ac4038 /src/api/encoding.rs
parentbe35cbdce2bd4e42a2bfc41ff5b21e332d20eb29 (diff)
downloadgarage-0957d0fdfadb27e49c24d63994f52197a9c9cd1c.tar.gz
garage-0957d0fdfadb27e49c24d63994f52197a9c9cd1c.zip
Work on API
Diffstat (limited to 'src/api/encoding.rs')
-rw-r--r--src/api/encoding.rs33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/api/encoding.rs b/src/api/encoding.rs
new file mode 100644
index 00000000..25999207
--- /dev/null
+++ b/src/api/encoding.rs
@@ -0,0 +1,33 @@
+pub fn xml_escape(s: &str) -> String {
+ s.replace("<", "&lt;")
+ .replace(">", "&gt;")
+ .replace("\"", "&quot;")
+}
+
+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
+}
+
+pub fn xml_encode_key(k: &str, urlencode: bool) -> String {
+ if urlencode {
+ uri_encode(k, true)
+ } else {
+ xml_escape(k)
+ }
+}