generated from QuiltMC/quilt-template-mod
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from LostLuma/feature/native-http-client
Add minimal native HTTP client
- Loading branch information
Showing
27 changed files
with
681 additions
and
214 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Catculator | ||
|
||
Native implementations of some of Kit Tunes features. | ||
|
||
## Development | ||
|
||
Test new binaries by setting the `catculator.natives_path` system property when running the game: | ||
Example: `-Dcatculator.natives_path=/home/lilly/projects/kit-tunes/projects/catculator/target/debug` |
7 changes: 7 additions & 0 deletions
7
.../catculator/src/main/java/net/pixaurora/catculator/api/error/ClientResponseException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package net.pixaurora.catculator.api.error; | ||
|
||
public class ClientResponseException extends Exception { | ||
public ClientResponseException(String message) { | ||
super(message); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
projects/catculator/src/main/java/net/pixaurora/catculator/api/http/Client.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package net.pixaurora.catculator.api.http; | ||
|
||
import net.pixaurora.catculator.impl.http.ClientImpl; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.io.IOException; | ||
|
||
public interface Client extends AutoCloseable { | ||
static @NotNull Client create(String userAgent) throws IOException { | ||
return new ClientImpl(userAgent); | ||
} | ||
|
||
@NotNull RequestBuilder get(String url); | ||
@NotNull RequestBuilder post(String url); | ||
|
||
@Override | ||
void close(); // Remove throws Exception from AutoCloseable | ||
} |
12 changes: 12 additions & 0 deletions
12
projects/catculator/src/main/java/net/pixaurora/catculator/api/http/RequestBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package net.pixaurora.catculator.api.http; | ||
|
||
import net.pixaurora.catculator.api.error.ClientResponseException; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public interface RequestBuilder { | ||
@NotNull Response send() throws ClientResponseException; | ||
|
||
@NotNull RequestBuilder body(byte[] data); | ||
@NotNull RequestBuilder query(String key, String value); | ||
@NotNull RequestBuilder header(String key, String value); | ||
} |
14 changes: 14 additions & 0 deletions
14
projects/catculator/src/main/java/net/pixaurora/catculator/api/http/Response.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package net.pixaurora.catculator.api.http; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public interface Response { | ||
int status(); | ||
byte[] body(); | ||
@Nullable String header(@NotNull String name); | ||
|
||
default boolean ok() { | ||
return this.status() >= 200 && this.status() < 300; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
projects/catculator/src/main/java/net/pixaurora/catculator/impl/http/ClientImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package net.pixaurora.catculator.impl.http; | ||
|
||
import net.pixaurora.catculator.api.http.Client; | ||
import net.pixaurora.catculator.api.http.RequestBuilder; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.io.IOException; | ||
|
||
public class ClientImpl implements Client { | ||
private final long ptr; | ||
private boolean active = true; | ||
|
||
public ClientImpl(String userAgent) throws IOException { | ||
this.ptr = create(userAgent); | ||
} | ||
|
||
@Override | ||
public @NotNull RequestBuilder get(String url) { | ||
if (this.active) { | ||
return this.request("GET", url); | ||
} else { | ||
throw new RuntimeException("HTTP client inactive."); | ||
} | ||
} | ||
|
||
@Override | ||
public @NotNull RequestBuilder post(String url) { | ||
if (this.active) { | ||
return this.request("POST", url); | ||
} else { | ||
throw new RuntimeException("HTTP client inactive."); | ||
} | ||
} | ||
|
||
@Override | ||
public void close() { | ||
if (!this.active) { | ||
return; | ||
} | ||
|
||
this.drop(); | ||
this.active = false; | ||
} | ||
|
||
private native @NotNull RequestBuilder request(String method, String url); | ||
|
||
private static native long create(@NotNull String userAgent) throws IOException; | ||
private native void drop(); | ||
} |
42 changes: 42 additions & 0 deletions
42
projects/catculator/src/main/java/net/pixaurora/catculator/impl/http/RequestBuilderImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package net.pixaurora.catculator.impl.http; | ||
|
||
import net.pixaurora.catculator.api.http.RequestBuilder; | ||
import net.pixaurora.catculator.api.http.Response; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public class RequestBuilderImpl implements RequestBuilder { | ||
private long ptr; | ||
|
||
private RequestBuilderImpl(long ptr) { | ||
this.ptr = ptr; | ||
} | ||
|
||
@Override | ||
public @NotNull Response send() { | ||
return this.send0(); | ||
} | ||
|
||
@Override | ||
public @NotNull RequestBuilder body(byte[] data) { | ||
this.body0(data); | ||
return this; | ||
} | ||
|
||
@Override | ||
public @NotNull RequestBuilder query(String key, String value) { | ||
this.query0(key, value); | ||
return this; | ||
} | ||
|
||
@Override | ||
public @NotNull RequestBuilder header(String key, String value) { | ||
this.header0(key, value); | ||
return this; | ||
} | ||
|
||
private native @NotNull Response send0(); | ||
|
||
private native void body0(byte[] data); | ||
private native void query0(String key, String value); | ||
private native void header0(String key, String value); | ||
} |
34 changes: 34 additions & 0 deletions
34
projects/catculator/src/main/java/net/pixaurora/catculator/impl/http/ResponseImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package net.pixaurora.catculator.impl.http; | ||
|
||
import net.pixaurora.catculator.api.http.Response; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.Map; | ||
|
||
public class ResponseImpl implements Response { | ||
private final int status; | ||
private final byte[] body; | ||
private final Map<String, String> headers; | ||
|
||
private ResponseImpl(int status, byte[] body, Map<String, String> headers) { | ||
this.status = status; | ||
this.body = body; | ||
this.headers = headers; | ||
} | ||
|
||
@Override | ||
public int status() { | ||
return this.status; | ||
} | ||
|
||
@Override | ||
public byte[] body() { | ||
return this.body; | ||
} | ||
|
||
@Override | ||
public @Nullable String header(@NotNull String name) { | ||
return this.headers.get(name); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
projects/catculator/src/main/java/net/pixaurora/catculator/impl/util/JniUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package net.pixaurora.catculator.impl.util; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class JniUtil { | ||
private static @NotNull Map<String, String> newMap() { | ||
return new HashMap<>(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
use jni::{ | ||
objects::{JClass, JObject, JString, JValue}, | ||
sys::jlong, | ||
JNIEnv, | ||
}; | ||
use reqwest::{blocking::Client, Method}; | ||
|
||
use crate::{ | ||
bridge::{drop_box, pack_box, pull_box}, | ||
utils::JStringToString, | ||
Error, Result, | ||
}; | ||
|
||
const REQUEST_BUILDER_CLASS: &str = "net/pixaurora/catculator/impl/http/RequestBuilderImpl"; | ||
|
||
fn create(env: &mut JNIEnv, user_agent: JString) -> Result<jlong> { | ||
let client = Client::builder() | ||
.https_only(true) | ||
.user_agent(user_agent.to_string(env)?) | ||
.build()?; | ||
|
||
Ok(pack_box(client)) | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "system" fn Java_net_pixaurora_catculator_impl_http_ClientImpl_create<'r>( | ||
mut env: JNIEnv<'r>, | ||
_class: JClass<'r>, | ||
user_agent: JString<'r>, | ||
) -> jlong { | ||
match create(&mut env, user_agent) { | ||
Ok(ptr) => return ptr, | ||
Err(error) => error.throw(&mut env), | ||
} | ||
|
||
jlong::default() | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "system" fn Java_net_pixaurora_catculator_impl_http_ClientImpl_drop<'r>( | ||
mut env: JNIEnv<'r>, | ||
this: JObject<'r>, | ||
) -> () { | ||
if let Err(error) = drop_box::<Client>(&mut env, &this) { | ||
panic!("Couldn't drop http client due to an error! {}", error); | ||
} | ||
} | ||
|
||
fn request<'r>( | ||
env: &mut JNIEnv<'r>, | ||
this: &JObject<'r>, | ||
method: JString<'r>, | ||
url: JString<'r>, | ||
) -> Result<JObject<'r>> { | ||
let client = pull_box::<Client>(env, this)?; | ||
|
||
let method = match Method::from_bytes(method.to_string(env)?.as_bytes()) { | ||
Ok(method) => method, | ||
Err(_) => return Err(Error::String(String::from("Invalid HTTP method."))), | ||
}; | ||
|
||
let ptr = pack_box(client.request(method, url.to_string(env)?)); | ||
|
||
let class = env.find_class(REQUEST_BUILDER_CLASS)?; | ||
let instance = env.new_object(class, "(J)V", &[JValue::Long(ptr)])?; | ||
|
||
Ok(instance) | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "system" fn Java_net_pixaurora_catculator_impl_http_ClientImpl_request<'r>( | ||
mut env: JNIEnv<'r>, | ||
this: JObject<'r>, | ||
method: JString<'r>, | ||
url: JString<'r>, | ||
) -> JObject<'r> { | ||
match request(&mut env, &this, method, url) { | ||
Ok(object) => return object, | ||
Err(error) => error.throw(&mut env), | ||
}; | ||
|
||
JObject::null() | ||
} |
Oops, something went wrong.