fix(core): enable CORS on the asset protocol, closes #2965 (#2974)

This commit is contained in:
Lucas Fernandes Nogueira
2021-12-09 00:56:58 -03:00
committed by GitHub
parent 76ce9f61dd
commit d28ac8aac0
4 changed files with 87 additions and 50 deletions

5
.changes/asset-cors.md Normal file
View File

@@ -0,0 +1,5 @@
---
"tauri": patch
---
Enable CORS on the `asset` protocol.

View File

@@ -303,6 +303,17 @@ impl<R: Runtime> WindowManager<R> {
registered_scheme_protocols.push("tauri".into());
}
if !registered_scheme_protocols.contains(&"asset".into()) {
let window_url = Url::parse(&pending.url).unwrap();
let window_origin = format!(
"{}://{}{}",
window_url.scheme(),
window_url.host().unwrap(),
if let Some(port) = window_url.port() {
format!(":{}", port)
} else {
"".into()
}
);
pending.register_uri_scheme_protocol("asset", move |request| {
#[cfg(target_os = "windows")]
let path = request.uri().replace("asset://localhost/", "");
@@ -313,11 +324,13 @@ impl<R: Runtime> WindowManager<R> {
.to_string();
let path_for_data = path.clone();
let mut response =
HttpResponseBuilder::new().header("Access-Control-Allow-Origin", &window_origin);
// handle 206 (partial range) http request
if let Some(range) = request.headers().get("range").cloned() {
let mut status_code = 200;
let path_for_data = path_for_data.clone();
let mut response = HttpResponseBuilder::new();
let (headers, status_code, data) = crate::async_runtime::safe_block_on(async move {
let mut headers = HashMap::new();
let mut buf = Vec::new();
@@ -371,10 +384,14 @@ impl<R: Runtime> WindowManager<R> {
}
}
let data =
crate::async_runtime::safe_block_on(async move { tokio::fs::read(path_for_data).await })?;
let mime_type = MimeType::parse(&data, &path);
HttpResponseBuilder::new().mimetype(&mime_type).body(data)
if let Ok(data) =
crate::async_runtime::safe_block_on(async move { tokio::fs::read(path_for_data).await })
{
let mime_type = MimeType::parse(&data, &path);
response.mimetype(&mime_type).body(data)
} else {
response.status(404).body(Vec::new())
}
});
}
@@ -634,6 +651,8 @@ impl<R: Runtime> WindowManager<R> {
_ => unimplemented!(),
};
pending.url = url;
if is_local {
let label = pending.label.clone();
pending = self.prepare_pending_window(pending, &label, pending_labels, app_handle.clone())?;
@@ -644,8 +663,6 @@ impl<R: Runtime> WindowManager<R> {
pending.file_drop_handler = Some(self.prepare_file_drop(app_handle));
}
pending.url = url;
// in `Windows`, we need to force a data_directory
// but we do respect user-specification
#[cfg(target_os = "windows")]

View File

@@ -345,33 +345,17 @@ dependencies = [
[[package]]
name = "clap"
version = "3.0.0-beta.5"
version = "3.0.0-rc.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "feff3878564edb93745d58cf63e17b63f24142506e7a20c87a5521ed7bfb1d63"
checksum = "79b70f999da60e6619a29b131739d2211ed4d4301f40372e94a8081422e9d6c7"
dependencies = [
"atty",
"bitflags",
"clap_derive",
"indexmap",
"lazy_static",
"os_str_bytes",
"strsim 0.10.0",
"termcolor",
"textwrap",
"unicase",
]
[[package]]
name = "clap_derive"
version = "3.0.0-beta.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b15c6b4f786ffb6192ffe65a36855bc1fc2444bcd0945ae16748dcd6ed7d0d3"
dependencies = [
"heck",
"proc-macro-error",
"proc-macro2",
"quote",
"syn",
]
[[package]]
@@ -1907,9 +1891,9 @@ dependencies = [
[[package]]
name = "os_str_bytes"
version = "4.2.0"
version = "6.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "addaa943333a514159c80c97ff4a93306530d965d27e139188283cd13e06a799"
checksum = "8e22443d1643a904602595ba1cd8f7d896afe56d26712531c5ff73a15b2fbf64"
dependencies = [
"memchr",
]
@@ -3105,9 +3089,6 @@ name = "textwrap"
version = "0.14.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0066c8d12af8b5acd21e00547c3797fde4e8677254a7ee429176ccebbe93dd80"
dependencies = [
"unicode-width",
]
[[package]]
name = "thin-slice"
@@ -3264,15 +3245,6 @@ version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56dee185309b50d1f11bfedef0fe6d036842e3fb77413abef29f8f8d1c5d4c1c"
[[package]]
name = "unicase"
version = "2.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "50f37be617794602aabbeee0be4f259dc1778fabe05e2d67ee8f79326d5cb4f6"
dependencies = [
"version_check",
]
[[package]]
name = "unicode-bidi"
version = "0.3.7"
@@ -3294,12 +3266,6 @@ version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8895849a949e7845e06bd6dc1aa51731a103c42707010a5b591c0038fb73385b"
[[package]]
name = "unicode-width"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973"
[[package]]
name = "unicode-xid"
version = "0.2.2"

View File

@@ -112,6 +112,15 @@ dependencies = [
"generic-array 0.14.4",
]
[[package]]
name = "block-buffer"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1d36a02058e76b040de25a4464ba1c80935655595b661505c8b39b664828b95"
dependencies = [
"generic-array 0.14.4",
]
[[package]]
name = "block-padding"
version = "0.1.5"
@@ -317,6 +326,15 @@ dependencies = [
"libc",
]
[[package]]
name = "cpufeatures"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95059428f66df56b63431fdb4e1947ed2190586af5c5a8a8b71122bdf5a7f469"
dependencies = [
"libc",
]
[[package]]
name = "crc32fast"
version = "1.2.1"
@@ -370,6 +388,15 @@ dependencies = [
"lazy_static",
]
[[package]]
name = "crypto-common"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "567569e659735adb39ff2d4c20600f7cd78be5471f8c58ab162bce3c03fdbc5f"
dependencies = [
"generic-array 0.14.4",
]
[[package]]
name = "crypto-mac"
version = "0.11.1"
@@ -455,6 +482,17 @@ dependencies = [
"generic-array 0.14.4",
]
[[package]]
name = "digest"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8549e6bfdecd113b7e221fe60b433087f6957387a20f8118ebca9b12af19143d"
dependencies = [
"block-buffer 0.10.0",
"crypto-common",
"generic-array 0.14.4",
]
[[package]]
name = "dirs-next"
version = "2.0.0"
@@ -912,9 +950,9 @@ checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"
[[package]]
name = "libc"
version = "0.2.99"
version = "0.2.109"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a7f823d141fe0a24df1e23b4af4e3c7ba9e5966ec514ea068c93024aa7deb765"
checksum = "f98a04dce437184842841303488f70d0188c5f51437d2a834dc097eafa909a01"
[[package]]
name = "libflate"
@@ -1693,7 +1731,7 @@ dependencies = [
"hmac",
"pbkdf2",
"salsa20",
"sha2",
"sha2 0.9.5",
]
[[package]]
@@ -1826,11 +1864,22 @@ checksum = "b362ae5752fd2137731f9fa25fd4d9058af34666ca1966fb969119cc35719f12"
dependencies = [
"block-buffer 0.9.0",
"cfg-if 1.0.0",
"cpufeatures",
"cpufeatures 0.1.5",
"digest 0.9.0",
"opaque-debug 0.3.0",
]
[[package]]
name = "sha2"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "900d964dd36bb15bcf2f2b35694c072feab74969a54f2bbeec7a2d725d2bdcb6"
dependencies = [
"cfg-if 1.0.0",
"cpufeatures 0.2.1",
"digest 0.10.0",
]
[[package]]
name = "shared_child"
version = "1.0.0"
@@ -1927,7 +1976,7 @@ dependencies = [
"regex",
"serde",
"serde_json",
"sha2",
"sha2 0.10.0",
"strsim",
"tar",
"tempfile",