Skip to content

Commit d28ac8a

Browse files
authored
fix(core): enable CORS on the asset protocol, closes #2965 (#2974)
1 parent 76ce9f6 commit d28ac8a

4 files changed

Lines changed: 87 additions & 50 deletions

File tree

.changes/asset-cors.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
Enable CORS on the `asset` protocol.

core/tauri/src/manager.rs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,17 @@ impl<R: Runtime> WindowManager<R> {
303303
registered_scheme_protocols.push("tauri".into());
304304
}
305305
if !registered_scheme_protocols.contains(&"asset".into()) {
306+
let window_url = Url::parse(&pending.url).unwrap();
307+
let window_origin = format!(
308+
"{}://{}{}",
309+
window_url.scheme(),
310+
window_url.host().unwrap(),
311+
if let Some(port) = window_url.port() {
312+
format!(":{}", port)
313+
} else {
314+
"".into()
315+
}
316+
);
306317
pending.register_uri_scheme_protocol("asset", move |request| {
307318
#[cfg(target_os = "windows")]
308319
let path = request.uri().replace("asset://localhost/", "");
@@ -313,11 +324,13 @@ impl<R: Runtime> WindowManager<R> {
313324
.to_string();
314325
let path_for_data = path.clone();
315326

327+
let mut response =
328+
HttpResponseBuilder::new().header("Access-Control-Allow-Origin", &window_origin);
329+
316330
// handle 206 (partial range) http request
317331
if let Some(range) = request.headers().get("range").cloned() {
318332
let mut status_code = 200;
319333
let path_for_data = path_for_data.clone();
320-
let mut response = HttpResponseBuilder::new();
321334
let (headers, status_code, data) = crate::async_runtime::safe_block_on(async move {
322335
let mut headers = HashMap::new();
323336
let mut buf = Vec::new();
@@ -371,10 +384,14 @@ impl<R: Runtime> WindowManager<R> {
371384
}
372385
}
373386

374-
let data =
375-
crate::async_runtime::safe_block_on(async move { tokio::fs::read(path_for_data).await })?;
376-
let mime_type = MimeType::parse(&data, &path);
377-
HttpResponseBuilder::new().mimetype(&mime_type).body(data)
387+
if let Ok(data) =
388+
crate::async_runtime::safe_block_on(async move { tokio::fs::read(path_for_data).await })
389+
{
390+
let mime_type = MimeType::parse(&data, &path);
391+
response.mimetype(&mime_type).body(data)
392+
} else {
393+
response.status(404).body(Vec::new())
394+
}
378395
});
379396
}
380397

@@ -634,6 +651,8 @@ impl<R: Runtime> WindowManager<R> {
634651
_ => unimplemented!(),
635652
};
636653

654+
pending.url = url;
655+
637656
if is_local {
638657
let label = pending.label.clone();
639658
pending = self.prepare_pending_window(pending, &label, pending_labels, app_handle.clone())?;
@@ -644,8 +663,6 @@ impl<R: Runtime> WindowManager<R> {
644663
pending.file_drop_handler = Some(self.prepare_file_drop(app_handle));
645664
}
646665

647-
pending.url = url;
648-
649666
// in `Windows`, we need to force a data_directory
650667
// but we do respect user-specification
651668
#[cfg(target_os = "windows")]

examples/api/src-tauri/Cargo.lock

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

tooling/cli.rs/Cargo.lock

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

0 commit comments

Comments
 (0)