Skip to content

Commit

Permalink
refactor(protocol): respond with a function call instead of return va…
Browse files Browse the repository at this point in the history
…lue (#1017)

Co-authored-by: Amr Bashir <amr.bashir2015@gmail.com>
  • Loading branch information
lucasfernog and amrbashir committed Sep 6, 2023
1 parent 5ca0651 commit 4bdf1c3
Show file tree
Hide file tree
Showing 10 changed files with 452 additions and 308 deletions.
5 changes: 5 additions & 0 deletions .changes/custom-protocol-responder.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": minor
---

Added `WebViewBuilder::with_asynchronous_custom_protocol` to allow implementing a protocol handler that resolves asynchronously.
5 changes: 5 additions & 0 deletions .changes/with-custom-protocol-rturn-value.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": minor
---

**Breaking change:** `WebViewBuidler::with_custom_protocol` closure now returns `http::Response` instead of `Result<http::Response>`.
104 changes: 60 additions & 44 deletions examples/custom_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

const PAGE1_HTML: &[u8] = include_bytes!("custom_protocol_page1.html");
use std::{
fs::{canonicalize, read},
path::PathBuf,
};

fn main() -> wry::Result<()> {
use std::{
fs::{canonicalize, read},
path::PathBuf,
};
use http::Request;
use wry::{
application::{
event::{Event, StartCause, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::WindowBuilder,
},
http::{header::CONTENT_TYPE, Response},
webview::WebViewBuilder,
};

use wry::{
application::{
event::{Event, StartCause, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::WindowBuilder,
},
http::{header::CONTENT_TYPE, Response},
webview::WebViewBuilder,
};
const PAGE1_HTML: &[u8] = include_bytes!("custom_protocol_page1.html");

fn main() -> wry::Result<()> {
let event_loop = EventLoop::new();
let window = WindowBuilder::new()
.with_title("Custom Protocol")
Expand All @@ -28,35 +29,17 @@ fn main() -> wry::Result<()> {

let _webview = WebViewBuilder::new(window)
.unwrap()
.with_custom_protocol("wry".into(), move |request| {
let path = request.uri().path();
// Read the file content from file path
let content = if path == "/" {
PAGE1_HTML.into()
} else {
// `1..` for removing leading slash
read(canonicalize(PathBuf::from("examples").join(&path[1..]))?)?.into()
};

// 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 mimetype = if path.ends_with(".html") || path == "/" {
"text/html"
} else if path.ends_with(".js") {
"text/javascript"
} else if path.ends_with(".png") {
"image/png"
} else if path.ends_with(".wasm") {
"application/wasm"
} else {
unimplemented!();
};

Response::builder()
.header(CONTENT_TYPE, mimetype)
.body(content)
.map_err(Into::into)
.with_asynchronous_custom_protocol("wry".into(), move |request, responder| {
match get_wry_response(request) {
Ok(http_response) => responder.respond(http_response),
Err(e) => responder.respond(
http::Response::builder()
.header(CONTENT_TYPE, "text/plain")
.status(500)
.body(e.to_string().as_bytes().to_vec())
.unwrap(),
),
}
})
// tell the webview to load the custom protocol
.with_url("wry://localhost")?
Expand All @@ -75,3 +58,36 @@ fn main() -> wry::Result<()> {
}
});
}

fn get_wry_response(
request: Request<Vec<u8>>,
) -> Result<http::Response<Vec<u8>>, Box<dyn std::error::Error>> {
let path = request.uri().path();
// Read the file content from file path
let content = if path == "/" {
PAGE1_HTML.into()
} else {
// `1..` for removing leading slash
read(canonicalize(PathBuf::from("examples").join(&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 mimetype = if path.ends_with(".html") || path == "/" {
"text/html"
} else if path.ends_with(".js") {
"text/javascript"
} else if path.ends_with(".png") {
"image/png"
} else if path.ends_with(".wasm") {
"application/wasm"
} else {
unimplemented!();
};

Response::builder()
.header(CONTENT_TYPE, mimetype)
.body(content)
.map_err(Into::into)
}
45 changes: 29 additions & 16 deletions examples/form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

fn main() -> wry::Result<()> {
use std::fs::{canonicalize, read};

use wry::{
application::{
event::{Event, StartCause, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::WindowBuilder,
},
http::{header::CONTENT_TYPE, method::Method, Response},
webview::WebViewBuilder,
};
use std::{
borrow::Cow,
fs::{canonicalize, read},
};

use wry::{
application::{
event::{Event, StartCause, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::WindowBuilder,
},
http::{header::CONTENT_TYPE, method::Method, Response},
webview::WebViewBuilder,
};

fn main() -> wry::Result<()> {
let event_loop = EventLoop::new();
let window = WindowBuilder::new()
.with_title("Hello World")
Expand All @@ -34,10 +37,13 @@ fn main() -> wry::Result<()> {
// remove leading slash
let path = &request.uri().path()[1..];

Response::builder()
.header(CONTENT_TYPE, "text/html")
.body(read(canonicalize(path)?)?.into())
.map_err(Into::into)
get_response(path).unwrap_or_else(|error| {
http::Response::builder()
.status(http::StatusCode::BAD_REQUEST)
.header(CONTENT_TYPE, "text/plain")
.body(error.to_string().as_bytes().to_vec().into())
.unwrap()
})
})
// tell the webview to load the custom protocol
.with_url("wry://localhost/examples/form.html")?
Expand All @@ -56,3 +62,10 @@ fn main() -> wry::Result<()> {
}
});
}

fn get_response(path: &str) -> Result<Response<Cow<'static, [u8]>>, Box<dyn std::error::Error>> {
Response::builder()
.header(CONTENT_TYPE, "text/html")
.body(read(canonicalize(path)?)?.into())
.map_err(Into::into)
}
Loading

0 comments on commit 4bdf1c3

Please sign in to comment.