diff --git a/bench/tests/src/cpu_intensive.rs b/bench/tests/src/cpu_intensive.rs index ea147b9a6..7fb597f22 100644 --- a/bench/tests/src/cpu_intensive.rs +++ b/bench/tests/src/cpu_intensive.rs @@ -25,10 +25,10 @@ fn main() -> wry::Result<()> { }; let webview = WebViewBuilder::new(window) .unwrap() - .with_custom_protocol("wry.bench".into(), move |request| { + .with_custom_protocol("wrybench".into(), move |request| { let path = request.uri().to_string(); - let requested_asset_path = path.strip_prefix("wry.bench://").unwrap(); - let (data, mimetype): (Vec, _) = match requested_asset_path { + let requested_asset_path = path.strip_prefix("wrybench://localhost").unwrap(); + let (data, mimetype): (Vec, String) = match requested_asset_path { "/index.css" => ( include_bytes!("static/index.css").to_vec(), "text/css".into(), @@ -52,7 +52,7 @@ fn main() -> wry::Result<()> { .body(data) .map_err(Into::into) }) - .with_url("wry.bench://")? + .with_url("wrybench://localhost")? .with_ipc_handler(handler) .build()?; diff --git a/bench/tests/src/custom_protocol.rs b/bench/tests/src/custom_protocol.rs index 51c325cfe..4fb86792f 100644 --- a/bench/tests/src/custom_protocol.rs +++ b/bench/tests/src/custom_protocol.rs @@ -32,7 +32,7 @@ fn main() -> wry::Result<()> { let _webview = WebViewBuilder::new(window) .unwrap() .with_ipc_handler(handler) - .with_custom_protocol("wry.bench".into(), move |_request| { + .with_custom_protocol("wrybench".into(), move |_request| { let index_html = r#" @@ -56,7 +56,7 @@ fn main() -> wry::Result<()> { .body(index_html.into()) .map_err(Into::into) }) - .with_url("wry.bench://")? + .with_url("wrybench://localhost")? .build()?; event_loop.run(move |event, _, control_flow| { diff --git a/examples/custom_protocol.rs b/examples/custom_protocol.rs index 436ed6c2c..f6d92f288 100644 --- a/examples/custom_protocol.rs +++ b/examples/custom_protocol.rs @@ -2,6 +2,8 @@ // SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: MIT +use std::path::PathBuf; + fn main() -> wry::Result<()> { use std::fs::{canonicalize, read}; @@ -24,15 +26,21 @@ fn main() -> wry::Result<()> { let webview = WebViewBuilder::new(window) .unwrap() .with_custom_protocol("wry".into(), move |request| { - let path = request.uri().to_string(); - let path = path.strip_prefix("wry://").unwrap(); + let path = &request.uri().path(); // Read the file content from file path - let content = read(canonicalize(&path)?)?; + let content = read(canonicalize(PathBuf::from("examples").join( + if path == &"/" { + "index.html" + } else { + // remove leading slash + &path[1..] + }, + ))?)?; // Return asset contents and mime types based on file extentions // If you don't want to do this manually, there are some crates for you. // Such as `infer` and `mime_guess`. - let (data, meta) = if path.ends_with(".html") { + let (data, meta) = if path.ends_with(".html") || path == &"/" { (content, "text/html") } else if path.ends_with(".js") { (content, "text/javascript") @@ -48,7 +56,7 @@ fn main() -> wry::Result<()> { .map_err(Into::into) }) // tell the webview to load the custom protocol - .with_url("wry://examples/index.html")? + .with_url("wry://localhost")? .with_devtools(true) .build()?; diff --git a/examples/form_post.rs b/examples/form_post.rs index 89c6e4880..fe8ea6ac5 100644 --- a/examples/form_post.rs +++ b/examples/form_post.rs @@ -31,8 +31,8 @@ fn main() -> wry::Result<()> { } } - let path = request.uri().to_string(); - let path = path.strip_prefix("wry://").unwrap(); + // remove leading slash + let path = &request.uri().path()[1..]; Response::builder() .header(CONTENT_TYPE, "text/html") @@ -40,7 +40,7 @@ fn main() -> wry::Result<()> { .map_err(Into::into) }) // tell the webview to load the custom protocol - .with_url("wry://examples/form.html")? + .with_url("wry://localhost/examples/form.html")? .build()?; event_loop.run(move |event, _, control_flow| { diff --git a/examples/stream.html b/examples/stream.html index 40773c692..38ac349b8 100644 --- a/examples/stream.html +++ b/examples/stream.html @@ -13,7 +13,7 @@