From 43daeafd73680809d371657dbfde800c5fe1776b Mon Sep 17 00:00:00 2001 From: Lucas Fernandes Nogueira Date: Mon, 23 May 2022 05:37:50 -0700 Subject: [PATCH] refactor: cleanup logic to get path from protocol req url, closes #4132 (#4192) --- core/tauri/src/manager.rs | 14 +++++++------- examples/api/src-tauri/Cargo.lock | 3 +++ examples/streaming/main.rs | 4 ++-- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/core/tauri/src/manager.rs b/core/tauri/src/manager.rs index 2e564fb14..425355074 100644 --- a/core/tauri/src/manager.rs +++ b/core/tauri/src/manager.rs @@ -504,9 +504,9 @@ impl WindowManager { 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 WindowManager { // 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(window: &Window, 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(); diff --git a/examples/api/src-tauri/Cargo.lock b/examples/api/src-tauri/Cargo.lock index b4b943f7f..dbb5baeb1 100644 --- a/examples/api/src-tauri/Cargo.lock +++ b/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" diff --git a/examples/streaming/main.rs b/examples/streaming/main.rs index a2c1cba21..d64d93446 100644 --- a/examples/streaming/main.rs +++ b/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();