Skip to content

Commit

Permalink
fix(android): properly parse content-type for response mime type (#772)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Nov 28, 2022
1 parent a847197 commit 1db5ea6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .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.
2 changes: 1 addition & 1 deletion examples/menu.rs
Expand Up @@ -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,
Expand Down
27 changes: 22 additions & 5 deletions src/webview/android/binding.rs
Expand Up @@ -65,11 +65,28 @@ fn handle_request(env: JNIEnv, request: JObject) -> Result<jobject, JniError> {
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")?;
Expand Down Expand Up @@ -103,7 +120,7 @@ fn handle_request(env: JNIEnv, request: JObject) -> Result<jobject, JniError> {
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);
Expand Down

0 comments on commit 1db5ea6

Please sign in to comment.