Skip to content

Commit

Permalink
fix: skip leading slash for asset protocol, closes #7815 (#7822)
Browse files Browse the repository at this point in the history
Co-authored-by: Lucas Nogueira <[email protected]>
  • Loading branch information
amrbashir and lucasfernog authored Sep 12, 2023
1 parent 100d9ed commit a68ccaf
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changes/tauri-asset-protocol.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'tauri': 'patch:bug'
---

Fix `asset` protocol failing to fetch files.
7 changes: 4 additions & 3 deletions core/tauri/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions core/tauri/src/ipc/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
3 changes: 2 additions & 1 deletion core/tauri/src/protocol/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
4 changes: 2 additions & 2 deletions examples/api/src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions examples/streaming/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit a68ccaf

Please sign in to comment.