|
4 | 4 |
|
5 | 5 | //! Access Control List types. |
6 | 6 |
|
7 | | -use glob::Pattern; |
8 | 7 | use serde::{Deserialize, Serialize}; |
9 | | -use std::num::NonZeroU64; |
| 8 | +use std::{num::NonZeroU64, str::FromStr, sync::Arc}; |
10 | 9 | use thiserror::Error; |
| 10 | +use url::Url; |
11 | 11 |
|
12 | 12 | use crate::platform::Target; |
13 | 13 |
|
@@ -204,16 +204,60 @@ pub struct PermissionSet { |
204 | 204 | pub permissions: Vec<String>, |
205 | 205 | } |
206 | 206 |
|
| 207 | +/// UrlPattern for [`ExecutionContext::Remote`]. |
| 208 | +#[derive(Debug, Clone)] |
| 209 | +pub struct RemoteUrlPattern(Arc<urlpattern::UrlPattern>, String); |
| 210 | + |
| 211 | +impl FromStr for RemoteUrlPattern { |
| 212 | + type Err = urlpattern::quirks::Error; |
| 213 | + |
| 214 | + fn from_str(s: &str) -> std::result::Result<Self, Self::Err> { |
| 215 | + let init = urlpattern::UrlPatternInit::parse_constructor_string::<regex::Regex>(s, None)?; |
| 216 | + let pattern = urlpattern::UrlPattern::parse(init)?; |
| 217 | + Ok(Self(Arc::new(pattern), s.to_string())) |
| 218 | + } |
| 219 | +} |
| 220 | + |
| 221 | +impl RemoteUrlPattern { |
| 222 | + #[doc(hidden)] |
| 223 | + pub fn as_str(&self) -> &str { |
| 224 | + &self.1 |
| 225 | + } |
| 226 | + |
| 227 | + /// Test if a given URL matches the pattern. |
| 228 | + pub fn test(&self, url: &Url) -> bool { |
| 229 | + self |
| 230 | + .0 |
| 231 | + .test(urlpattern::UrlPatternMatchInput::Url(url.clone())) |
| 232 | + .unwrap_or_default() |
| 233 | + } |
| 234 | +} |
| 235 | + |
| 236 | +impl PartialEq for RemoteUrlPattern { |
| 237 | + fn eq(&self, other: &Self) -> bool { |
| 238 | + self.0.protocol() == other.0.protocol() |
| 239 | + && self.0.username() == other.0.username() |
| 240 | + && self.0.password() == other.0.password() |
| 241 | + && self.0.hostname() == other.0.hostname() |
| 242 | + && self.0.port() == other.0.port() |
| 243 | + && self.0.pathname() == other.0.pathname() |
| 244 | + && self.0.search() == other.0.search() |
| 245 | + && self.0.hash() == other.0.hash() |
| 246 | + } |
| 247 | +} |
| 248 | + |
| 249 | +impl Eq for RemoteUrlPattern {} |
| 250 | + |
207 | 251 | /// Execution context of an IPC call. |
208 | | -#[derive(Debug, Default, Clone, Eq, PartialEq, PartialOrd, Ord, Hash)] |
| 252 | +#[derive(Debug, Default, Clone, Eq, PartialEq)] |
209 | 253 | pub enum ExecutionContext { |
210 | 254 | /// A local URL is used (the Tauri app URL). |
211 | 255 | #[default] |
212 | 256 | Local, |
213 | 257 | /// Remote URL is tring to use the IPC. |
214 | 258 | Remote { |
215 | | - /// The URL trying to access the IPC (glob pattern). |
216 | | - url: Pattern, |
| 259 | + /// The URL trying to access the IPC (URL pattern). |
| 260 | + url: RemoteUrlPattern, |
217 | 261 | }, |
218 | 262 | } |
219 | 263 |
|
|
0 commit comments