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(android): properly parse content-type for response mime type #772

Merged
merged 1 commit into from
Nov 28, 2022
Merged
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/fix-android-mime-type.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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