use crate::{assets::*, edits::EditQueue};
use dioxus_interpreter_js::unified_bindings::SLEDGEHAMMER_JS;
use dioxus_interpreter_js::NATIVE_JS;
use std::path::{Path, PathBuf};
use wry::{
http::{status::StatusCode, Request, Response},
RequestAsyncResponder, Result,
};
#[cfg(any(target_os = "android", target_os = "windows"))]
const EDITS_PATH: &str = "http://dioxus.index.html/edits";
#[cfg(not(any(target_os = "android", target_os = "windows")))]
const EDITS_PATH: &str = "dioxus://index.html/edits";
static DEFAULT_INDEX: &str = include_str!("./index.html");
pub(super) fn index_request(
request: &Request<Vec<u8>>,
custom_head: Option<String>,
custom_index: Option<String>,
root_name: &str,
headless: bool,
) -> Option<Response<Vec<u8>>> {
if request.uri().path() != "/" {
return None;
}
let mut index = custom_index.unwrap_or_else(|| DEFAULT_INDEX.to_string());
let head = match custom_head {
Some(mut head) => {
if let Some(assets_head) = assets_head() {
head.push_str(&assets_head);
}
Some(head)
}
None => assets_head(),
};
if let Some(head) = head {
index.insert_str(index.find("</head>").expect("Head element to exist"), &head);
}
index.insert_str(
index.find("</body>").expect("Body element to exist"),
&module_loader(root_name, headless),
);
Response::builder()
.header("Content-Type", "text/html")
.header("Access-Control-Allow-Origin", "*")
.body(index.into())
.ok()
}
fn assets_head() -> Option<String> {
#[cfg(any(
target_os = "windows",
target_os = "macos",
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
))]
{
let head = crate::protocol::get_asset_root_or_default();
let head = head.join("dist").join("__assets_head.html");
match std::fs::read_to_string(&head) {
Ok(s) => Some(s),
Err(err) => {
tracing::error!("Failed to read {head:?}: {err}");
None
}
}
}
#[cfg(not(any(
target_os = "windows",
target_os = "macos",
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
)))]
{
None
}
}
pub(super) fn desktop_handler(
request: Request<Vec<u8>>,
asset_handlers: AssetHandlerRegistry,
edit_queue: &EditQueue,
responder: RequestAsyncResponder,
) {
if request.uri().path().trim_matches('/') == "edits" {
return edit_queue.handle_request(responder);
}
let path = PathBuf::from(
urlencoding::decode(request.uri().path().trim_start_matches('/'))
.expect("expected URL to be UTF-8 encoded")
.as_ref(),
);
if path.parent().is_none() {
return tracing::error!("Asset request has no parent {path:?}");
}
if let Some(name) = path.iter().next().unwrap().to_str() {
if asset_handlers.has_handler(name) {
return asset_handlers.handle_request(name, request, responder);
}
}
match serve_from_fs(path) {
Ok(res) => responder.respond(res),
Err(e) => {
tracing::error!("Error serving request from filesystem {}", e);
}
}
}
fn serve_from_fs(path: PathBuf) -> Result<Response<Vec<u8>>> {
let mut asset = get_asset_root_or_default().join(&path);
if !asset.exists() {
asset = PathBuf::from("/").join(&path);
}
if !asset.exists() {
asset = get_asset_root_or_default().join("dist").join(&path);
}
if !asset.exists() {
return Ok(Response::builder()
.status(StatusCode::NOT_FOUND)
.body(String::from("Not Found").into_bytes())?);
}
Ok(Response::builder()
.header("Content-Type", get_mime_from_path(&asset)?)
.body(std::fs::read(asset)?)?)
}
fn module_loader(root_id: &str, headless: bool) -> String {
format!(
r#"
<script type="module">
// Bring the sledgehammer code
{SLEDGEHAMMER_JS}
// And then extend it with our native bindings
{NATIVE_JS}
// The nativeinterprerter extends the sledgehammer interpreter with a few extra methods that we use for IPC
window.interpreter = new NativeInterpreter("{EDITS_PATH}");
// Wait for the page to load before sending the initialize message
window.onload = function() {{
let root_element = window.document.getElementById("{root_id}");
if (root_element != null) {{
window.interpreter.initialize(root_element);
window.ipc.postMessage(window.interpreter.serializeIpcMessage("initialize"));
}}
window.interpreter.waitForRequest({headless});
}}
</script>
"#
)
}
fn get_asset_root_or_default() -> PathBuf {
get_asset_root().unwrap_or_else(|| std::env::current_dir().unwrap())
}
#[allow(unreachable_code)]
fn get_asset_root() -> Option<PathBuf> {
if std::env::var_os("CARGO").is_some() {
return dioxus_cli_config::CURRENT_CONFIG
.as_ref()
.map(|c| c.out_dir())
.ok();
}
#[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);
return dunce::canonicalize(absolute_resources_root).ok();
}
None
}
fn get_mime_from_path(trimmed: &Path) -> Result<&'static str> {
if trimmed.extension().is_some_and(|ext| ext == "svg") {
return Ok("image/svg+xml");
}
match infer::get_from_path(trimmed)?.map(|f| f.mime_type()) {
Some(f) if f != "text/plain" => Ok(f),
_ => Ok(get_mime_by_ext(trimmed)),
}
}
fn get_mime_by_ext(trimmed: &Path) -> &'static str {
match trimmed.extension().and_then(|e| e.to_str()) {
Some("bin") => "application/octet-stream",
Some("css") => "text/css",
Some("csv") => "text/csv",
Some("html") => "text/html",
Some("ico") => "image/vnd.microsoft.icon",
Some("js") => "text/javascript",
Some("json") => "application/json",
Some("jsonld") => "application/ld+json",
Some("mjs") => "text/javascript",
Some("rtf") => "application/rtf",
Some("svg") => "image/svg+xml",
Some("mp4") => "video/mp4",
Some(_) => "text/html",
None => "application/octet-stream",
}
}