diff --git a/pbs-api-types/Cargo.toml b/pbs-api-types/Cargo.toml index 3bee23ba..cd5dc129 100644 --- a/pbs-api-types/Cargo.toml +++ b/pbs-api-types/Cargo.toml @@ -12,6 +12,7 @@ lazy_static = "1.4" libc = "0.2" nix = "0.19.1" openssl = "0.10" +percent-encoding = "2.1" regex = "1.2" serde = { version = "1.0", features = ["derive"] } diff --git a/pbs-api-types/src/lib.rs b/pbs-api-types/src/lib.rs index bb5d152f..2ecd5170 100644 --- a/pbs-api-types/src/lib.rs +++ b/pbs-api-types/src/lib.rs @@ -4,6 +4,7 @@ use serde::{Deserialize, Serialize}; use anyhow::bail; pub mod common_regex; +pub mod percent_encoding; use proxmox_schema::{ api, const_regex, ApiStringFormat, ApiType, ArraySchema, Schema, StringSchema, ReturnType, diff --git a/pbs-api-types/src/percent_encoding.rs b/pbs-api-types/src/percent_encoding.rs new file mode 100644 index 00000000..afe011e2 --- /dev/null +++ b/pbs-api-types/src/percent_encoding.rs @@ -0,0 +1,22 @@ +use percent_encoding::{utf8_percent_encode, AsciiSet}; + +/// This used to be: `SIMPLE_ENCODE_SET` plus space, `"`, `#`, `<`, `>`, backtick, `?`, `{`, `}` +pub const DEFAULT_ENCODE_SET: &AsciiSet = &percent_encoding::CONTROLS // 0..1f and 7e + // The SIMPLE_ENCODE_SET adds space and anything >= 0x7e (7e itself is already included above) + .add(0x20) + .add(0x7f) + // the DEFAULT_ENCODE_SET added: + .add(b' ') + .add(b'"') + .add(b'#') + .add(b'<') + .add(b'>') + .add(b'`') + .add(b'?') + .add(b'{') + .add(b'}'); + +/// percent encode a url component +pub fn percent_encode_component(comp: &str) -> String { + utf8_percent_encode(comp, percent_encoding::NON_ALPHANUMERIC).to_string() +}