protocol.rs 9.9 KB

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