From d23c96a0862a6440d3630979b5f6623034f49fa4 Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Sun, 27 Nov 2022 14:50:48 -0300 Subject: [PATCH] fix(android): properly parse content-type for response mime type --- .changes/fix-android-mime-type.md | 5 +++++ examples/menu.rs | 2 +- src/webview/android/binding.rs | 27 ++++++++++++++++++++++----- 3 files changed, 28 insertions(+), 6 deletions(-) create mode 100644 .changes/fix-android-mime-type.md diff --git a/.changes/fix-android-mime-type.md b/.changes/fix-android-mime-type.md new file mode 100644 index 000000000..a400e9125 --- /dev/null +++ b/.changes/fix-android-mime-type.md @@ -0,0 +1,5 @@ +--- +"wry": patch +--- + +Properly parse the content type header for the `android.webkit.WebResourceResponse` mime type. diff --git a/examples/menu.rs b/examples/menu.rs index 70561fc5b..0234d6d21 100644 --- a/examples/menu.rs +++ b/examples/menu.rs @@ -13,9 +13,9 @@ fn main() -> wry::Result<()> { accelerator::Accelerator, event::{Event, StartCause, WindowEvent}, event_loop::{ControlFlow, EventLoop}, - window::WindowBuilder, keyboard::{KeyCode, ModifiersState}, menu::{MenuBar, MenuItemAttributes}, + window::WindowBuilder, }, http::{header::CONTENT_TYPE, Response}, webview::WebViewBuilder, diff --git a/src/webview/android/binding.rs b/src/webview/android/binding.rs index b1336e239..539c7e3a5 100644 --- a/src/webview/android/binding.rs +++ b/src/webview/android/binding.rs @@ -65,11 +65,28 @@ fn handle_request(env: JNIEnv, request: JObject) -> Result { if let Some(response) = response { let status_code = response.status().as_u16() as i32; let reason_phrase = "OK"; - let encoding = "UTF-8"; - let mime_type = if let Some(mime) = response.headers().get(CONTENT_TYPE) { - env.new_string(mime.to_str().unwrap())?.into() + let (mime_type, encoding) = if let Some(content_type) = response.headers().get(CONTENT_TYPE) { + let content_type = content_type.to_str().unwrap().trim(); + let mut s = content_type.split(';'); + let mime_type = s.next().unwrap().trim(); + let mut encoding = None; + for token in s { + let token = token.trim(); + if token.starts_with("charset=") { + encoding.replace(token.split('=').nth(1).unwrap()); + break; + } + } + ( + env.new_string(mime_type)?.into(), + if let Some(encoding) = encoding { + env.new_string(&encoding)?.into() + } else { + JObject::null().into() + }, + ) } else { - JObject::null() + (JObject::null().into(), JObject::null().into()) }; let hashmap = env.find_class("java/util/HashMap")?; @@ -103,7 +120,7 @@ fn handle_request(env: JNIEnv, request: JObject) -> Result { let web_resource_response = env.new_object( web_resource_response_class, "(Ljava/lang/String;Ljava/lang/String;ILjava/lang/String;Ljava/util/Map;Ljava/io/InputStream;)V", - &[mime_type.into(), env.new_string(encoding)?.into(), status_code.into(), env.new_string(reason_phrase)?.into(), response_headers.into(), stream.into()], + &[mime_type, encoding, status_code.into(), env.new_string(reason_phrase)?.into(), response_headers.into(), stream.into()], )?; return Ok(*web_resource_response);