From d843e8c33c51ed38224a869968c5184cddf3c185 Mon Sep 17 00:00:00 2001 From: rhysd Date: Fri, 27 Sep 2024 00:03:47 +0900 Subject: [PATCH] fix(macos): do not ignore errors on sending HTTP responses Signed-off-by: rhysd --- .changes/dont-ignore-response-errors-macos.md | 5 +++ src/error.rs | 2 -- src/wkwebview/mod.rs | 31 +++++++++---------- 3 files changed, 19 insertions(+), 19 deletions(-) create mode 100644 .changes/dont-ignore-response-errors-macos.md diff --git a/.changes/dont-ignore-response-errors-macos.md b/.changes/dont-ignore-response-errors-macos.md new file mode 100644 index 000000000..b45a7f8e1 --- /dev/null +++ b/.changes/dont-ignore-response-errors-macos.md @@ -0,0 +1,5 @@ +--- +"wry": patch +--- + +On macOS, do not ignore errors on sending HTTP resopnses. Errors now cause panics. diff --git a/src/error.rs b/src/error.rs index 7e3946b14..f6b1cbd29 100644 --- a/src/error.rs +++ b/src/error.rs @@ -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), } diff --git a/src/wkwebview/mod.rs b/src/wkwebview/mod.rs index 6ff79df70..d50ac6b87 100644 --- a/src/wkwebview/mod.rs +++ b/src/wkwebview/mod.rs @@ -279,11 +279,12 @@ impl InnerWebView { let responder: Box>)> = 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( @@ -291,7 +292,7 @@ impl InnerWebView { webview_id: u32, url: id, /* NSURL */ sent_response: HttpResponse>, - ) -> 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); @@ -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]; }, );