Browse Source

fix: skip leading slash for asset protocol, closes #7815 (#7822)

Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
Amr Bashir 1 year ago
parent
commit
a68ccaf59a

+ 5 - 0
.changes/tauri-asset-protocol.md

@@ -0,0 +1,5 @@
+---
+'tauri': 'patch:bug'
+---
+
+Fix `asset` protocol failing to fetch files.

+ 4 - 3
core/tauri/src/app.rs

@@ -1355,8 +1355,8 @@ impl<R: Runtime> Builder<R> {
   /// ```
   /// tauri::Builder::default()
   ///   .register_uri_scheme_protocol("app-files", |_app, request| {
-  ///     let path = request.uri().path().trim_start_matches('/');
-  ///     if let Ok(data) = std::fs::read(path) {
+  ///     // skip leading `/`
+  ///     if let Ok(data) = std::fs::read(&request.uri().path()[1..]) {
   ///       http::Response::builder()
   ///         .body(data)
   ///         .unwrap()
@@ -1397,7 +1397,8 @@ impl<R: Runtime> Builder<R> {
   /// ```
   /// tauri::Builder::default()
   ///   .register_asynchronous_uri_scheme_protocol("app-files", |_app, request, responder| {
-  ///     let path = request.uri().path().trim_start_matches('/').to_string();
+  ///     // skip leading `/`
+  ///     let path = request.uri().path()[1..].to_string();
   ///     std::thread::spawn(move || {
   ///       if let Ok(data) = std::fs::read(path) {
   ///         responder.respond(

+ 2 - 2
core/tauri/src/ipc/protocol.rs

@@ -291,8 +291,8 @@ fn parse_invoke_request<R: Runtime>(
   #[allow(unused_mut)]
   let (parts, mut body) = request.into_parts();
 
-  let cmd = parts.uri.path().trim_start_matches('/');
-  let cmd = percent_encoding::percent_decode(cmd.as_bytes())
+  // skip leading `/`
+  let cmd = percent_encoding::percent_decode(parts.uri.path()[1..].as_bytes())
     .decode_utf8_lossy()
     .to_string();
 

+ 2 - 1
core/tauri/src/protocol/asset.rs

@@ -34,7 +34,8 @@ fn get_response(
   scope: &FsScope,
   window_origin: &str,
 ) -> Result<Response<Cow<'static, [u8]>>, Box<dyn std::error::Error>> {
-  let path = percent_encoding::percent_decode(request.uri().path().as_bytes())
+  // skip leading `/`
+  let path = percent_encoding::percent_decode(request.uri().path()[1..].as_bytes())
     .decode_utf8_lossy()
     .to_string();
 

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

@@ -3399,7 +3399,7 @@ checksum = "9d0e916b1148c8e263850e1ebcbd046f333e0683c724876bb0da63ea4373dc8a"
 
 [[package]]
 name = "tauri"
-version = "2.0.0-alpha.13"
+version = "2.0.0-alpha.14"
 dependencies = [
  "anyhow",
  "bytes",
@@ -3568,7 +3568,7 @@ dependencies = [
 
 [[package]]
 name = "tauri-runtime-wry"
-version = "1.0.0-alpha.1"
+version = "1.0.0-alpha.2"
 dependencies = [
  "cocoa 0.24.1",
  "gtk",

+ 2 - 3
examples/streaming/main.rs

@@ -78,9 +78,8 @@ fn get_stream_response(
   request: http::Request<Vec<u8>>,
   boundary_id: &Arc<Mutex<i32>>,
 ) -> Result<http::Response<Vec<u8>>, Box<dyn std::error::Error>> {
-  // get the file path
-  let path = request.uri().path();
-  let path = percent_encoding::percent_decode(path.as_bytes())
+  // skip leading `/`
+  let path = percent_encoding::percent_decode(request.uri().path()[1..].as_bytes())
     .decode_utf8_lossy()
     .to_string();