From 55c589aa19412cd9682731a900123b5adcbf773d Mon Sep 17 00:00:00 2001 From: Amr Bashir Date: Thu, 7 Mar 2024 03:12:01 +0000 Subject: [PATCH] feat(http): enhance scope config to accept string as well (#1025) Committed via a GitHub action: https://github.com/tauri-apps/plugins-workspace/actions/runs/8182312945 Co-authored-by: lucasfernog --- build.rs | 35 +++++++++++++++++++++++++++++------ src/scope.rs | 17 ++++++++++------- 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/build.rs b/build.rs index 3ad55b4..7811cff 100644 --- a/build.rs +++ b/build.rs @@ -9,8 +9,10 @@ mod scope; const COMMANDS: &[&str] = &["fetch", "fetch_cancel", "fetch_send", "fetch_read_body"]; /// HTTP scope entry object definition. +#[allow(unused)] #[derive(schemars::JsonSchema)] -struct ScopeEntry { +#[serde(untagged)] +enum ScopeEntry { /// A URL that can be accessed by the webview when using the HTTP APIs. /// Wildcards can be used following the URL pattern standard. /// @@ -25,18 +27,39 @@ struct ScopeEntry { /// - "https://*.github.com/tauri-apps/tauri": allows any subdomain of "github.com" with the "tauri-apps/api" path /// /// - "https://myapi.service.com/users/*": allows access to any URLs that begins with "https://myapi.service.com/users/" - url: String, + Value(String), + + Object { + /// A URL that can be accessed by the webview when using the HTTP APIs. + /// Wildcards can be used following the URL pattern standard. + /// + /// See [the URL Pattern spec](https://urlpattern.spec.whatwg.org/) for more information. + /// + /// Examples: + /// + /// - "https://*" : allows all HTTPS origin on port 443 + /// + /// - "https://*:*" : allows all HTTPS origin on any port + /// + /// - "https://*.github.com/tauri-apps/tauri": allows any subdomain of "github.com" with the "tauri-apps/api" path + /// + /// - "https://myapi.service.com/users/*": allows access to any URLs that begins with "https://myapi.service.com/users/" + url: String, + }, } // ensure scope entry is up to date impl From for scope::Entry { fn from(value: ScopeEntry) -> Self { + let url = match value { + ScopeEntry::Value(url) => url, + ScopeEntry::Object { url } => url, + }; + scope::Entry { url: urlpattern::UrlPattern::parse( - urlpattern::UrlPatternInit::parse_constructor_string::( - &value.url, None, - ) - .unwrap(), + urlpattern::UrlPatternInit::parse_constructor_string::(&url, None) + .unwrap(), ) .unwrap(), } diff --git a/src/scope.rs b/src/scope.rs index e04e421..9067f40 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -25,17 +25,20 @@ impl<'de> Deserialize<'de> for Entry { D: Deserializer<'de>, { #[derive(Deserialize)] - struct EntryRaw { - url: String, + #[serde(untagged)] + enum EntryRaw { + Value(String), + Object { url: String }, } EntryRaw::deserialize(deserializer).and_then(|raw| { + let url = match raw { + EntryRaw::Value(url) => url, + EntryRaw::Object { url } => url, + }; Ok(Entry { - url: parse_url_pattern(&raw.url).map_err(|e| { - serde::de::Error::custom(format!( - "`{}` is not a valid URL pattern: {e}", - raw.url - )) + url: parse_url_pattern(&url).map_err(|e| { + serde::de::Error::custom(format!("`{}` is not a valid URL pattern: {e}", url)) })?, }) })