浏览代码

refactor: use `strip_prefix` to remove request uri domain, ref #4132

Lucas Nogueira 3 年之前
父节点
当前提交
c479922f9a
共有 3 个文件被更改,包括 12 次插入9 次删除
  1. 6 3
      core/tauri/src/manager.rs
  2. 4 4
      examples/api/src-tauri/Cargo.lock
  3. 2 2
      examples/streaming/main.rs

+ 6 - 3
core/tauri/src/manager.rs

@@ -503,10 +503,11 @@ impl<R: Runtime> WindowManager<R> {
       pending.register_uri_scheme_protocol("asset", move |request| {
         let parsed_path = Url::parse(request.uri())?;
         let filtered_path = &parsed_path[..Position::AfterPath];
+        // safe to unwrap: request.uri() always starts with this prefix
         #[cfg(target_os = "windows")]
-        let path = filtered_path.trim_start_matches("asset://localhost/");
+        let path = filtered_path.strip_prefix("asset://localhost/").unwrap();
         #[cfg(not(target_os = "windows"))]
-        let path = filtered_path.trim_start_matches("asset://");
+        let path = filtered_path.strip_prefix("asset://").unwrap();
         let path = percent_encoding::percent_decode(path.as_bytes())
           .decode_utf8_lossy()
           .to_string();
@@ -829,7 +830,9 @@ impl<R: Runtime> WindowManager<R> {
         // ignore query string and fragment
         .next()
         .unwrap()
-        .trim_start_matches("tauri://localhost")
+        // safe to unwrap: request.uri() always starts with this prefix
+        .strip_prefix("tauri://localhost")
+        .unwrap()
         .to_string();
       let asset = manager.get_asset(path)?;
       let mut builder = HttpResponseBuilder::new()

+ 4 - 4
examples/api/src-tauri/Cargo.lock

@@ -3052,7 +3052,7 @@ dependencies = [
 
 [[package]]
 name = "tauri"
-version = "1.0.0-rc.11"
+version = "1.0.0-rc.12"
 dependencies = [
  "anyhow",
  "attohttpc",
@@ -3113,7 +3113,7 @@ dependencies = [
 
 [[package]]
 name = "tauri-build"
-version = "1.0.0-rc.9"
+version = "1.0.0-rc.10"
 dependencies = [
  "anyhow",
  "cargo_toml",
@@ -3158,7 +3158,7 @@ dependencies = [
 
 [[package]]
 name = "tauri-runtime"
-version = "0.5.1"
+version = "0.6.0"
 dependencies = [
  "gtk",
  "http",
@@ -3175,7 +3175,7 @@ dependencies = [
 
 [[package]]
 name = "tauri-runtime-wry"
-version = "0.5.2"
+version = "0.6.0"
 dependencies = [
  "cocoa",
  "gtk",

+ 2 - 2
examples/streaming/main.rs

@@ -44,9 +44,9 @@ fn main() {
       let mut response = ResponseBuilder::new();
       // get the wanted path
       #[cfg(target_os = "windows")]
-      let path = request.uri().trim_start_matches("stream://localhost/");
+      let path = request.uri().strip_prefix("stream://localhost/").unwrap();
       #[cfg(not(target_os = "windows"))]
-      let path = request.uri().trim_start_matches("stream://");
+      let path = request.uri().strip_prefix("stream://").unwrap();
       let path = percent_encoding::percent_decode(path.as_bytes())
         .decode_utf8_lossy()
         .to_string();