Parcourir la source

refactor: cleanup logic to get path from protocol req url, closes #4132 (#4192)

Lucas Fernandes Nogueira il y a 3 ans
Parent
commit
43daeafd73
3 fichiers modifiés avec 12 ajouts et 9 suppressions
  1. 7 7
      core/tauri/src/manager.rs
  2. 3 0
      examples/api/src-tauri/Cargo.lock
  3. 2 2
      examples/streaming/main.rs

+ 7 - 7
core/tauri/src/manager.rs

@@ -504,9 +504,9 @@ impl<R: Runtime> WindowManager<R> {
         let parsed_path = Url::parse(request.uri())?;
         let filtered_path = &parsed_path[..Position::AfterPath];
         #[cfg(target_os = "windows")]
-        let path = filtered_path.replace("asset://localhost/", "");
+        let path = filtered_path.trim_start_matches("asset://localhost/");
         #[cfg(not(target_os = "windows"))]
-        let path = filtered_path.replace("asset://", "");
+        let path = filtered_path.trim_start_matches("asset://");
         let path = percent_encoding::percent_decode(path.as_bytes())
           .decode_utf8_lossy()
           .to_string();
@@ -829,8 +829,8 @@ impl<R: Runtime> WindowManager<R> {
         // ignore query string and fragment
         .next()
         .unwrap()
-        .to_string()
-        .replace("tauri://localhost", "");
+        .trim_start_matches("tauri://localhost")
+        .to_string();
       let asset = manager.get_asset(path)?;
       let mut builder = HttpResponseBuilder::new()
         .header("Access-Control-Allow-Origin", &window_origin)
@@ -1366,15 +1366,15 @@ fn on_menu_event<R: Runtime>(window: &Window<R>, event: &MenuEvent) -> crate::Re
 }
 
 #[cfg(feature = "isolation")]
-fn request_to_path(request: &tauri_runtime::http::Request, replace: &str) -> String {
+fn request_to_path(request: &tauri_runtime::http::Request, base_url: &str) -> String {
   let mut path = request
     .uri()
     .split(&['?', '#'][..])
     // ignore query string
     .next()
     .unwrap()
-    .to_string()
-    .replace(replace, "");
+    .trim_start_matches(base_url)
+    .to_string();
 
   if path.ends_with('/') {
     path.pop();

+ 3 - 0
examples/api/src-tauri/Cargo.lock

@@ -2722,6 +2722,9 @@ name = "semver"
 version = "1.0.7"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "d65bd28f48be7196d222d95b9243287f48d27aca604e08497513019ff0502cc4"
+dependencies = [
+ "serde",
+]
 
 [[package]]
 name = "semver-parser"

+ 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().replace("stream://localhost/", "");
+      let path = request.uri().trim_start_matches("stream://localhost/");
       #[cfg(not(target_os = "windows"))]
-      let path = request.uri().replace("stream://", "");
+      let path = request.uri().trim_start_matches("stream://");
       let path = percent_encoding::percent_decode(path.as_bytes())
         .decode_utf8_lossy()
         .to_string();