tauri-apps / wry

Cross-platform WebView library in Rust for Tauri.
Apache License 2.0
3.28k stars 243 forks source link

File uri causes panic in `attach_ipc_handler` #1255

Open andrewbaxter opened 3 weeks ago

andrewbaxter commented 3 weeks ago

Describe the bug I'm using a file:// uri for the webview, and attach an IPC handler. When I run the app it panics at

thread 'main' panicked at /home/.../.cargo/registry/src/index.crates.io-6f17d22bba15001f/wry-0.39.4/src/webkitgtk/mod.rs:531:16:
called `Result::unwrap()` on an `Err` value: http::Error(InvalidUri(InvalidFormat))
stack backtrace:
   0: rust_begin_unwind
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/panicking.rs:645:5
   1: core::panicking::panic_fmt
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/panicking.rs:72:14
   2: core::result::unwrap_failed
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/result.rs:1649:5
   3: core::result::Result<T,E>::unwrap
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/result.rs:1073:23
   4: wry::webkitgtk::InnerWebView::attach_ipc_handler::{{closure}}
             at /home/.../.cargo/registry/src/index.crates.io-6f17d22bba15001f/wry-0.39.4/src/webkitgtk/mod.rs:528:13

which is

            Request::builder()
              .uri(webview.uri().unwrap().to_string())
              .body(js.to_string())
              .unwrap(),

Steps To Reproduce

Expected behavior

  1. I'd expect the request to be built successfully. Seems like an http issue https://github.com/hyperium/http/issues/323
  2. Failing that, I'd be happy with a fake/standin/override url. I'm only using the body of the IPC request (how common is needing the URL?)

Screenshots

Platform and Versions (please complete the following information): OS: Arch Rustc: 1.76.0

Additional context

andrewbaxter commented 3 weeks ago

Looks like they're open to PRs... I'll try to take a look, but one more item in my growing backlog.

amrbashir commented 3 weeks ago

Can you please show a full reproduction example?

andrewbaxter commented 3 weeks ago
use tao::{
    event::{
        Event,
        WindowEvent,
    },
    event_loop::{
        ControlFlow,
        EventLoop,
    },
    platform::unix::WindowExtUnix,
    window::WindowBuilder,
};
use wry::{
    WebViewBuilder,
    WebViewBuilderExtUnix,
};

fn main() -> wry::Result<()> {
    let event_loop = EventLoop::new();
    let window = WindowBuilder::new().build(&event_loop).unwrap();
    let _webview =
        WebViewBuilder::new_gtk(window.default_vbox().unwrap())
            .with_url(format!("file://{}/examples/test.html", env!("CARGO_MANIFEST_DIR")))
            .with_ipc_handler(|_r| { })
            .build()?;
    event_loop.run(move |event, _, control_flow| {
        *control_flow = ControlFlow::Wait;
        if let Event::WindowEvent { event: WindowEvent::CloseRequested, .. } = event {
            *control_flow = ControlFlow::Exit
        }
    });
}

with html

<html>
  <body>
    <script>
      window.ipc.postMessage("");
    </script>
  </body>
</html>
amrbashir commented 3 weeks ago

I guess we need to fix this panic and reduce the number of .unwrap() calls we have.

anyways, iirc file:// is not supported on all webviews and I'd recommend using a custom protocol to serve your asserts, see https://github.com/tauri-apps/wry/blob/dev/examples/custom_protocol.rs example

andrewbaxter commented 3 weeks ago

Yeah, and for error logging... in my case I only need Linux though and even with a custom protocol (can I use file:// as a custom protocol?) I'd want the sorts of uris that cause the panic here. I'm working on a fix for the http issue.