use crate::document::NATIVE_EVAL_JS;
use crate::{assets::*, webview::WebviewEdits};
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";
#[cfg(any(target_os = "android", target_os = "windows"))]
const EVENTS_PATH: &str = "http://dioxus.index.html/__events";
#[cfg(not(any(target_os = "android", target_os = "windows")))]
const EVENTS_PATH: &str = "dioxus://index.html/__events";
static DEFAULT_INDEX: &str = include_str!("./index.html");
#[allow(clippy::too_many_arguments)] pub(super) fn desktop_handler(
request: Request<Vec<u8>>,
asset_handlers: AssetHandlerRegistry,
responder: RequestAsyncResponder,
edit_state: &WebviewEdits,
custom_head: Option<String>,
custom_index: Option<String>,
root_name: &str,
headless: bool,
) {
if let Some(index_bytes) =
index_request(&request, custom_head, custom_index, root_name, headless)
{
return responder.respond(index_bytes);
}
let trimmed_uri = request.uri().path().trim_matches('/');
if trimmed_uri == "__edits" {
return edit_state.wry_queue.handle_request(responder);
}
if trimmed_uri == "__events" {
return edit_state.handle_event(request, responder);
}
if let Some(name) = request.uri().path().split('/').next() {
if asset_handlers.has_handler(name) {
let _name = name.to_string();
return asset_handlers.handle_request(&_name, request, responder);
}
}
match serve_asset(request) {
Ok(res) => responder.respond(res),
Err(_e) => responder.respond(
Response::builder()
.status(StatusCode::INTERNAL_SERVER_ERROR)
.body(String::from("Failed to serve asset").into_bytes())
.unwrap(),
),
}
}
fn serve_asset(request: Request<Vec<u8>>) -> Result<Response<Vec<u8>>> {
let mut uri_path = PathBuf::from(
urlencoding::decode(request.uri().path())
.expect("expected URL to be UTF-8 encoded")
.as_ref(),
);
#[cfg(target_os = "android")]
{
if let Some(asset) = to_java_load_asset(request.uri().path()) {
return Ok(Response::builder()
.header("Content-Type", get_mime_by_ext(&uri_path))
.header("Access-Control-Allow-Origin", "*")
.body(asset)?);
}
}
if !uri_path.exists() || uri_path.starts_with("/assets/") {
let bundle_root = get_asset_root();
let relative_path = uri_path.strip_prefix("/").unwrap();
uri_path = bundle_root.join(relative_path);
}
if uri_path.exists() {
let mime_type = get_mime_from_path(&uri_path);
return Ok(Response::builder()
.header("Content-Type", mime_type?)
.header("Access-Control-Allow-Origin", "*")
.body(std::fs::read(uri_path)?)?);
}
Ok(Response::builder()
.status(StatusCode::NOT_FOUND)
.body(String::from("Not Found").into_bytes())?)
}
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());
if let Some(head) = custom_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 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 native interpreter extends the sledgehammer interpreter with a few extra methods that we use for IPC
window.interpreter = new NativeInterpreter("{EDITS_PATH}", "{EVENTS_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>
<script type="module">
// Include the code for eval
{NATIVE_EVAL_JS}
</script>
"#
)
}
#[allow(unreachable_code)]
fn get_asset_root() -> PathBuf {
let cur_exe = std::env::current_exe().unwrap();
#[cfg(target_os = "macos")]
{
return cur_exe
.parent()
.unwrap()
.parent()
.unwrap()
.join("Resources");
}
cur_exe.parent().unwrap().to_path_buf()
}
fn get_mime_from_path(asset: &Path) -> Result<&'static str> {
if asset.extension().is_some_and(|ext| ext == "svg") {
return Ok("image/svg+xml");
}
match infer::get_from_path(asset)?.map(|f| f.mime_type()) {
Some(f) if f != "text/plain" => Ok(f),
_other => Ok(get_mime_by_ext(asset)),
}
}
fn get_mime_by_ext(trimmed: &Path) -> &'static str {
match trimmed.extension().and_then(|e| e.to_str()) {
Some("js") => "text/javascript; charset=utf-8",
Some("css") => "text/css; charset=utf-8",
Some("json") => "application/json; charset=utf-8",
Some("svg") => "image/svg+xml; charset=utf-8",
Some("html") => "text/html; charset=utf-8",
Some("mjs") => "text/javascript; charset=utf-8",
Some("bin") => "application/octet-stream",
Some("csv") => "text/csv",
Some("ico") => "image/vnd.microsoft.icon",
Some("jsonld") => "application/ld+json",
Some("rtf") => "application/rtf",
Some("mp4") => "video/mp4",
Some(_) => "text/html; charset=utf-8",
None => "application/octet-stream",
}
}
#[cfg(target_os = "android")]
pub(crate) fn to_java_load_asset(filepath: &str) -> Option<Vec<u8>> {
use std::{io::Read, ptr::NonNull};
let ctx = ndk_context::android_context();
let vm = unsafe { jni::JavaVM::from_raw(ctx.vm().cast()) }.unwrap();
let mut env = vm.attach_current_thread().unwrap();
let asset_manager_ptr = env
.call_method(
unsafe { jni::objects::JObject::from_raw(ctx.context().cast()) },
"getAssets",
"()Landroid/content/res/AssetManager;",
&[],
)
.expect("Failed to get asset manager")
.l()
.expect("Failed to get asset manager as object");
unsafe {
let asset_manager =
ndk_sys::AAssetManager_fromJava(env.get_native_interface(), *asset_manager_ptr);
let asset_manager = ndk::asset::AssetManager::from_ptr(
NonNull::new(asset_manager).expect("Invalid asset manager"),
);
let normalized = filepath
.trim_start_matches("/assets/")
.trim_start_matches('/');
let cstr = std::ffi::CString::new(normalized).unwrap();
let mut asset = asset_manager.open(&cstr)?;
Some(asset.buffer().unwrap().to_vec())
}
}