Extract protocol hander into module
This commit is contained in:
2 files changed
+51
-43
No files matched your search
@@ -160,50 +160,11 @@ pub fn launch_with_props<P: 'static + Send>(
|
|||||||
}
|
}
|
||||||
_ => (),
|
_ => (),
|
||||||
})
|
})
|
||||||
.with_custom_protocol(String::from("dioxus"), move |request| {
|
.unwrap_or_else(|| {
|
||||||
// Any content that that uses the `dioxus://` scheme will be shuttled through this handler as a "special case"
|
log::warn!("invalid IPC message received");
|
||||||
// For now, we only serve two pieces of content which get included as bytes into the final binary.
|
});
|
||||||
let path = request.uri().replace("dioxus://", "");
|
|
||||||
|
|
||||||
// all assets shouldbe called from index.html
|
|
||||||
let trimmed = path.trim_start_matches("index.html/");
|
|
||||||
|
|
||||||
if trimmed.is_empty() {
|
|
||||||
wry::http::ResponseBuilder::new()
|
|
||||||
.mimetype("text/html")
|
|
||||||
.body(include_bytes!("./index.html").to_vec())
|
|
||||||
} else if trimmed == "index.js" {
|
|
||||||
wry::http::ResponseBuilder::new()
|
|
||||||
.mimetype("text/javascript")
|
|
||||||
.body(dioxus_interpreter_js::INTERPRTER_JS.as_bytes().to_vec())
|
|
||||||
} else {
|
|
||||||
// Read the file content from file path
|
|
||||||
use std::fs::read;
|
|
||||||
|
|
||||||
let path_buf = std::path::Path::new(trimmed).canonicalize()?;
|
|
||||||
let cur_path = std::path::Path::new(".").canonicalize()?;
|
|
||||||
|
|
||||||
if !path_buf.starts_with(cur_path) {
|
|
||||||
return wry::http::ResponseBuilder::new()
|
|
||||||
.status(wry::http::status::StatusCode::FORBIDDEN)
|
|
||||||
.body(String::from("Forbidden").into_bytes());
|
|
||||||
}
|
|
||||||
|
|
||||||
if !path_buf.exists() {
|
|
||||||
return wry::http::ResponseBuilder::new()
|
|
||||||
.status(wry::http::status::StatusCode::NOT_FOUND)
|
|
||||||
.body(String::from("Not Found").into_bytes());
|
|
||||||
}
|
|
||||||
|
|
||||||
let mime = mime_guess::from_path(&path_buf).first_or_octet_stream();
|
|
||||||
|
|
||||||
// do not let path searching to go two layers beyond the caller level
|
|
||||||
let data = read(path_buf)?;
|
|
||||||
let meta = format!("{}", mime);
|
|
||||||
|
|
||||||
wry::http::ResponseBuilder::new().mimetype(&meta).body(data)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
.with_custom_protocol(String::from("dioxus"), protocol::desktop_handler)
|
||||||
.with_file_drop_handler(move |window, evet| {
|
.with_file_drop_handler(move |window, evet| {
|
||||||
file_handler
|
file_handler
|
||||||
.as_ref()
|
.as_ref()
|
||||||
|
|||||||
@@ -0,0 +1,47 @@
|
|||||||
|
use std::path::Path;
|
||||||
|
use wry::{
|
||||||
|
http::{status::StatusCode, Request, Response, ResponseBuilder},
|
||||||
|
Result,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub(super) fn desktop_handler(request: &Request) -> Result<Response> {
|
||||||
|
// 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://", "");
|
||||||
|
|
||||||
|
// all assets should be called from index.html
|
||||||
|
let trimmed = path.trim_start_matches("index.html/");
|
||||||
|
|
||||||
|
if trimmed.is_empty() {
|
||||||
|
ResponseBuilder::new()
|
||||||
|
.mimetype("text/html")
|
||||||
|
.body(include_bytes!("./index.html").to_vec())
|
||||||
|
} else if trimmed == "index.js" {
|
||||||
|
ResponseBuilder::new()
|
||||||
|
.mimetype("text/javascript")
|
||||||
|
.body(dioxus_interpreter_js::INTERPRETER_JS.as_bytes().to_vec())
|
||||||
|
} else {
|
||||||
|
let path_buf = Path::new(trimmed).canonicalize()?;
|
||||||
|
let cur_path = Path::new(".").canonicalize()?;
|
||||||
|
|
||||||
|
if !path_buf.starts_with(cur_path) {
|
||||||
|
return ResponseBuilder::new()
|
||||||
|
.status(StatusCode::FORBIDDEN)
|
||||||
|
.body(String::from("Forbidden").into_bytes());
|
||||||
|
}
|
||||||
|
|
||||||
|
if !path_buf.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();
|
||||||
|
|
||||||
|
// do not let path searching to go two layers beyond the caller level
|
||||||
|
let data = std::fs::read(path_buf)?;
|
||||||
|
let meta = format!("{}", mime);
|
||||||
|
|
||||||
|
ResponseBuilder::new().mimetype(&meta).body(data)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user