protocol.rs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. use dioxus_interpreter_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. format!(
  12. r#"
  13. <script>
  14. {INTERPRETER_JS}
  15. let rootname = "{root_name}";
  16. let root = window.document.getElementById(rootname);
  17. if (root != null) {{
  18. window.interpreter = new Interpreter(root);
  19. window.ipc.postMessage(serializeIpcMessage("initialize"));
  20. }}
  21. </script>
  22. "#
  23. )
  24. }
  25. pub(super) fn desktop_handler(
  26. request: &Request<Vec<u8>>,
  27. custom_head: Option<String>,
  28. custom_index: Option<String>,
  29. root_name: &str,
  30. ) -> Result<Response<Cow<'static, [u8]>>> {
  31. // If the request is for the root, we'll serve the index.html file.
  32. if request.uri().path() == "/" {
  33. // If a custom index is provided, just defer to that, expecting the user to know what they're doing.
  34. // we'll look for the closing </body> tag and insert our little module loader there.
  35. let body = match custom_index {
  36. Some(custom_index) => custom_index
  37. .replace("</body>", &format!("{}</body>", module_loader(root_name)))
  38. .into_bytes(),
  39. None => {
  40. // Otherwise, we'll serve the default index.html and apply a custom head if that's specified.
  41. let mut template = include_str!("./index.html").to_string();
  42. if let Some(custom_head) = custom_head {
  43. template = template.replace("<!-- CUSTOM HEAD -->", &custom_head);
  44. }
  45. template
  46. .replace("<!-- MODULE LOADER -->", &module_loader(root_name))
  47. .into_bytes()
  48. }
  49. };
  50. return Response::builder()
  51. .header("Content-Type", "text/html")
  52. .body(Cow::from(body))
  53. .map_err(From::from);
  54. }
  55. // Else, try to serve a file from the filesystem.
  56. let path = PathBuf::from(request.uri().path().trim_start_matches('/'));
  57. // If the path is relative, we'll try to serve it from the assets directory.
  58. let mut asset = get_asset_root()
  59. .unwrap_or_else(|| Path::new(".").to_path_buf())
  60. .join(&path);
  61. if !asset.exists() {
  62. asset = PathBuf::from("/").join(path);
  63. }
  64. if asset.exists() {
  65. return Response::builder()
  66. .header("Content-Type", get_mime_from_path(&asset)?)
  67. .body(Cow::from(std::fs::read(asset)?))
  68. .map_err(From::from);
  69. }
  70. Response::builder()
  71. .status(StatusCode::NOT_FOUND)
  72. .body(Cow::from(String::from("Not Found").into_bytes()))
  73. .map_err(From::from)
  74. }
  75. #[allow(unreachable_code)]
  76. fn get_asset_root() -> Option<PathBuf> {
  77. /*
  78. We're matching exactly how cargo-bundle works.
  79. - [x] macOS
  80. - [ ] Windows
  81. - [ ] Linux (rpm)
  82. - [ ] Linux (deb)
  83. - [ ] iOS
  84. - [ ] Android
  85. */
  86. if std::env::var_os("CARGO").is_some() {
  87. return None;
  88. }
  89. // TODO: support for other platforms
  90. #[cfg(target_os = "macos")]
  91. {
  92. let bundle = core_foundation::bundle::CFBundle::main_bundle();
  93. let bundle_path = bundle.path()?;
  94. let resources_path = bundle.resources_path()?;
  95. let absolute_resources_root = bundle_path.join(resources_path);
  96. let canonical_resources_root = dunce::canonicalize(absolute_resources_root).ok()?;
  97. return Some(canonical_resources_root);
  98. }
  99. None
  100. }
  101. /// Get the mime type from a path-like string
  102. fn get_mime_from_path(trimmed: &Path) -> Result<&'static str> {
  103. if trimmed.ends_with(".svg") {
  104. return Ok("image/svg+xml");
  105. }
  106. let res = match infer::get_from_path(trimmed)?.map(|f| f.mime_type()) {
  107. Some(t) if t == "text/plain" => get_mime_by_ext(trimmed),
  108. Some(f) => f,
  109. None => get_mime_by_ext(trimmed),
  110. };
  111. Ok(res)
  112. }
  113. /// Get the mime type from a URI using its extension
  114. fn get_mime_by_ext(trimmed: &Path) -> &'static str {
  115. match trimmed.extension().and_then(|e| e.to_str()) {
  116. Some("bin") => "application/octet-stream",
  117. Some("css") => "text/css",
  118. Some("csv") => "text/csv",
  119. Some("html") => "text/html",
  120. Some("ico") => "image/vnd.microsoft.icon",
  121. Some("js") => "text/javascript",
  122. Some("json") => "application/json",
  123. Some("jsonld") => "application/ld+json",
  124. Some("mjs") => "text/javascript",
  125. Some("rtf") => "application/rtf",
  126. Some("svg") => "image/svg+xml",
  127. Some("mp4") => "video/mp4",
  128. // Assume HTML when a TLD is found for eg. `dioxus:://dioxuslabs.app` | `dioxus://hello.com`
  129. Some(_) => "text/html",
  130. // https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
  131. // using octet stream according to this:
  132. None => "application/octet-stream",
  133. }
  134. }