Skip to content

Commit

Permalink
plugin: Plugin exec percent encoding path and query (#773)
Browse files Browse the repository at this point in the history
* spi-plugin: fix: support bool and number type in exec api body entry

* support percentage encode

* spi-object: support check key exists (#771)

---------

Co-authored-by: ZzIsGod1019 <1498852723@qq.com>
  • Loading branch information
4t145 and ZzIsGod1019 authored Jun 13, 2024
1 parent 37ff11e commit e8de93f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion backend/spi/spi-plugin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ tardis = { workspace = true, features = ["reldb-postgres", "web-server"] }
bios-basic = { path = "../../basic", features = ["default"] }
bios-sdk-invoke = { path = "../../../frontend/sdks/invoke", features = ["default"] }
strum = { workspace = true, features = ["derive"] }

percent-encoding = "2"
[dev-dependencies]
tardis = { workspace = true, features = ["test", "ws-client"] }
bios-basic = { path = "../../basic", features = ["default", "test"] }
Expand Down
26 changes: 22 additions & 4 deletions backend/spi/spi-plugin/src/serv/plugin_exec_serv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,32 @@ impl PluginExecServ {

fn build_url(path: &str, query: Option<HashMap<String, String>>, body: Option<Value>, funs: &TardisFunsInst) -> TardisResult<String> {
let mut path = path.to_string();
fn enc(s: &str) -> percent_encoding::PercentEncode<'_> {
percent_encoding::utf8_percent_encode(s, percent_encoding::NON_ALPHANUMERIC)
}
if let Some(query) = query {
let query_str = query.iter().map(|(k, v)| format!("{}={}", k, v)).collect::<Vec<String>>().join("&");
let query_str = query.iter().fold(String::default(), |mut s, (k, v)| {
if k.is_empty() {
return s;
}
if !s.is_empty() {
s.push('&')
}
s.extend(enc(k));
if !v.is_empty() {
s.push('=');
s.extend(enc(v));
}
s
});
if path.ends_with('?') {
path.push_str(&query_str);
} else if path.contains('?') {
path.push_str(&format!("&{}", query_str));
path.push('&');
path.push_str(&query_str);
} else {
path.push_str(&format!("?{}", query_str));
path.push('?');
path.push_str(&query_str);
}
}
if !path.contains(':') {
Expand All @@ -102,7 +120,7 @@ impl PluginExecServ {
match new_r {
Value::Bool(v) => return if *v { "true" } else { "false" }.into(),
Value::Number(v) => return v.to_string().into(),
Value::String(v) => return v.into(),
Value::String(v) => return enc(v).to_string().into(),
// TODO: Support more types: Null, Array, Object
_ => return "".into(),
}
Expand Down

0 comments on commit e8de93f

Please sign in to comment.