protocol.rs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. use dioxus_interpreter_js::{COMMON_JS, INTERPRETER_JS};
  2. use std::{
  3. borrow::Cow,
  4. path::{Path, PathBuf},
  5. };
  6. use wry::{
  7. http::{status::StatusCode, Request, Response},
  8. Result,
  9. };
  10. fn module_loader(root_name: &str) -> String {
  11. let js = INTERPRETER_JS.replace(
  12. "/*POST_HANDLE_EDITS*/",
  13. r#"// Prevent file inputs from opening the file dialog on click
  14. let inputs = document.querySelectorAll("input");
  15. for (let input of inputs) {
  16. if (!input.getAttribute("data-dioxus-file-listener")) {
  17. // prevent file inputs from opening the file dialog on click
  18. const type = input.getAttribute("type");
  19. if (type === "file") {
  20. input.setAttribute("data-dioxus-file-listener", true);
  21. input.addEventListener("click", (event) => {
  22. let target = event.target;
  23. let target_id = find_real_id(target);
  24. if (target_id !== null) {
  25. const send = (event_name) => {
  26. const message = 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 });
  27. window.ipc.postMessage(message);
  28. };
  29. send("change&input");
  30. }
  31. event.preventDefault();
  32. });
  33. }
  34. }
  35. }"#,
  36. );
  37. format!(
  38. r#"
  39. <script type="module">
  40. {js}
  41. let rootname = "{root_name}";
  42. let root = window.document.getElementById(rootname);
  43. if (root != null) {{
  44. window.interpreter = new Interpreter(root, new InterpreterConfig(true));
  45. window.ipc.postMessage(serializeIpcMessage("initialize"));
  46. }}
  47. </script>
  48. "#
  49. )
  50. }
  51. #[allow(unused_variables)]
  52. pub(super) fn desktop_handler(
  53. request: &Request<Vec<u8>>,
  54. custom_head: Option<String>,
  55. custom_index: Option<String>,
  56. assets_head: Option<String>,
  57. root_name: &str,
  58. ) -> Result<Response<Cow<'static, [u8]>>> {
  59. // If the request is for the root, we'll serve the index.html file.
  60. if request.uri().path() == "/" {
  61. // If a custom index is provided, just defer to that, expecting the user to know what they're doing.
  62. // we'll look for the closing </body> tag and insert our little module loader there.
  63. let body = match custom_index {
  64. Some(custom_index) => custom_index
  65. .replace("</body>", &format!("{}</body>", module_loader(root_name)))
  66. .into_bytes(),
  67. None => {
  68. // Otherwise, we'll serve the default index.html and apply a custom head if that's specified.
  69. let mut template = include_str!("./index.html").to_string();
  70. #[allow(unused_mut)]
  71. let mut head = custom_head.unwrap_or_default();
  72. #[cfg(debug_assertions)]
  73. {
  74. use assets_cli_support::AssetManifestExt;
  75. let manifest = assets_cli_support::AssetManifest::load();
  76. head += &manifest.head();
  77. }
  78. #[cfg(not(debug_assertions))]
  79. {
  80. if let Some(assets_head) = assets_head {
  81. head += &assets_head;
  82. } else {
  83. log::warn!("No assets head found. You can compile assets with the dioxus-cli in release mode");
  84. }
  85. }
  86. template = template.replace("<!-- CUSTOM HEAD -->", &head);
  87. template
  88. .replace("<!-- MODULE LOADER -->", &module_loader(root_name))
  89. .into_bytes()
  90. }
  91. };
  92. return Response::builder()
  93. .header("Content-Type", "text/html")
  94. .body(Cow::from(body))
  95. .map_err(From::from);
  96. } else if request.uri().path() == "/common.js" {
  97. return Response::builder()
  98. .header("Content-Type", "text/javascript")
  99. .body(Cow::from(COMMON_JS.as_bytes()))
  100. .map_err(From::from);
  101. }
  102. // Else, try to serve a file from the filesystem.
  103. let decoded = urlencoding::decode(request.uri().path().trim_start_matches('/'))
  104. .expect("expected URL to be UTF-8 encoded");
  105. let path = PathBuf::from(&*decoded);
  106. // If the path is relative, we'll try to serve it from the assets directory.
  107. let mut asset = get_asset_root_or_default().join(&path);
  108. if !asset.exists() {
  109. asset = PathBuf::from("/").join(path);
  110. }
  111. if asset.exists() {
  112. return Response::builder()
  113. .header("Content-Type", get_mime_from_path(&asset)?)
  114. .body(Cow::from(std::fs::read(asset)?))
  115. .map_err(From::from);
  116. }
  117. log::error!(
  118. "Failed to find {} (as path {})",
  119. request.uri().path(),
  120. asset.display()
  121. );
  122. Response::builder()
  123. .status(StatusCode::NOT_FOUND)
  124. .body(Cow::from(String::from("Not Found").into_bytes()))
  125. .map_err(From::from)
  126. }
  127. #[allow(unreachable_code)]
  128. pub(crate) fn get_asset_root_or_default() -> PathBuf {
  129. get_asset_root().unwrap_or_else(|| Path::new(".").to_path_buf())
  130. }
  131. #[allow(unreachable_code)]
  132. fn get_asset_root() -> Option<PathBuf> {
  133. /*
  134. We're matching exactly how cargo-bundle works.
  135. - [x] macOS
  136. - [ ] Windows
  137. - [ ] Linux (rpm)
  138. - [ ] Linux (deb)
  139. - [ ] iOS
  140. - [ ] Android
  141. */
  142. if std::env::var_os("CARGO").is_some() {
  143. return None;
  144. }
  145. // TODO: support for other platforms
  146. #[cfg(target_os = "macos")]
  147. {
  148. let bundle = core_foundation::bundle::CFBundle::main_bundle();
  149. let bundle_path = bundle.path()?;
  150. let resources_path = bundle.resources_path()?;
  151. let absolute_resources_root = bundle_path.join(resources_path);
  152. let canonical_resources_root = dunce::canonicalize(absolute_resources_root).ok()?;
  153. return Some(canonical_resources_root);
  154. }
  155. None
  156. }
  157. /// Get the mime type from a path-like string
  158. fn get_mime_from_path(trimmed: &Path) -> Result<&'static str> {
  159. if trimmed.ends_with(".svg") {
  160. return Ok("image/svg+xml");
  161. }
  162. let res = match infer::get_from_path(trimmed)?.map(|f| f.mime_type()) {
  163. Some(f) => {
  164. if f == "text/plain" {
  165. get_mime_by_ext(trimmed)
  166. } else {
  167. f
  168. }
  169. }
  170. None => get_mime_by_ext(trimmed),
  171. };
  172. Ok(res)
  173. }
  174. /// Get the mime type from a URI using its extension
  175. fn get_mime_by_ext(trimmed: &Path) -> &'static str {
  176. match trimmed.extension().and_then(|e| e.to_str()) {
  177. Some("bin") => "application/octet-stream",
  178. Some("css") => "text/css",
  179. Some("csv") => "text/csv",
  180. Some("html") => "text/html",
  181. Some("ico") => "image/vnd.microsoft.icon",
  182. Some("js") => "text/javascript",
  183. Some("json") => "application/json",
  184. Some("jsonld") => "application/ld+json",
  185. Some("mjs") => "text/javascript",
  186. Some("rtf") => "application/rtf",
  187. Some("svg") => "image/svg+xml",
  188. Some("mp4") => "video/mp4",
  189. // Assume HTML when a TLD is found for eg. `dioxus:://dioxuslabs.app` | `dioxus://hello.com`
  190. Some(_) => "text/html",
  191. // https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
  192. // using octet stream according to this:
  193. None => "application/octet-stream",
  194. }
  195. }