package com.nutomic.syncthingandroid.syncthing;

import android.os.AsyncTask;
import android.util.Log;

import com.nutomic.syncthingandroid.util.Https;

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;

import java.io.IOException;

/**
 * Sends a new config to {@link #URI_CONFIG}.
 */
public class PostConfigTask extends AsyncTask<String, Void, Boolean> {

    private static final String TAG = "PostConfigTask";

    public static final String URI_CONFIG = "/rest/system/config";

    private String mHttpsCertPath;

    public PostConfigTask(String httpsCertPath) {
        mHttpsCertPath = httpsCertPath;
    }

    /**
     * params[0] Syncthing hostname
     * params[1] Syncthing API key
     * params[2] The new config
     */
    @Override
    protected Boolean doInBackground(String... params) {
        String fullUri = params[0] + URI_CONFIG;
        Log.v(TAG, "Calling Rest API at " + fullUri);

        HttpClient httpclient = Https.createHttpsClient(mHttpsCertPath);
        HttpPost post = new HttpPost(fullUri);
        post.addHeader(new BasicHeader(RestApi.HEADER_API_KEY, params[1]));

        try {
            post.setEntity(new StringEntity(params[3], HTTP.UTF_8));
            Log.v(TAG, "API call parameters: " + params[3]);
            httpclient.execute(post);
        } catch (IOException|IllegalArgumentException e) {
            Log.w(TAG, "Failed to call Rest API at " + fullUri, e);
            return false;
        }
        return true;
    }

}

@@ -1332,17 +1332,17 @@
 (params%5B
-3
+2
 %5D, HTTP.
@@ -1410,9 +1410,9 @@
 ams%5B
-3
+2
 %5D);%0A

Fix ArrayIndexOutOfBoundsException crash