From 24743e44e92def5384b2a8e1f9b5bcac7bfc00ca Mon Sep 17 00:00:00 2001 From: Jonathan Kelley Date: Wed, 23 Feb 2022 13:53:45 -0500 Subject: [PATCH 1/3] feat: canoncialize assets for macOS --- packages/desktop/Cargo.toml | 5 ++++ packages/desktop/src/protocol.rs | 41 ++++++++++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/packages/desktop/Cargo.toml b/packages/desktop/Cargo.toml index 6fd277ba4..f90d00000 100644 --- a/packages/desktop/Cargo.toml +++ b/packages/desktop/Cargo.toml @@ -29,6 +29,11 @@ dioxus-html = { path = "../html", features = ["serialize"], version = "^0.1.6" } webbrowser = "0.5.5" mime_guess = "2.0.3" dioxus-interpreter-js = { path = "../interpreter", version = "^0.0.0" } +dunce = "1.0.2" + + +[target.'cfg(target_os = "macos")'.dependencies] +core-foundation = "0.9.3" [features] diff --git a/packages/desktop/src/protocol.rs b/packages/desktop/src/protocol.rs index c8f007c1d..6cac600f2 100644 --- a/packages/desktop/src/protocol.rs +++ b/packages/desktop/src/protocol.rs @@ -1,4 +1,4 @@ -use std::path::Path; +use std::path::{Path, PathBuf}; use wry::{ http::{status::StatusCode, Request, Response, ResponseBuilder}, Result, @@ -21,8 +21,13 @@ pub(super) fn desktop_handler(request: &Request) -> Result { .mimetype("text/javascript") .body(dioxus_interpreter_js::INTERPRETER_JS.as_bytes().to_vec()) } else { + // the path of the asset specified without any relative paths let path_buf = Path::new(trimmed).canonicalize()?; - let cur_path = Path::new(".").canonicalize()?; + + // the current path of the bundle + let cur_path = get_asset_root() + .unwrap_or_else(|| Path::new(".").to_path_buf()) + .canonicalize()?; if !path_buf.starts_with(cur_path) { return ResponseBuilder::new() @@ -45,3 +50,35 @@ pub(super) fn desktop_handler(request: &Request) -> Result { ResponseBuilder::new().mimetype(&meta).body(data) } } + +#[allow(unreachable_code)] +fn get_asset_root() -> Option { + /* + We're matching exactly how cargo-bundle works. + + - [x] macOS + - [ ] Windows + - [ ] Linux (rpm) + - [ ] Linux (deb) + - [ ] iOS + - [ ] Android + + */ + + if std::env::var_os("CARGO").is_some() { + return None; + } + + // TODO: support for other platforms + #[cfg(target_os = "macos")] + { + let bundle = core_foundation::bundle::CFBundle::main_bundle(); + let bundle_path = bundle.path()?; + let resources_path = bundle.resources_path()?; + let absolute_resources_root = bundle_path.join(resources_path); + let canonical_resources_root = dunce::canonicalize(absolute_resources_root).ok()?; + return Some(canonical_resources_root); + } + + None +} From c12bcd8150371ff7655ffa8b1b1ec80eecd9a777 Mon Sep 17 00:00:00 2001 From: Jonathan Kelley Date: Wed, 23 Feb 2022 14:19:00 -0500 Subject: [PATCH 2/3] fix: protocol works on in both cargo and bundle This commit enables assets to be served locally through cargo run or through a bundle strategy. We use cargo-bundle's bundle strategy. This PR checks only for macOS targets. --- packages/desktop/src/protocol.rs | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/packages/desktop/src/protocol.rs b/packages/desktop/src/protocol.rs index 6cac600f2..b7daba550 100644 --- a/packages/desktop/src/protocol.rs +++ b/packages/desktop/src/protocol.rs @@ -21,30 +21,25 @@ pub(super) fn desktop_handler(request: &Request) -> Result { .mimetype("text/javascript") .body(dioxus_interpreter_js::INTERPRETER_JS.as_bytes().to_vec()) } else { - // the path of the asset specified without any relative paths - let path_buf = Path::new(trimmed).canonicalize()?; + let asset_root = get_asset_root().unwrap_or_else(|| Path::new(".").to_path_buf()); + let asset = asset_root.join(trimmed).canonicalize()?; - // the current path of the bundle - let cur_path = get_asset_root() - .unwrap_or_else(|| Path::new(".").to_path_buf()) - .canonicalize()?; - - if !path_buf.starts_with(cur_path) { + if !asset.starts_with(asset_root) { return ResponseBuilder::new() .status(StatusCode::FORBIDDEN) .body(String::from("Forbidden").into_bytes()); } - if !path_buf.exists() { + if !asset.exists() { return ResponseBuilder::new() .status(StatusCode::NOT_FOUND) .body(String::from("Not Found").into_bytes()); } - let mime = mime_guess::from_path(&path_buf).first_or_octet_stream(); + let mime = mime_guess::from_path(&asset).first_or_octet_stream(); // do not let path searching to go two layers beyond the caller level - let data = std::fs::read(path_buf)?; + let data = std::fs::read(asset)?; let meta = format!("{}", mime); ResponseBuilder::new().mimetype(&meta).body(data) @@ -73,10 +68,11 @@ fn get_asset_root() -> Option { #[cfg(target_os = "macos")] { let bundle = core_foundation::bundle::CFBundle::main_bundle(); - let bundle_path = bundle.path()?; - let resources_path = bundle.resources_path()?; - let absolute_resources_root = bundle_path.join(resources_path); - let canonical_resources_root = dunce::canonicalize(absolute_resources_root).ok()?; + let bundle_path = dbg!(bundle.path()?); + let resources_path = dbg!(bundle.resources_path()?); + let absolute_resources_root = dbg!(bundle_path.join(resources_path)); + let canonical_resources_root = dbg!(dunce::canonicalize(absolute_resources_root).ok()?); + return Some(canonical_resources_root); } From ccb3aa7977c016546bb310ca3f86d26a521ec223 Mon Sep 17 00:00:00 2001 From: Jonathan Kelley Date: Wed, 23 Feb 2022 15:11:57 -0500 Subject: [PATCH 3/3] feat: allow custom root directories --- packages/desktop/Cargo.toml | 3 --- packages/desktop/src/cfg.rs | 12 ++++++++++++ packages/desktop/src/lib.rs | 7 ++++++- packages/desktop/src/protocol.rs | 6 ++++-- 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/packages/desktop/Cargo.toml b/packages/desktop/Cargo.toml index f90d00000..017d399c1 100644 --- a/packages/desktop/Cargo.toml +++ b/packages/desktop/Cargo.toml @@ -30,12 +30,9 @@ webbrowser = "0.5.5" mime_guess = "2.0.3" dioxus-interpreter-js = { path = "../interpreter", version = "^0.0.0" } dunce = "1.0.2" - - [target.'cfg(target_os = "macos")'.dependencies] core-foundation = "0.9.3" - [features] default = ["tokio_runtime"] tokio_runtime = ["tokio"] diff --git a/packages/desktop/src/cfg.rs b/packages/desktop/src/cfg.rs index 161a574b1..6028706bc 100644 --- a/packages/desktop/src/cfg.rs +++ b/packages/desktop/src/cfg.rs @@ -1,3 +1,5 @@ +use std::path::PathBuf; + use wry::application::window::Icon; use wry::{ application::{ @@ -18,6 +20,7 @@ pub struct DesktopConfig { pub(crate) pre_rendered: Option, pub(crate) event_handler: Option>, pub(crate) disable_context_menu: bool, + pub(crate) resource_dir: Option, } pub(crate) type WryProtocol = ( @@ -38,14 +41,23 @@ impl DesktopConfig { file_drop_handler: None, pre_rendered: None, disable_context_menu: !cfg!(debug_assertions), + resource_dir: None, } } + /// set the directory from which assets will be searched in release mode + pub fn with_resource_directory(mut self, path: impl Into) -> Self { + self.resource_dir = Some(path.into()); + self + } + + /// Set whether or not the right-click context menu should be disabled. pub fn with_disable_context_menu(&mut self, disable: bool) -> &mut Self { self.disable_context_menu = disable; self } + /// With pre-rendered HTML content pub fn with_prerendered(&mut self, content: String) -> &mut Self { self.pre_rendered = Some(content); self diff --git a/packages/desktop/src/lib.rs b/packages/desktop/src/lib.rs index 268639597..5ddb0583a 100644 --- a/packages/desktop/src/lib.rs +++ b/packages/desktop/src/lib.rs @@ -122,8 +122,11 @@ pub fn launch_with_props( let (is_ready, sender) = (desktop.is_ready.clone(), desktop.sender.clone()); let proxy = proxy.clone(); + let file_handler = cfg.file_drop_handler.take(); + let resource_dir = cfg.resource_dir.clone(); + let mut webview = WebViewBuilder::new(window) .unwrap() .with_transparent(cfg.window.window.transparent) @@ -160,7 +163,9 @@ pub fn launch_with_props( log::warn!("invalid IPC message received"); }); }) - .with_custom_protocol(String::from("dioxus"), protocol::desktop_handler) + .with_custom_protocol(String::from("dioxus"), move |r| { + protocol::desktop_handler(r, resource_dir.clone()) + }) .with_file_drop_handler(move |window, evet| { file_handler .as_ref() diff --git a/packages/desktop/src/protocol.rs b/packages/desktop/src/protocol.rs index b7daba550..9422ec157 100644 --- a/packages/desktop/src/protocol.rs +++ b/packages/desktop/src/protocol.rs @@ -4,7 +4,7 @@ use wry::{ Result, }; -pub(super) fn desktop_handler(request: &Request) -> Result { +pub(super) fn desktop_handler(request: &Request, asset_root: Option) -> Result { // Any content that uses the `dioxus://` scheme will be shuttled through this handler as a "special case". // For now, we only serve two pieces of content which get included as bytes into the final binary. let path = request.uri().replace("dioxus://", ""); @@ -21,7 +21,9 @@ pub(super) fn desktop_handler(request: &Request) -> Result { .mimetype("text/javascript") .body(dioxus_interpreter_js::INTERPRETER_JS.as_bytes().to_vec()) } else { - let asset_root = get_asset_root().unwrap_or_else(|| Path::new(".").to_path_buf()); + let asset_root = asset_root + .unwrap_or_else(|| get_asset_root().unwrap_or_else(|| Path::new(".").to_path_buf())); + let asset = asset_root.join(trimmed).canonicalize()?; if !asset.starts_with(asset_root) {