Skip to content

Commit a68ccaf

Browse files
fix: skip leading slash for asset protocol, closes #7815 (#7822)
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
1 parent 100d9ed commit a68ccaf

File tree

6 files changed

+17
-11
lines changed

6 files changed

+17
-11
lines changed

.changes/tauri-asset-protocol.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'tauri': 'patch:bug'
3+
---
4+
5+
Fix `asset` protocol failing to fetch files.

core/tauri/src/app.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,8 +1355,8 @@ impl<R: Runtime> Builder<R> {
13551355
/// ```
13561356
/// tauri::Builder::default()
13571357
/// .register_uri_scheme_protocol("app-files", |_app, request| {
1358-
/// let path = request.uri().path().trim_start_matches('/');
1359-
/// if let Ok(data) = std::fs::read(path) {
1358+
/// // skip leading `/`
1359+
/// if let Ok(data) = std::fs::read(&request.uri().path()[1..]) {
13601360
/// http::Response::builder()
13611361
/// .body(data)
13621362
/// .unwrap()
@@ -1397,7 +1397,8 @@ impl<R: Runtime> Builder<R> {
13971397
/// ```
13981398
/// tauri::Builder::default()
13991399
/// .register_asynchronous_uri_scheme_protocol("app-files", |_app, request, responder| {
1400-
/// let path = request.uri().path().trim_start_matches('/').to_string();
1400+
/// // skip leading `/`
1401+
/// let path = request.uri().path()[1..].to_string();
14011402
/// std::thread::spawn(move || {
14021403
/// if let Ok(data) = std::fs::read(path) {
14031404
/// responder.respond(

core/tauri/src/ipc/protocol.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,8 @@ fn parse_invoke_request<R: Runtime>(
291291
#[allow(unused_mut)]
292292
let (parts, mut body) = request.into_parts();
293293

294-
let cmd = parts.uri.path().trim_start_matches('/');
295-
let cmd = percent_encoding::percent_decode(cmd.as_bytes())
294+
// skip leading `/`
295+
let cmd = percent_encoding::percent_decode(parts.uri.path()[1..].as_bytes())
296296
.decode_utf8_lossy()
297297
.to_string();
298298

core/tauri/src/protocol/asset.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ fn get_response(
3434
scope: &FsScope,
3535
window_origin: &str,
3636
) -> Result<Response<Cow<'static, [u8]>>, Box<dyn std::error::Error>> {
37-
let path = percent_encoding::percent_decode(request.uri().path().as_bytes())
37+
// skip leading `/`
38+
let path = percent_encoding::percent_decode(request.uri().path()[1..].as_bytes())
3839
.decode_utf8_lossy()
3940
.to_string();
4041

examples/api/src-tauri/Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/streaming/main.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,8 @@ fn get_stream_response(
7878
request: http::Request<Vec<u8>>,
7979
boundary_id: &Arc<Mutex<i32>>,
8080
) -> Result<http::Response<Vec<u8>>, Box<dyn std::error::Error>> {
81-
// get the file path
82-
let path = request.uri().path();
83-
let path = percent_encoding::percent_decode(path.as_bytes())
81+
// skip leading `/`
82+
let path = percent_encoding::percent_decode(request.uri().path()[1..].as_bytes())
8483
.decode_utf8_lossy()
8584
.to_string();
8685

0 commit comments

Comments
 (0)