protocol.rs 8.7 KB

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