lpac-jni: Update HTTP interface
This commit is contained in:
parent
51357d945b
commit
b5a63f3de3
|
@ -24,5 +24,5 @@ interface HttpInterface {
|
|||
}
|
||||
}
|
||||
|
||||
fun transmit(url: String, tx: ByteArray): HttpResponse
|
||||
fun transmit(url: String, tx: ByteArray, headers: Array<String>): HttpResponse
|
||||
}
|
|
@ -5,15 +5,20 @@ import java.net.HttpURLConnection
|
|||
import java.net.URL
|
||||
|
||||
class HttpInterfaceImpl: HttpInterface {
|
||||
override fun transmit(url: String, tx: ByteArray): HttpInterface.HttpResponse {
|
||||
android.util.Log.d("aaa", url)
|
||||
override fun transmit(
|
||||
url: String,
|
||||
tx: ByteArray,
|
||||
headers: Array<String>
|
||||
): HttpInterface.HttpResponse {
|
||||
val conn = URL(url).openConnection() as HttpURLConnection
|
||||
conn.requestMethod = "POST"
|
||||
conn.doInput = true
|
||||
conn.doOutput = true
|
||||
conn.setRequestProperty("User-Agent", "gsma-rsp-lpad")
|
||||
conn.setRequestProperty("X-Admin-Protocol", "gsma/rsp/v2.2.0")
|
||||
conn.setRequestProperty("Content-Type", "application/json")
|
||||
|
||||
for (h in headers) {
|
||||
val s = h.split(":", limit = 2)
|
||||
conn.setRequestProperty(s[0], s[1])
|
||||
}
|
||||
|
||||
conn.outputStream.write(tx)
|
||||
conn.outputStream.flush()
|
||||
|
|
|
@ -1 +1 @@
|
|||
Subproject commit dd568c3e0ef6128e273582d92a355977de2bbeb2
|
||||
Subproject commit 29984b34220581ec3779876d964b41bb566851f5
|
|
@ -24,7 +24,7 @@ void interface_wrapper_init() {
|
|||
|
||||
jclass http_class = (*env)->FindClass(env, "net/typeblog/lpac_jni/HttpInterface");
|
||||
method_http_transmit = (*env)->GetMethodID(env, http_class, "transmit",
|
||||
"(Ljava/lang/String;[B)Lnet/typeblog/lpac_jni/HttpInterface$HttpResponse;");
|
||||
"(Ljava/lang/String;[B[Ljava/lang/String;)Lnet/typeblog/lpac_jni/HttpInterface$HttpResponse;");
|
||||
|
||||
jclass resp_class = (*env)->FindClass(env, "net/typeblog/lpac_jni/HttpInterface$HttpResponse");
|
||||
field_resp_rcode = (*env)->GetFieldID(env, resp_class, "rcode", "I");
|
||||
|
@ -72,12 +72,24 @@ static int apdu_interface_transmit(struct euicc_ctx *ctx, uint8_t **rx, uint32_t
|
|||
return 0;
|
||||
}
|
||||
|
||||
static int http_interface_transmit(struct euicc_ctx *ctx, const char *url, uint32_t *rcode, uint8_t **rx, uint32_t *rx_len, const uint8_t *tx, uint32_t tx_len) {
|
||||
static int http_interface_transmit(struct euicc_ctx *ctx, const char *url, uint32_t *rcode, uint8_t **rx, uint32_t *rx_len, const uint8_t *tx, uint32_t tx_len, const char **headers) {
|
||||
LPAC_JNI_SETUP_ENV;
|
||||
jstring jurl = toJString(env, url);
|
||||
jbyteArray txArr = (*env)->NewByteArray(env, tx_len);
|
||||
(*env)->SetByteArrayRegion(env, txArr, 0, tx_len, (const jbyte *) tx);
|
||||
jobject ret = (*env)->CallObjectMethod(env, LPAC_JNI_CTX(ctx)->http_interface, method_http_transmit, jurl, txArr);
|
||||
|
||||
int num_headers = 0;
|
||||
while (headers[num_headers] != NULL) {
|
||||
num_headers++;
|
||||
}
|
||||
jobjectArray headersArr = (*env)->NewObjectArray(env, num_headers, string_class, NULL);
|
||||
for (int i = 0; i < num_headers; i++) {
|
||||
jstring header = toJString(env, headers[i]);
|
||||
(*env)->SetObjectArrayElement(env, headersArr, i, header);
|
||||
(*env)->DeleteLocalRef(env, header);
|
||||
}
|
||||
|
||||
jobject ret = (*env)->CallObjectMethod(env, LPAC_JNI_CTX(ctx)->http_interface, method_http_transmit, jurl, txArr, headersArr);
|
||||
LPAC_JNI_EXCEPTION_RETURN;
|
||||
*rcode = (*env)->GetIntField(env, ret, field_resp_rcode);
|
||||
jbyteArray rxArr = (jbyteArray) (*env)->GetObjectField(env, ret, field_resp_data);
|
||||
|
@ -86,6 +98,7 @@ static int http_interface_transmit(struct euicc_ctx *ctx, const char *url, uint3
|
|||
(*env)->GetByteArrayRegion(env, rxArr, 0, *rx_len, *rx);
|
||||
(*env)->DeleteLocalRef(env, txArr);
|
||||
(*env)->DeleteLocalRef(env, rxArr);
|
||||
(*env)->DeleteLocalRef(env, headersArr);
|
||||
(*env)->DeleteLocalRef(env, ret);
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -14,5 +14,6 @@ struct lpac_jni_ctx {
|
|||
(*jvm)->AttachCurrentThread(jvm, &env, NULL)
|
||||
|
||||
extern JavaVM *jvm;
|
||||
extern jclass string_class;
|
||||
|
||||
jstring toJString(JNIEnv *env, const char *pat);
|
Loading…
Reference in a new issue