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.
This commit is contained in:
1 parent
24743e44e9
commit
c12bcd8150
1 file changed
+11
-15
@@ -21,30 +21,25 @@ pub(super) fn desktop_handler(request: &Request) -> Result<Response> {
|
|||||||
.mimetype("text/javascript")
|
.mimetype("text/javascript")
|
||||||
.body(dioxus_interpreter_js::INTERPRETER_JS.as_bytes().to_vec())
|
.body(dioxus_interpreter_js::INTERPRETER_JS.as_bytes().to_vec())
|
||||||
} else {
|
} else {
|
||||||
// the path of the asset specified without any relative paths
|
let asset_root = get_asset_root().unwrap_or_else(|| Path::new(".").to_path_buf());
|
||||||
let path_buf = Path::new(trimmed).canonicalize()?;
|
let asset = asset_root.join(trimmed).canonicalize()?;
|
||||||
|
|
||||||
// the current path of the bundle
|
if !asset.starts_with(asset_root) {
|
||||||
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()
|
return ResponseBuilder::new()
|
||||||
.status(StatusCode::FORBIDDEN)
|
.status(StatusCode::FORBIDDEN)
|
||||||
.body(String::from("Forbidden").into_bytes());
|
.body(String::from("Forbidden").into_bytes());
|
||||||
}
|
}
|
||||||
|
|
||||||
if !path_buf.exists() {
|
if !asset.exists() {
|
||||||
return ResponseBuilder::new()
|
return ResponseBuilder::new()
|
||||||
.status(StatusCode::NOT_FOUND)
|
.status(StatusCode::NOT_FOUND)
|
||||||
.body(String::from("Not Found").into_bytes());
|
.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
|
// 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);
|
let meta = format!("{}", mime);
|
||||||
|
|
||||||
ResponseBuilder::new().mimetype(&meta).body(data)
|
ResponseBuilder::new().mimetype(&meta).body(data)
|
||||||
@@ -73,10 +68,11 @@ fn get_asset_root() -> Option<PathBuf> {
|
|||||||
#[cfg(target_os = "macos")]
|
#[cfg(target_os = "macos")]
|
||||||
{
|
{
|
||||||
let bundle = core_foundation::bundle::CFBundle::main_bundle();
|
let bundle = core_foundation::bundle::CFBundle::main_bundle();
|
||||||
let bundle_path = bundle.path()?;
|
let bundle_path = dbg!(bundle.path()?);
|
||||||
let resources_path = bundle.resources_path()?;
|
let resources_path = dbg!(bundle.resources_path()?);
|
||||||
let absolute_resources_root = bundle_path.join(resources_path);
|
let absolute_resources_root = dbg!(bundle_path.join(resources_path));
|
||||||
let canonical_resources_root = dunce::canonicalize(absolute_resources_root).ok()?;
|
let canonical_resources_root = dbg!(dunce::canonicalize(absolute_resources_root).ok()?);
|
||||||
|
|
||||||
return Some(canonical_resources_root);
|
return Some(canonical_resources_root);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user