From 257465a6d555c544c706a888ed499b3dbf9d8278 Mon Sep 17 00:00:00 2001 From: chaosprint Date: Sat, 6 May 2023 08:13:28 +0200 Subject: [PATCH 1/3] add shared array buffer support for dev server as opt config --- Cargo.lock | 79 ++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 +- src/cli/cfg.rs | 5 +++ src/cli/serve/mod.rs | 1 + src/config.rs | 7 ++++ src/server/mod.rs | 66 ++++++++++++++++++++++++++++++++++-- 6 files changed, 157 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 41d9f8eb5..c806d22fe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -49,6 +49,21 @@ dependencies = [ "memchr", ] +[[package]] +name = "alloc-no-stdlib" +version = "2.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc7bb162ec39d46ab1ca8c77bf72e890535becd1751bb45f64c597edb4c8c6b3" + +[[package]] +name = "alloc-stdlib" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94fb8275041c72129eb51b7d0322c29b8387a0386127718b096429201a5d6ece" +dependencies = [ + "alloc-no-stdlib", +] + [[package]] name = "android_system_properties" version = "0.1.5" @@ -76,6 +91,20 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b" +[[package]] +name = "async-compression" +version = "0.3.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "942c7cd7ae39e91bde4820d74132e9862e62c2f386c3aa90ccf55949f5bad63a" +dependencies = [ + "brotli", + "flate2", + "futures-core", + "memchr", + "pin-project-lite", + "tokio", +] + [[package]] name = "async-trait" version = "0.1.64" @@ -241,6 +270,27 @@ dependencies = [ "generic-array", ] +[[package]] +name = "brotli" +version = "3.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1a0b1dbcc8ae29329621f8d4f0d835787c1c38bb1401979b49d13b0b305ff68" +dependencies = [ + "alloc-no-stdlib", + "alloc-stdlib", + "brotli-decompressor", +] + +[[package]] +name = "brotli-decompressor" +version = "2.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b6561fd3f895a11e8f72af2cb7d22e08366bebc2b6b57f7744c4bda27034744" +dependencies = [ + "alloc-no-stdlib", + "alloc-stdlib", +] + [[package]] name = "bstr" version = "0.2.17" @@ -1521,6 +1571,15 @@ version = "2.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "30e22bd8629359895450b59ea7a776c850561b96a3b1d31321c1949d9e6c9146" +[[package]] +name = "iri-string" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f0f7638c1e223529f1bfdc48c8b133b9e0b434094d1d28473161ee48b235f78" +dependencies = [ + "nom", +] + [[package]] name = "is_executable" version = "0.1.2" @@ -1737,6 +1796,12 @@ dependencies = [ "unicase", ] +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + [[package]] name = "miniz_oxide" version = "0.6.2" @@ -1823,6 +1888,16 @@ dependencies = [ "static_assertions", ] +[[package]] +name = "nom" +version = "7.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" +dependencies = [ + "memchr", + "minimal-lexical", +] + [[package]] name = "notify" version = "5.1.0" @@ -2902,6 +2977,8 @@ version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "aba3f3efabf7fb41fae8534fc20a817013dd1c12cb45441efb6c82e6556b4cd8" dependencies = [ + "async-compression", + "base64 0.13.1", "bitflags", "bytes", "futures-core", @@ -2910,12 +2987,14 @@ dependencies = [ "http-body", "http-range-header", "httpdate", + "iri-string", "mime", "mime_guess", "percent-encoding", "pin-project-lite", "tokio", "tokio-util", + "tower", "tower-layer", "tower-service", "tracing", diff --git a/Cargo.toml b/Cargo.toml index 45539252c..442d4aa7d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -41,7 +41,7 @@ indicatif = "0.17.0-rc.11" subprocess = "0.2.9" axum = { version = "0.5.1", features = ["ws", "headers"] } -tower-http = { version = "0.2.2", features = ["fs", "trace"] } +tower-http = { version = "0.2.2", features = ["full"] } headers = "0.3.7" walkdir = "2" diff --git a/src/cli/cfg.rs b/src/cli/cfg.rs index 9fe453c49..d3a7038ee 100644 --- a/src/cli/cfg.rs +++ b/src/cli/cfg.rs @@ -72,6 +72,11 @@ pub struct ConfigOptsServe { #[serde(default)] pub hot_reload: bool, + /// Build with shared_array_buffer enabled in JS [default: false] + #[clap(long)] + #[serde(default)] + pub shared_array_buffer: bool, + /// Space separated list of features to activate #[clap(long)] pub features: Option>, diff --git a/src/cli/serve/mod.rs b/src/cli/serve/mod.rs index ce0dd743e..1868f9636 100644 --- a/src/cli/serve/mod.rs +++ b/src/cli/serve/mod.rs @@ -20,6 +20,7 @@ impl Serve { // change the relase state. crate_config.with_hot_reload(self.serve.hot_reload); + crate_config.with_shared_array_buffer(self.serve.shared_array_buffer); crate_config.with_release(self.serve.release); crate_config.with_verbose(self.serve.verbose); diff --git a/src/config.rs b/src/config.rs index 171d50a63..4bf5b1a6f 100644 --- a/src/config.rs +++ b/src/config.rs @@ -146,6 +146,7 @@ pub struct CrateConfig { pub dioxus_config: DioxusConfig, pub release: bool, pub hot_reload: bool, + pub shared_array_buffer: bool, pub verbose: bool, pub custom_profile: Option, pub features: Option>, @@ -213,6 +214,7 @@ impl CrateConfig { release, dioxus_config, hot_reload, + shared_array_buffer: false, custom_profile, features, verbose, @@ -234,6 +236,11 @@ impl CrateConfig { self } + pub fn with_shared_array_buffer(&mut self, shared_array_buffer: bool) -> &mut Self { + self.shared_array_buffer = shared_array_buffer; + self + } + pub fn with_verbose(&mut self, verbose: bool) -> &mut Self { self.verbose = verbose; self diff --git a/src/server/mod.rs b/src/server/mod.rs index 42622466b..d33ff8aee 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -2,7 +2,10 @@ use crate::{builder, plugin::PluginManager, serve::Serve, BuildResult, CrateConf use axum::{ body::{Full, HttpBody}, extract::{ws::Message, Extension, TypedHeader, WebSocketUpgrade}, - http::{Response, StatusCode}, + http::{ + header::{HeaderName, HeaderValue}, + Method, Response, StatusCode, + }, response::IntoResponse, routing::{get, get_service}, Router, @@ -22,7 +25,10 @@ use std::{ use tokio::sync::broadcast; use tower::ServiceBuilder; use tower_http::services::fs::{ServeDir, ServeFileSystemResponseBody}; - +use tower_http::{ + cors::{Any, CorsLayer}, + ServiceBuilderExt, +}; mod proxy; pub struct BuildManager { @@ -264,8 +270,35 @@ pub async fn startup_hot_reload(ip: String, port: u16, config: CrateConfig) -> R }, ); + let cors = CorsLayer::new() + // allow `GET` and `POST` when accessing the resource + .allow_methods([Method::GET, Method::POST]) + // allow requests from any origin + .allow_origin(Any) + .allow_headers(Any); + + let (coep, coop) = if config.shared_array_buffer { + ( + HeaderValue::from_static("require-corp"), + HeaderValue::from_static("same-origin"), + ) + } else { + ( + HeaderValue::from_static("unsafe-none"), + HeaderValue::from_static("unsafe-none"), + ) + }; + let file_service_config = config.clone(); let file_service = ServiceBuilder::new() + .override_response_header( + HeaderName::from_static("cross-origin-embedder-policy"), + coep, + ) + .override_response_header( + HeaderName::from_static("cross-origin-opener-policy"), + coop, + ) .and_then( move |response: Response| async move { let response = if file_service_config @@ -316,6 +349,7 @@ pub async fn startup_hot_reload(ip: String, port: u16, config: CrateConfig) -> R let router = router .route("/_dioxus/hot_reload", get(hot_reload_handler)) + .layer(cors) .layer(Extension(ws_reload_state)) .layer(Extension(hot_reload_state)); @@ -409,8 +443,35 @@ pub async fn startup_default(ip: String, port: u16, config: CrateConfig) -> Resu PluginManager::on_serve_start(&config)?; + let cors = CorsLayer::new() + // allow `GET` and `POST` when accessing the resource + .allow_methods([Method::GET, Method::POST]) + // allow requests from any origin + .allow_origin(Any) + .allow_headers(Any); + + let (coep, coop) = if config.shared_array_buffer { + ( + HeaderValue::from_static("require-corp"), + HeaderValue::from_static("same-origin"), + ) + } else { + ( + HeaderValue::from_static("unsafe-none"), + HeaderValue::from_static("unsafe-none"), + ) + }; + let file_service_config = config.clone(); let file_service = ServiceBuilder::new() + .override_response_header( + HeaderName::from_static("cross-origin-embedder-policy"), + coep, + ) + .override_response_header( + HeaderName::from_static("cross-origin-opener-policy"), + coop, + ) .and_then( move |response: Response| async move { let response = if file_service_config @@ -459,6 +520,7 @@ pub async fn startup_default(ip: String, port: u16, config: CrateConfig) -> Resu ) }), ) + .layer(cors) .layer(Extension(ws_reload_state)); axum::Server::bind(&format!("0.0.0.0:{}", port).parse().unwrap()) From b196affb027e7c67151dc1d3c15c007e9111c995 Mon Sep 17 00:00:00 2001 From: YuKun Liu Date: Sat, 6 May 2023 19:37:36 -0700 Subject: [PATCH 2/3] fix: changed flag name to `cross-origin-policy` --- Cargo.lock | 41 ++++++++++++++++------------------------- src/cli/cfg.rs | 5 +++-- src/cli/serve/mod.rs | 2 +- src/config.rs | 8 ++++---- src/server/mod.rs | 18 ++---------------- 5 files changed, 26 insertions(+), 48 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f1814fe6e..863ae5823 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -789,14 +789,14 @@ dependencies = [ "tower-http 0.2.5", "walkdir", "wasm-bindgen-cli-support", - "zip 0.6.4", + "zip 0.6.5", ] [[package]] name = "dioxus-core" -version = "0.3.2" +version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bc602f0ca7030f80fe4abc47cc43ffa75c6e7f48208c2b3f6ae24ec7bc6f6618" +checksum = "4e4d15b0bb9c58d015b2295f240600dd76e427758377569fa33783afc295706a" dependencies = [ "bumpalo", "bumpslab", @@ -1607,15 +1607,6 @@ dependencies = [ "once_cell", ] -[[package]] -name = "iri-string" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f0f7638c1e223529f1bfdc48c8b133b9e0b434094d1d28473161ee48b235f78" -dependencies = [ - "nom", -] - [[package]] name = "is_executable" version = "0.1.2" @@ -1703,9 +1694,9 @@ checksum = "884e2677b40cc8c339eaefcb701c32ef1fd2493d71118dc0ca4b6a736c93bd67" [[package]] name = "libc" -version = "0.2.142" +version = "0.2.143" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a987beff54b60ffa6d51982e1aa1146bc42f19bd26be28b0586f252fccf5317" +checksum = "edc207893e85c5d6be840e969b496b53d94cec8be2d501b214f50daa97fa8024" [[package]] name = "libz-sys" @@ -2632,9 +2623,9 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.160" +version = "1.0.162" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bb2f3770c8bce3bcda7e149193a069a0f4365bda1fa5cd88e03bca26afc1216c" +checksum = "71b2f6e1ab5c2b98c05f0f35b236b22e8df7ead6ffbf51d7808da7f8817e7ab6" dependencies = [ "serde_derive", ] @@ -2651,9 +2642,9 @@ dependencies = [ [[package]] name = "serde_derive" -version = "1.0.160" +version = "1.0.162" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "291a097c63d8497e00160b166a967a4a79c64f3facdd01cbd7502231688d77df" +checksum = "a2a0814352fd64b58489904a44ea8d90cb1a91dcb6b4f5ebabc32c8318e93cb6" dependencies = [ "proc-macro2", "quote", @@ -2919,9 +2910,9 @@ dependencies = [ [[package]] name = "time" -version = "0.3.20" +version = "0.3.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cd0cbfecb4d19b5ea75bb31ad904eb5b9fa13f21079c3b92017ebdf4999a5890" +checksum = "8f3403384eaacbca9923fa06940178ac13e4edb725486d70e8e15881d0c836cc" dependencies = [ "serde", "time-core", @@ -2929,9 +2920,9 @@ dependencies = [ [[package]] name = "time-core" -version = "0.1.0" +version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2e153e1f1acaef8acc537e68b44906d2db6436e2b35ac2c6b42640fff91f00fd" +checksum = "7300fbefb4dadc1af235a9cef3737cea692a9d97e1b9cbcd4ebdae6f8868e6fb" [[package]] name = "tinyvec" @@ -3870,9 +3861,9 @@ dependencies = [ [[package]] name = "zip" -version = "0.6.4" +version = "0.6.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0445d0fbc924bb93539b4316c11afb121ea39296f99a3c4c9edad09e3658cdef" +checksum = "7e92305c174683d78035cbf1b70e18db6329cc0f1b9cae0a52ca90bf5bfe7125" dependencies = [ "aes", "byteorder", @@ -3884,7 +3875,7 @@ dependencies = [ "hmac", "pbkdf2", "sha1", - "time 0.3.20", + "time 0.3.21", "zstd", ] diff --git a/src/cli/cfg.rs b/src/cli/cfg.rs index 8df83707e..cf78e0678 100644 --- a/src/cli/cfg.rs +++ b/src/cli/cfg.rs @@ -77,10 +77,11 @@ pub struct ConfigOptsServe { #[serde(default)] pub hot_reload: bool, - /// Build with shared_array_buffer enabled in JS [default: false] + /// Set cross-origin-policy to same-origin [default: false] + #[clap(name="cross-origin-policy")] #[clap(long)] #[serde(default)] - pub shared_array_buffer: bool, + pub cross_origin_policy: bool, /// Space separated list of features to activate #[clap(long)] diff --git a/src/cli/serve/mod.rs b/src/cli/serve/mod.rs index 29b5f396e..48113c1c1 100644 --- a/src/cli/serve/mod.rs +++ b/src/cli/serve/mod.rs @@ -20,7 +20,7 @@ impl Serve { // change the relase state. crate_config.with_hot_reload(self.serve.hot_reload); - crate_config.with_shared_array_buffer(self.serve.shared_array_buffer); + crate_config.with_cross_origin_policy(self.serve.cross_origin_policy); crate_config.with_release(self.serve.release); crate_config.with_verbose(self.serve.verbose); diff --git a/src/config.rs b/src/config.rs index 4bf5b1a6f..9dc95f739 100644 --- a/src/config.rs +++ b/src/config.rs @@ -146,7 +146,7 @@ pub struct CrateConfig { pub dioxus_config: DioxusConfig, pub release: bool, pub hot_reload: bool, - pub shared_array_buffer: bool, + pub cross_origin_policy: bool, pub verbose: bool, pub custom_profile: Option, pub features: Option>, @@ -214,7 +214,7 @@ impl CrateConfig { release, dioxus_config, hot_reload, - shared_array_buffer: false, + cross_origin_policy: false, custom_profile, features, verbose, @@ -236,8 +236,8 @@ impl CrateConfig { self } - pub fn with_shared_array_buffer(&mut self, shared_array_buffer: bool) -> &mut Self { - self.shared_array_buffer = shared_array_buffer; + pub fn with_cross_origin_policy(&mut self, cross_origin_policy: bool) -> &mut Self { + self.cross_origin_policy = cross_origin_policy; self } diff --git a/src/server/mod.rs b/src/server/mod.rs index 69bb2c6d5..e422032ab 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -176,13 +176,6 @@ pub async fn startup_hot_reload(ip: String, port: u16, config: CrateConfig, star .clone() .unwrap_or_else(|| vec![PathBuf::from("src")]); - let cors = CorsLayer::new() - // allow `GET` and `POST` when accessing the resource - .allow_methods([Method::GET, Method::POST]) - // allow requests from any origin - .allow_origin(Any) - .allow_headers(Any); - let watcher_config = config.clone(); let watcher_ip = ip.clone(); let mut last_update_time = chrono::Local::now().timestamp(); @@ -285,7 +278,7 @@ pub async fn startup_hot_reload(ip: String, port: u16, config: CrateConfig, star .allow_origin(Any) .allow_headers(Any); - let (coep, coop) = if config.shared_array_buffer { + let (coep, coop) = if config.cross_origin_policy { ( HeaderValue::from_static("require-corp"), HeaderValue::from_static("same-origin"), @@ -399,13 +392,6 @@ pub async fn startup_default( let mut last_update_time = chrono::Local::now().timestamp(); - let cors = CorsLayer::new() - // allow `GET` and `POST` when accessing the resource - .allow_methods([Method::GET, Method::POST]) - // allow requests from any origin - .allow_origin(Any) - .allow_headers(Any); - // file watcher: check file change let allow_watch_path = config .dioxus_config @@ -476,7 +462,7 @@ pub async fn startup_default( .allow_origin(Any) .allow_headers(Any); - let (coep, coop) = if config.shared_array_buffer { + let (coep, coop) = if config.cross_origin_policy { ( HeaderValue::from_static("require-corp"), HeaderValue::from_static("same-origin"), From 3fd2eb844c1f76658d67e44d54bb0d35ad551b93 Mon Sep 17 00:00:00 2001 From: YuKun Liu Date: Sun, 7 May 2023 00:40:25 -0700 Subject: [PATCH 3/3] docs: add document --- docs/src/cmd/serve.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/docs/src/cmd/serve.md b/docs/src/cmd/serve.md index 2e24cb3f7..dc2d14593 100644 --- a/docs/src/cmd/serve.md +++ b/docs/src/cmd/serve.md @@ -55,3 +55,17 @@ You can add `--open` flag to open system default browser when server startup. ``` dioxus serve --open ``` + + +## Cross Origin Policy + +use `cross-origin-policy` can change corss-origin header in serverside. + +``` + Cross-Origin-Opener-Policy: same-origin + Cross-Origin-Embedder-Policy: require-corp +``` + +``` +dioxus serve --corss-origin-policy +``` \ No newline at end of file