Explorar o código

fix(core): handle requests to `https://tauri.*` on Windows (#4270)

Lucas Fernandes Nogueira %!s(int64=3) %!d(string=hai) anos
pai
achega
74457222b4
Modificáronse 2 ficheiros con 15 adicións e 5 borrados
  1. 5 0
      .changes/fix-url-parsing-windows.md
  2. 10 5
      core/tauri/src/manager.rs

+ 5 - 0
.changes/fix-url-parsing-windows.md

@@ -0,0 +1,5 @@
+---
+"tauri": patch
+---
+
+Fixes a crash when a request is made to `https://tauri.$URL` on Windows where `$URL` is not `localhost/**` e.g. `https://tauri.studio`.

+ 10 - 5
core/tauri/src/manager.rs

@@ -503,9 +503,13 @@ 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.strip_prefix("asset://localhost/").unwrap();
+        let path = filtered_path
+          .strip_prefix("asset://localhost/")
+          // the `strip_prefix` only returns None when a request is made to `https://tauri.$P` on Windows
+          // where `$P` is not `localhost/*`
+          .unwrap_or("");
+        // safe to unwrap: request.uri() always starts with this prefix
         #[cfg(not(target_os = "windows"))]
         let path = filtered_path.strip_prefix("asset://").unwrap();
         let path = percent_encoding::percent_decode(path.as_bytes())
@@ -830,10 +834,11 @@ impl<R: Runtime> WindowManager<R> {
         // ignore query string and fragment
         .next()
         .unwrap()
-        // safe to unwrap: request.uri() always starts with this prefix
         .strip_prefix("tauri://localhost")
-        .unwrap()
-        .to_string();
+        .map(|p| p.to_string())
+        // the `strip_prefix` only returns None when a request is made to `https://tauri.$P` on Windows
+        // where `$P` is not `localhost/*`
+        .unwrap_or_else(|| "".to_string());
       let asset = manager.get_asset(path)?;
       let mut builder = HttpResponseBuilder::new()
         .header("Access-Control-Allow-Origin", &window_origin)