Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(macos): do not ignore errors on sending HTTP responses #1372

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/dont-ignore-response-errors-macos.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": patch
---

On macOS, do not ignore errors on sending HTTP resopnses. Errors now cause panics.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think our users would appreciate panics, we should instead just log::error the result

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, does it mean users can recover the error when it occurs? This is an internal error and users don't know why it happened. And there is no other way to receive a response via custom protocol. For unrecoverable errors, I believe panics are a correct way to handle them.

https://doc.rust-lang.org/beta/book/ch09-03-to-panic-or-not-to-panic.html

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lucasfernog what do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the quote from the rust book applies well to gui applications. gui apps should really never crash like this, or at least not from inside a library (from someone else than the app dev), but of course the dev not having a way to react to those errors is quite problematic too, hmm 🤔

2 changes: 0 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ pub enum Error {
#[cfg(target_os = "android")]
#[error(transparent)]
CrossBeamRecvError(#[from] crossbeam_channel::RecvError),
#[error("Custom protocol task is invalid.")]
CustomProtocolTaskInvalid,
#[error("Failed to register URL scheme: {0}, could be due to invalid URL scheme or the scheme is already registered.")]
UrlSchemeRegisterError(String),
}
31 changes: 14 additions & 17 deletions src/wkwebview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,19 +279,20 @@ impl InnerWebView {

let responder: Box<dyn FnOnce(HttpResponse<Cow<'static, [u8]>>)> = Box::new(
move |sent_response| {
fn check_webview_id_valid(webview_id: u32) -> crate::Result<()> {
match WEBVIEW_IDS.lock().unwrap().contains(&webview_id) {
true => Ok(()),
false => Err(crate::Error::CustomProtocolTaskInvalid),
}
fn assert_webview_id_valid(webview_id: u32) {
assert!(
WEBVIEW_IDS.lock().unwrap().contains(&webview_id),
"WebView ID {} does not exist",
webview_id,
);
}

unsafe fn response(
task: id,
webview_id: u32,
url: id, /* NSURL */
sent_response: HttpResponse<Cow<'_, [u8]>>,
) -> crate::Result<()> {
) {
let content = sent_response.body();
// default: application/octet-stream, but should be provided by the client
let wanted_mime = sent_response.headers().get(CONTENT_TYPE);
Expand All @@ -318,31 +319,27 @@ impl InnerWebView {
let urlresponse: id = msg_send![class!(NSHTTPURLResponse), alloc];
let response: id = msg_send![urlresponse, initWithURL:url statusCode: wanted_status_code HTTPVersion:NSString::new(&wanted_version) headerFields:headers];

check_webview_id_valid(webview_id)?;
assert_webview_id_valid(webview_id);
(*task)
.send_message::<(id,), ()>(sel!(didReceiveResponse:), (response,))
.map_err(|_| crate::Error::CustomProtocolTaskInvalid)?;
.unwrap();

// Send data
let bytes = content.as_ptr() as *mut c_void;
let data: id = msg_send![class!(NSData), alloc];
let data: id = msg_send![data, initWithBytesNoCopy:bytes length:content.len() freeWhenDone: if content.len() == 0 { NO } else { YES }];

check_webview_id_valid(webview_id)?;
assert_webview_id_valid(webview_id);
(*task)
.send_message::<(id,), ()>(sel!(didReceiveData:), (data,))
.map_err(|_| crate::Error::CustomProtocolTaskInvalid)?;
.unwrap();

// Finish
check_webview_id_valid(webview_id)?;
(*task)
.send_message::<(), ()>(sel!(didFinish), ())
.map_err(|_| crate::Error::CustomProtocolTaskInvalid)?;

Ok(())
assert_webview_id_valid(webview_id);
(*task).send_message::<(), ()>(sel!(didFinish), ()).unwrap();
}

let _ = response(task, webview_id, url, sent_response);
response(task, webview_id, url, sent_response);
let () = msg_send![task, release];
},
);
Expand Down