]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
android: Add support to POST data via SimpleFetcher
authorTobias Brunner <tobias@strongswan.org>
Wed, 23 Aug 2017 14:14:36 +0000 (16:14 +0200)
committerTobias Brunner <tobias@strongswan.org>
Mon, 4 Sep 2017 08:41:29 +0000 (10:41 +0200)
That's required for OCSP verification.

src/frontends/android/app/src/main/java/org/strongswan/android/logic/SimpleFetcher.java
src/frontends/android/app/src/main/jni/libandroidbridge/backend/android_fetcher.c

index e7f029ba27fbb9771b4f1cc90644f1f5ca3d3232..b7334cee1df8f7298f591f9d8b13d6134181390b 100644 (file)
@@ -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;
        }
 }
index ce714106bd52ca75fa694d8d75ae0b83df331cc8..2e23f8b10804d6fff2e90e6dc8f5d3f6b996bc67 100644 (file)
@@ -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);
 }