protocol.rs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. use crate::{assets::*, edits::EditQueue};
  2. use dioxus_interpreter_js::binary_protocol::SLEDGEHAMMER_JS;
  3. use std::path::{Path, PathBuf};
  4. use wry::{
  5. http::{status::StatusCode, Request, Response},
  6. RequestAsyncResponder, Result,
  7. };
  8. fn handle_edits_code() -> String {
  9. const EDITS_PATH: &str = {
  10. #[cfg(any(target_os = "android", target_os = "windows"))]
  11. {
  12. "http://dioxus.index.html/edits"
  13. }
  14. #[cfg(not(any(target_os = "android", target_os = "windows")))]
  15. {
  16. "dioxus://index.html/edits"
  17. }
  18. };
  19. let prevent_file_upload = r#"// Prevent file inputs from opening the file dialog on click
  20. let inputs = document.querySelectorAll("input");
  21. for (let input of inputs) {
  22. if (!input.getAttribute("data-dioxus-file-listener")) {
  23. // prevent file inputs from opening the file dialog on click
  24. const type = input.getAttribute("type");
  25. if (type === "file") {
  26. input.setAttribute("data-dioxus-file-listener", true);
  27. input.addEventListener("click", (event) => {
  28. let target = event.target;
  29. let target_id = find_real_id(target);
  30. if (target_id !== null) {
  31. const send = (event_name) => {
  32. const message = window.interpreter.serializeIpcMessage("file_diolog", { accept: target.getAttribute("accept"), directory: target.getAttribute("webkitdirectory") === "true", multiple: target.hasAttribute("multiple"), target: parseInt(target_id), bubbles: event_bubbles(event_name), event: event_name });
  33. window.ipc.postMessage(message);
  34. };
  35. send("change&input");
  36. }
  37. event.preventDefault();
  38. });
  39. }
  40. }
  41. }"#;
  42. let polling_request = format!(
  43. r#"// Poll for requests
  44. window.interpreter.wait_for_request = (headless) => {{
  45. fetch(new Request("{EDITS_PATH}"))
  46. .then(response => {{
  47. response.arrayBuffer()
  48. .then(bytes => {{
  49. // In headless mode, the requestAnimationFrame callback is never called, so we need to run the bytes directly
  50. if (headless) {{
  51. run_from_bytes(bytes);
  52. }}
  53. else {{
  54. requestAnimationFrame(() => {{
  55. run_from_bytes(bytes);
  56. }});
  57. }}
  58. window.interpreter.wait_for_request(headless);
  59. }});
  60. }})
  61. }}"#
  62. );
  63. let mut interpreter = SLEDGEHAMMER_JS
  64. .replace("/*POST_HANDLE_EDITS*/", prevent_file_upload)
  65. .replace("export", "")
  66. + &polling_request;
  67. while let Some(import_start) = interpreter.find("import") {
  68. let import_end = interpreter[import_start..]
  69. .find(|c| c == ';' || c == '\n')
  70. .map(|i| i + import_start)
  71. .unwrap_or_else(|| interpreter.len());
  72. interpreter.replace_range(import_start..import_end, "");
  73. }
  74. format!("{interpreter}\nconst config = new InterpreterConfig(true);")
  75. }
  76. static DEFAULT_INDEX: &str = include_str!("./index.html");
  77. /// Build the index.html file we use for bootstrapping a new app
  78. ///
  79. /// We use wry/webview by building a special index.html that forms a bridge between the webview and your rust code
  80. ///
  81. /// This is similar to tauri, except we give more power to your rust code and less power to your frontend code.
  82. /// This lets us skip a build/bundle step - your code just works - but limits how your Rust code can actually
  83. /// mess with UI elements. We make this decision since other renderers like LiveView are very separate and can
  84. /// never properly bridge the gap. Eventually of course, the idea is to build a custom CSS/HTML renderer where you
  85. /// *do* have native control over elements, but that still won't work with liveview.
  86. pub(super) fn index_request(
  87. request: &Request<Vec<u8>>,
  88. custom_head: Option<String>,
  89. custom_index: Option<String>,
  90. root_name: &str,
  91. headless: bool,
  92. ) -> Option<Response<Vec<u8>>> {
  93. // If the request is for the root, we'll serve the index.html file.
  94. if request.uri().path() != "/" {
  95. return None;
  96. }
  97. // Load a custom index file if provided
  98. let mut index = custom_index.unwrap_or_else(|| DEFAULT_INDEX.to_string());
  99. // Insert a custom head if provided
  100. // We look just for the closing head tag. If a user provided a custom index with weird syntax, this might fail
  101. if let Some(head) = custom_head {
  102. index.insert_str(index.find("</head>").expect("Head element to exist"), &head);
  103. }
  104. // Inject our module loader by looking for a body tag
  105. // A failure mode here, obviously, is if the user provided a custom index without a body tag
  106. // Might want to document this
  107. index.insert_str(
  108. index.find("</body>").expect("Body element to exist"),
  109. &module_loader(root_name, headless),
  110. );
  111. Response::builder()
  112. .header("Content-Type", "text/html")
  113. .header("Access-Control-Allow-Origin", "*")
  114. .body(index.into())
  115. .ok()
  116. }
  117. // let assets_head = {
  118. // #[cfg(all(
  119. // debug_assertions,
  120. // any(
  121. // target_os = "windows",
  122. // target_os = "macos",
  123. // target_os = "linux",
  124. // target_os = "dragonfly",
  125. // target_os = "freebsd",
  126. // target_os = "netbsd",
  127. // target_os = "openbsd"
  128. // )
  129. // ))]
  130. // {
  131. // None
  132. // }
  133. // #[cfg(not(all(
  134. // debug_assertions,
  135. // any(
  136. // target_os = "windows",
  137. // target_os = "macos",
  138. // target_os = "linux",
  139. // target_os = "dragonfly",
  140. // target_os = "freebsd",
  141. // target_os = "netbsd",
  142. // target_os = "openbsd"
  143. // )
  144. // )))]
  145. // {
  146. // let head = crate::protocol::get_asset_root_or_default();
  147. // let head = head.join("dist/__assets_head.html");
  148. // match std::fs::read_to_string(&head) {
  149. // Ok(s) => Some(s),
  150. // Err(err) => {
  151. // tracing::error!("Failed to read {head:?}: {err}");
  152. // None
  153. // }
  154. // }
  155. // }
  156. // };
  157. /// Handle a request from the webview
  158. ///
  159. /// - Tries to stream edits if they're requested.
  160. /// - If that doesn't match, tries a user provided asset handler
  161. /// - If that doesn't match, tries to serve a file from the filesystem
  162. pub(super) fn desktop_handler(
  163. request: Request<Vec<u8>>,
  164. asset_handlers: AssetHandlerRegistry,
  165. edit_queue: &EditQueue,
  166. responder: RequestAsyncResponder,
  167. ) {
  168. // If the request is asking for edits (ie binary protocol streaming, do that)
  169. if request.uri().path().trim_matches('/') == "edits" {
  170. return edit_queue.handle_request(responder);
  171. }
  172. // If the user provided a custom asset handler, then call it and return the response if the request was handled.
  173. // The path is the first part of the URI, so we need to trim the leading slash.
  174. let path = PathBuf::from(
  175. urlencoding::decode(request.uri().path().trim_start_matches('/'))
  176. .expect("expected URL to be UTF-8 encoded")
  177. .as_ref(),
  178. );
  179. if path.parent().is_none() {
  180. return tracing::error!("Asset request has no parent {path:?}");
  181. }
  182. if let Some(name) = path.iter().next().unwrap().to_str() {
  183. if asset_handlers.has_handler(name) {
  184. return asset_handlers.handle_request(name, request, responder);
  185. }
  186. }
  187. // Else, try to serve a file from the filesystem.
  188. match serve_from_fs(path) {
  189. Ok(res) => responder.respond(res),
  190. Err(e) => tracing::error!("Error serving request from filesystem {}", e),
  191. }
  192. }
  193. fn serve_from_fs(path: PathBuf) -> Result<Response<Vec<u8>>> {
  194. // If the path is relative, we'll try to serve it from the assets directory.
  195. let mut asset = get_asset_root_or_default().join(&path);
  196. // If we can't find it, make it absolute and try again
  197. if !asset.exists() {
  198. asset = PathBuf::from("/").join(path);
  199. }
  200. if !asset.exists() {
  201. return Ok(Response::builder()
  202. .status(StatusCode::NOT_FOUND)
  203. .body(String::from("Not Found").into_bytes())?);
  204. }
  205. Ok(Response::builder()
  206. .header("Content-Type", get_mime_from_path(&asset)?)
  207. .body(std::fs::read(asset)?)?)
  208. }
  209. /// Construct the inline script that boots up the page and bridges the webview with rust code.
  210. ///
  211. /// The arguments here:
  212. /// - root_name: the root element (by Id) that we stream edits into
  213. /// - headless: is this page being loaded but invisible? Important because not all windows are visible and the
  214. /// interpreter can't connect until the window is ready.
  215. fn module_loader(root_id: &str, headless: bool) -> String {
  216. let js = handle_edits_code();
  217. format!(
  218. r#"
  219. <script type="module">
  220. {js}
  221. // Wait for the page to load
  222. window.onload = function() {{
  223. let rootname = "{root_id}";
  224. let root_element = window.document.getElementById(rootname);
  225. if (root_element != null) {{
  226. window.interpreter.initialize(root_element);
  227. window.ipc.postMessage(window.interpreter.serializeIpcMessage("initialize"));
  228. }}
  229. window.interpreter.wait_for_request({headless});
  230. }}
  231. </script>
  232. "#
  233. )
  234. }
  235. /// Get the asset directory, following tauri/cargo-bundles directory discovery approach
  236. ///
  237. /// Defaults to the current directory if no asset directory is found, which is useful for development when the app
  238. /// isn't bundled.
  239. fn get_asset_root_or_default() -> PathBuf {
  240. get_asset_root().unwrap_or_else(|| Path::new(".").to_path_buf())
  241. }
  242. /// Get the asset directory, following tauri/cargo-bundles directory discovery approach
  243. ///
  244. /// Currently supports:
  245. /// - [x] macOS
  246. /// - [ ] Windows
  247. /// - [ ] Linux (rpm)
  248. /// - [ ] Linux (deb)
  249. /// - [ ] iOS
  250. /// - [ ] Android
  251. #[allow(unreachable_code)]
  252. fn get_asset_root() -> Option<PathBuf> {
  253. // If running under cargo, there's no bundle!
  254. // There might be a smarter/more resilient way of doing this
  255. if std::env::var_os("CARGO").is_some() {
  256. return None;
  257. }
  258. #[cfg(target_os = "macos")]
  259. {
  260. let bundle = core_foundation::bundle::CFBundle::main_bundle();
  261. let bundle_path = bundle.path()?;
  262. let resources_path = bundle.resources_path()?;
  263. let absolute_resources_root = bundle_path.join(resources_path);
  264. return dunce::canonicalize(absolute_resources_root).ok();
  265. }
  266. None
  267. }
  268. /// Get the mime type from a path-like string
  269. fn get_mime_from_path(trimmed: &Path) -> Result<&'static str> {
  270. if trimmed.extension().is_some_and(|ext| ext == "svg") {
  271. return Ok("image/svg+xml");
  272. }
  273. match infer::get_from_path(trimmed)?.map(|f| f.mime_type()) {
  274. Some(f) if f != "text/plain" => Ok(f),
  275. _ => Ok(get_mime_by_ext(trimmed)),
  276. }
  277. }
  278. /// Get the mime type from a URI using its extension
  279. fn get_mime_by_ext(trimmed: &Path) -> &'static str {
  280. match trimmed.extension().and_then(|e| e.to_str()) {
  281. Some("bin") => "application/octet-stream",
  282. Some("css") => "text/css",
  283. Some("csv") => "text/csv",
  284. Some("html") => "text/html",
  285. Some("ico") => "image/vnd.microsoft.icon",
  286. Some("js") => "text/javascript",
  287. Some("json") => "application/json",
  288. Some("jsonld") => "application/ld+json",
  289. Some("mjs") => "text/javascript",
  290. Some("rtf") => "application/rtf",
  291. Some("svg") => "image/svg+xml",
  292. Some("mp4") => "video/mp4",
  293. // Assume HTML when a TLD is found for eg. `dioxus:://dioxuslabs.app` | `dioxus://hello.com`
  294. Some(_) => "text/html",
  295. // https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
  296. // using octet stream according to this:
  297. None => "application/octet-stream",
  298. }
  299. }