From: Tobias Brunner Date: Wed, 23 Aug 2017 14:14:36 +0000 (+0200) Subject: android: Add support to POST data via SimpleFetcher X-Git-Tag: 5.6.1dr2~10^2~2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=829cc56a534a67e904f0e1615258fc9c66e51cff;p=thirdparty%2Fstrongswan.git android: Add support to POST data via SimpleFetcher That's required for OCSP verification. --- diff --git a/src/frontends/android/app/src/main/java/org/strongswan/android/logic/SimpleFetcher.java b/src/frontends/android/app/src/main/java/org/strongswan/android/logic/SimpleFetcher.java index e7f029ba27..b7334cee1d 100644 --- a/src/frontends/android/app/src/main/java/org/strongswan/android/logic/SimpleFetcher.java +++ b/src/frontends/android/app/src/main/java/org/strongswan/android/logic/SimpleFetcher.java @@ -17,16 +17,18 @@ package org.strongswan.android.logic; import android.support.annotation.Keep; +import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; @Keep public class SimpleFetcher { - public static byte[] fetch(String uri) throws IOException + public static byte[] fetch(String uri, byte[] data, String contentType) throws IOException { URL url = new URL(uri); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); @@ -34,6 +36,18 @@ public class SimpleFetcher conn.setReadTimeout(10000); try { + if (contentType != null) + { + conn.setRequestProperty("Content-Type", contentType); + } + if (data != null) + { + conn.setDoOutput(true); + conn.setFixedLengthStreamingMode(data.length); + OutputStream out = new BufferedOutputStream(conn.getOutputStream()); + out.write(data); + out.close(); + } return streamToArray(conn.getInputStream()); } finally @@ -42,7 +56,7 @@ public class SimpleFetcher } } - private static byte[] streamToArray(InputStream in) + private static byte[] streamToArray(InputStream in) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; @@ -60,6 +74,10 @@ public class SimpleFetcher { e.printStackTrace(); } + finally + { + in.close(); + } return null; } } diff --git a/src/frontends/android/app/src/main/jni/libandroidbridge/backend/android_fetcher.c b/src/frontends/android/app/src/main/jni/libandroidbridge/backend/android_fetcher.c index ce714106bd..2e23f8b108 100644 --- a/src/frontends/android/app/src/main/jni/libandroidbridge/backend/android_fetcher.c +++ b/src/frontends/android/app/src/main/jni/libandroidbridge/backend/android_fetcher.c @@ -31,6 +31,16 @@ struct android_fetcher_t { * Callback function */ fetcher_callback_t cb; + + /** + * Data to POST + */ + chunk_t data; + + /** + * Type of data to POST + */ + char *request_type; }; METHOD(fetcher_t, fetch, status_t, @@ -38,8 +48,8 @@ METHOD(fetcher_t, fetch, status_t, { JNIEnv *env; jmethodID method_id; - jobjectArray jdata; - jstring juri; + jobjectArray jdata = NULL; + jstring juri, jct = NULL; chunk_t data; status_t status = FAILED; @@ -51,7 +61,7 @@ METHOD(fetcher_t, fetch, status_t, androidjni_attach_thread(&env); /* can't use FindClass here as this is not called by the main thread */ method_id = (*env)->GetStaticMethodID(env, android_simple_fetcher_class, - "fetch", "(Ljava/lang/String;)[B"); + "fetch", "(Ljava/lang/String;[BLjava/lang/String;)[B"); if (!method_id) { goto failed; @@ -61,8 +71,24 @@ METHOD(fetcher_t, fetch, status_t, { goto failed; } + if (this->request_type) + { + jct = (*env)->NewStringUTF(env, this->request_type); + if (!jct) + { + goto failed; + } + } + if (this->data.ptr) + { + jdata = byte_array_from_chunk(env, this->data); + if (!jdata) + { + goto failed; + } + } jdata = (*env)->CallStaticObjectMethod(env, android_simple_fetcher_class, - method_id, juri); + method_id, juri, jdata, jct); if (!jdata || androidjni_exception_occurred(env)) { goto failed; @@ -97,6 +123,16 @@ METHOD(fetcher_t, set_option, bool, this->cb = va_arg(args, fetcher_callback_t); break; } + case FETCH_REQUEST_DATA: + { + this->data = chunk_clone(va_arg(args, chunk_t)); + break; + } + case FETCH_REQUEST_TYPE: + { + this->request_type = strdup(va_arg(args, char*)); + break; + } default: supported = FALSE; break; @@ -108,6 +144,8 @@ METHOD(fetcher_t, set_option, bool, METHOD(fetcher_t, destroy, void, android_fetcher_t *this) { + chunk_clear(&this->data); + free(this->request_type); free(this); }