#Custom protocol crash on macOS

9 messages · Page 1 of 1 (latest)

buoyant marten
#

Hey
I'm trying to use custom protocol in macOS to load local resources.
However it crash once the request is made

Crash:

mounted request
thread 'main' panicked at src/lib.rs:8:50:
called `Result::unwrap()` on an `Err` value: Os { code: 2, kind: NotFound, message: "No such file or directory" }
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
fatal runtime error: failed to initiate panic, error 5

Code

use std::{fs, str::FromStr};
use tauri::{Url, WebviewWindow};

pub fn run() {
    tauri::Builder::default()
        .register_uri_scheme_protocol("mounted", move |app, request| {
            println!("mounted request");
            let content = fs::read("index.html").unwrap();
            tauri::http::Response::builder()
                .status(200)
                .header("Content-Type", "text/html")
                .body(content)
                .unwrap()
        })
        .setup(|app| {
            WebviewWindow::builder(
                app,
                "main",
                tauri::WebviewUrl::External(Url::from_str("mounted://hello").unwrap()),
            )
            .build()
            .unwrap();
            Ok(())
        })
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}

It only crash if I read the file there. Maybe memory (drop) issues?
Another issue: if the URL doesn't have path eg only mounted:// the custom protocol handler not being called. I think it should act as / path

minor stratus
#

It only crash if I read the file there. Maybe memory (drop) issues?
Well, the error would suggest that the file path is simply wrong. Not sure what the cwd is in dev mode but i'd suggest trying to move the index.html file around a bit, starting in src-tauri/src (next to the main.rs file) and then going up.

Another issue: if the URL doesn't have path eg only mounted:// the custom protocol handler not being called. I think it should act as / path
I think this is a webview behavior we can't change. It's the same as if you'd be trying to navigate to https:// - it's just not a valid url.

buoyant marten
# minor stratus > It only crash if I read the file there. Maybe memory (drop) issues? Well, the ...

You right! the file wasn't there.
However it was a simple example to simplify. but it happens in another project although the file is clearly there, and the read result is ok:

.register_uri_scheme_protocol("local", |app, request| {
      println!("local request");
      let front_dir = FRONTEND_DIR.lock().unwrap();
      let front_dir = front_dir.as_ref().unwrap();
      let mut path = request.uri().path();
      if path == "/" {
          path = "index.html";
      }
      let path = PathBuf::from(front_dir).join(path);
      println!("Absolute path: {:?}, {}", path, path.exists());
      let result = fs::read(path); // comment out fix the crash
      println!("result: {:?}", result); // comment out fix the crash
      // return normal response
      tauri::http::Response::builder().status(200).body(Vec::new()).unwrap()
      
  })
mount frontend
Creating window...
Created an example window
local request
Absolute path: "/Users/user/Documents/tauric/bindings/python/dist/index.html", true
result: Ok([60, 33,
// slient crash

I tried to use drop(result) and print after it and it prints after it

buoyant marten
minor stratus
#

hmm, maybe move your code in front of the tauri::Builder line and hardcode the paths you're trying from the frontend

#

If it still crashes it shouldn't be silent anymore

#

Does an unrelated navigation happen somewhat at the same time that may make it drop the request?

buoyant marten
buoyant marten
# minor stratus hmm, maybe move your code in front of the tauri::Builder line and hardcode the p...

Works

    let path = "/Users/user/Documents/tauric/bindings/python/dist/index.html";
    let content = std::fs::read(path).unwrap(); // read content of file
    let result = Builder::default()
        .register_uri_scheme_protocol("local", move|app, request| {
            println!("local request");
            tauri::http::Response::builder()
            .status(200)
            .body(content.clone())
            .unwrap()
        })

Crash

    let path = "/Users/user/Documents/tauric/bindings/python/dist/index.html";
    
    let result = Builder::default()
        .register_uri_scheme_protocol("local", move|app, request| {
            println!("local request");
            let content = std::fs::read(path).unwrap(); // read content of file
            tauri::http::Response::builder()
            .status(200)
            .body(content.clone())
            .unwrap()
        })

Crash

    let path = "/Users/user/Documents/tauric/bindings/python/dist/index.html";

    let handler = move |app, request| {
        println!("local request");
        let content = std::fs::read(path).unwrap();
        tauri::http::Response::builder()
        .status(200)
        .body(content.clone())
        .unwrap()
    };

    let result = Builder::default()
        .register_uri_scheme_protocol("local", move|app, request| {
            handler(app.clone(), request)
        })

Crash

    let result = Builder::default()
        .register_asynchronous_uri_scheme_protocol("local", move |app, request, responder| {
            println!("local request");
            tauri::async_runtime::spawn(async move {
                let content = std::fs::read(path).unwrap();
                responder.respond(
                    tauri::http::Response::builder()
                    .status(200)
                    .body(Vec::new())
                    .unwrap()
                );
            });

        })