Add some vm panic protection for non UTF-8 URLs.

This commit is contained in:
Kavin 2023-03-04 03:58:40 +00:00
parent 574c384bf1
commit 35653a4832
No known key found for this signature in database
GPG Key ID: 49451E4482CC5BCD
2 changed files with 11 additions and 3 deletions

View File

@ -43,7 +43,7 @@ signing {
}
group = 'rocks.kavin'
version = '1.0'
version = '1.0.1'
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17

View File

@ -7,7 +7,6 @@ use lazy_static::lazy_static;
use reqwest::{Client, Method, Url};
use tokio::runtime::Runtime;
pub fn add(left: usize, right: usize) -> usize {
left + right
}
@ -32,7 +31,16 @@ pub extern "system" fn Java_rocks_kavin_reqwest4j_ReqwestUtils_fetch(
// set method, url, body, headers
let method = Method::from_bytes(env.get_string(method).unwrap().to_bytes()).unwrap();
let url = Url::parse(&env.get_string(url).unwrap().to_str().unwrap()).unwrap();
let url = &env.get_string(url).unwrap();
let url = url.to_str();
if url.is_err() {
env.throw_new("java/lang/IllegalArgumentException", "Invalid URL provided, couldn't get string as UTF-8").unwrap();
return JObject::null().into_raw();
}
let url = Url::parse(url.unwrap()).unwrap();
let body = env.convert_byte_array(body).unwrap_or_default();
let headers: JMap = JMap::from_env(&env, headers).unwrap();
let headers = headers.iter().unwrap().fold(HashMap::new(), |mut headers, (key, value)| {