mirror of
https://github.com/silkimen/cordova-plugin-advanced-http.git
synced 2026-01-31 00:00:03 +08:00
WIP: re-implement Upload and Download Runnables
This commit is contained in:
15
plugin.xml
15
plugin.xml
@@ -50,7 +50,7 @@
|
||||
<platform name="android">
|
||||
<config-file target="res/xml/config.xml" parent="/*">
|
||||
<feature name="CordovaHttpPlugin">
|
||||
<param name="android-package" value="com.synconset.cordovahttp.CordovaHttpPlugin"/>
|
||||
<param name="android-package" value="com.silkimen.cordovahttp.CordovaHttpPlugin"/>
|
||||
</feature>
|
||||
</config-file>
|
||||
<config-file target="AndroidManifest.xml" parent="/manifest">
|
||||
@@ -61,17 +61,14 @@
|
||||
<source-file src="src/android/com/github/kevinsawicki/http/OkConnectionFactory.java" target-dir="src/com/github/kevinsawicki/http"/>
|
||||
<source-file src="src/android/com/github/kevinsawicki/http/TLSSocketFactory.java" target-dir="src/com/github/kevinsawicki/http"/>
|
||||
-->
|
||||
<source-file src="src/android/com/synconset/cordovahttp/CordovaHttp.java" target-dir="src/com/synconset/cordovahttp"/>
|
||||
<source-file src="src/android/com/synconset/cordovahttp/CordovaHttpDownload.java" target-dir="src/com/synconset/cordovahttp"/>
|
||||
<source-file src="src/android/com/silkimen/cordovahttp/CordovaHttpDownload.java" target-dir="src/com/silkimen/cordovahttp"/>
|
||||
<source-file src="src/android/com/silkimen/cordovahttp/CordovaHttpPlugin.java" target-dir="src/com/silkimen/cordovahttp"/>
|
||||
<source-file src="src/android/com/silkimen/cordovahttp/CordovaHttpRequest.java" target-dir="src/com/silkimen/cordovahttp"/>
|
||||
<source-file src="src/android/com/synconset/cordovahttp/CordovaHttpPlugin.java" target-dir="src/com/synconset/cordovahttp"/>
|
||||
<source-file src="src/android/com/synconset/cordovahttp/CordovaHttpPost.java" target-dir="src/com/synconset/cordovahttp"/>
|
||||
<source-file src="src/android/com/synconset/cordovahttp/CordovaHttpPut.java" target-dir="src/com/synconset/cordovahttp"/>
|
||||
<source-file src="src/android/com/synconset/cordovahttp/CordovaHttpPatch.java" target-dir="src/com/synconset/cordovahttp"/>
|
||||
<source-file src="src/android/com/synconset/cordovahttp/CordovaHttpUpload.java" target-dir="src/com/synconset/cordovahttp"/>
|
||||
<source-file src="src/android/com/silkimen/cordovahttp/CordovaHttpResponse.java" target-dir="src/com/silkimen/cordovahttp"/>
|
||||
<source-file src="src/android/com/silkimen/cordovahttp/CordovaHttpUpload.java" target-dir="src/com/silkimen/cordovahttp"/>
|
||||
|
||||
<source-file src="src/android/com/silkimen/http/HttpBodyDecoder.java" target-dir="src/com/silkimen/http"/>
|
||||
<source-file src="src/android/com/silkimen/http/HttpRequest.java" target-dir="src/com/silkimen/http"/>
|
||||
<source-file src="src/android/com/silkimen/http/HttpResponse.java" target-dir="src/com/silkimen/http"/>
|
||||
<source-file src="src/android/com/silkimen/http/JsonUtils.java" target-dir="src/com/silkimen/http"/>
|
||||
<framework src="com.squareup.okhttp3:okhttp-urlconnection:3.10.0"/>
|
||||
</platform>
|
||||
|
||||
105
src/android/com/silkimen/cordovahttp/CordovaHttpDownload.java
Normal file
105
src/android/com/silkimen/cordovahttp/CordovaHttpDownload.java
Normal file
@@ -0,0 +1,105 @@
|
||||
package com.silkimen.cordovahttp;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.net.UnknownHostException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
import javax.net.ssl.SSLHandshakeException;
|
||||
|
||||
import com.silkimen.http.HttpBodyDecoder;
|
||||
import com.silkimen.http.HttpRequest;
|
||||
import com.silkimen.http.HttpRequest.HttpRequestException;
|
||||
import com.silkimen.http.JsonUtils;
|
||||
|
||||
import org.apache.cordova.CallbackContext;
|
||||
import org.apache.cordova.file.FileUtils;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
class CordovaHttpDownload implements Runnable {
|
||||
private static final String TAG = "Cordova-Plugin-HTTP";
|
||||
|
||||
private String url;
|
||||
private JSONObject params;
|
||||
private JSONObject headers;
|
||||
private String filePath;
|
||||
private int timeout;
|
||||
private CallbackContext callbackContext;
|
||||
|
||||
public CordovaHttpDownload(String url, JSONObject params, JSONObject headers, String filePath, int timeout,
|
||||
CallbackContext callbackContext) {
|
||||
|
||||
this.url = url;
|
||||
this.params = params;
|
||||
this.headers = headers;
|
||||
this.filePath = filePath;
|
||||
this.timeout = timeout;
|
||||
this.callbackContext = callbackContext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
CordovaHttpResponse response = new CordovaHttpResponse();
|
||||
|
||||
try {
|
||||
String processedUrl = HttpRequest.encode(HttpRequest.append(this.url, JsonUtils.getObjectMap(this.params)));
|
||||
|
||||
HttpRequest request = new HttpRequest(processedUrl, "GET")
|
||||
.followRedirects(true /* @TODO */)
|
||||
.readTimeout(this.timeout)
|
||||
.acceptCharset("UTF-8")
|
||||
.uncompress(true)
|
||||
.headers(JsonUtils.getStringMap(this.headers));
|
||||
|
||||
response.setStatus(request.code());
|
||||
response.setUrl(request.url().toString());
|
||||
response.setHeaders(request.headers());
|
||||
|
||||
if (request.code() >= 200 && request.code() < 300) {
|
||||
File file = new File(new URI(filePath));
|
||||
|
||||
request.receive(file);
|
||||
response.setFileEntry(FileUtils.getFilePlugin().getEntryForFile(file));
|
||||
} else {
|
||||
response.setErrorMessage("There was an error downloading the file");
|
||||
}
|
||||
} catch (HttpRequestException e) {
|
||||
if (e.getCause() instanceof SSLHandshakeException) {
|
||||
response.setStatus(-2);
|
||||
response.setErrorMessage("SSL handshake failed: " + e.getMessage());
|
||||
Log.w(TAG, "SSL handshake failed", e);
|
||||
} else if (e.getCause() instanceof UnknownHostException) {
|
||||
response.setStatus(-3);
|
||||
response.setErrorMessage("Host could not be resolved: " + e.getMessage());
|
||||
Log.w(TAG, "Host could not be resolved", e);
|
||||
} else if (e.getCause() instanceof SocketTimeoutException) {
|
||||
response.setStatus(-4);
|
||||
response.setErrorMessage("Request timed out: " + e.getMessage());
|
||||
Log.w(TAG, "Request timed out", e);
|
||||
} else {
|
||||
response.setStatus(-1);
|
||||
response.setErrorMessage("There was an error with the request: " + e.getCause().getMessage());
|
||||
Log.w(TAG, "Generic request error", e);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
response.setStatus(-1);
|
||||
response.setErrorMessage(e.getMessage());
|
||||
Log.e(TAG, "An unexpected error occured", e);
|
||||
}
|
||||
|
||||
try {
|
||||
if (response.hasFailed()) {
|
||||
this.callbackContext.error(response.toJSON());
|
||||
} else {
|
||||
this.callbackContext.success(response.toJSON());
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
Log.e(TAG, "An unexpected error occured while processing HTTP response", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
220
src/android/com/silkimen/cordovahttp/CordovaHttpPlugin.java
Normal file
220
src/android/com/silkimen/cordovahttp/CordovaHttpPlugin.java
Normal file
@@ -0,0 +1,220 @@
|
||||
package com.silkimen.cordovahttp;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.security.KeyStore;
|
||||
import java.security.KeyStore.TrustedCertificateEntry;
|
||||
import java.security.cert.Certificate;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Enumeration;
|
||||
|
||||
import org.apache.cordova.CallbackContext;
|
||||
import org.apache.cordova.CordovaInterface;
|
||||
import org.apache.cordova.CordovaPlugin;
|
||||
import org.apache.cordova.CordovaWebView;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import android.util.Log;
|
||||
import android.content.res.AssetManager;
|
||||
|
||||
public class CordovaHttpPlugin extends CordovaPlugin {
|
||||
private static final String TAG = "Cordova-Plugin-HTTP";
|
||||
|
||||
private static boolean followRedirects = true;
|
||||
|
||||
@Override
|
||||
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
|
||||
super.initialize(cordova, webView);
|
||||
|
||||
try {
|
||||
// HttpRequest.clearCerts();
|
||||
this.pinSSLCertsFromCAStore();
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "An error occured while loading system's CA certificates", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean execute(String action, final JSONArray args, final CallbackContext callbackContext)
|
||||
throws JSONException {
|
||||
|
||||
if (action == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
switch (action) {
|
||||
case "get":
|
||||
return this.executeHttpRequestWithParams(action, args, callbackContext);
|
||||
case "post":
|
||||
return this.executeHttpRequestWithData(action, args, callbackContext);
|
||||
case "put":
|
||||
return this.executeHttpRequestWithData(action, args, callbackContext);
|
||||
case "patch":
|
||||
return this.executeHttpRequestWithData(action, args, callbackContext);
|
||||
case "head":
|
||||
return this.executeHttpRequestWithParams(action, args, callbackContext);
|
||||
case "delete":
|
||||
return this.executeHttpRequestWithParams(action, args, callbackContext);
|
||||
case "uploadFile":
|
||||
return this.uploadFile(args, callbackContext);
|
||||
case "downloadFile":
|
||||
return this.downloadFile(args, callbackContext);
|
||||
case "setSSLCertMode":
|
||||
return this.setSSLCertMode(args, callbackContext);
|
||||
case "disableRedirect":
|
||||
return this.disableRedirect(args, callbackContext);
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean executeHttpRequestWithParams(final String method, final JSONArray args,
|
||||
final CallbackContext callbackContext) throws JSONException {
|
||||
|
||||
String url = args.getString(0);
|
||||
JSONObject params = args.getJSONObject(1);
|
||||
JSONObject headers = args.getJSONObject(2);
|
||||
int timeout = args.getInt(3) * 1000;
|
||||
|
||||
CordovaHttpRequest get = new CordovaHttpRequest(method.toUpperCase(), url, params, headers, timeout,
|
||||
callbackContext);
|
||||
|
||||
cordova.getThreadPool().execute(get);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean executeHttpRequestWithData(final String method, final JSONArray args,
|
||||
final CallbackContext callbackContext) throws JSONException {
|
||||
|
||||
String url = args.getString(0);
|
||||
Object data = args.get(1);
|
||||
String serializer = args.getString(2);
|
||||
JSONObject headers = args.getJSONObject(3);
|
||||
int timeout = args.getInt(4) * 1000;
|
||||
|
||||
CordovaHttpRequest request = new CordovaHttpRequest(method.toUpperCase(), url, serializer, data, headers, timeout,
|
||||
callbackContext);
|
||||
|
||||
cordova.getThreadPool().execute(request);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean uploadFile(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
|
||||
String url = args.getString(0);
|
||||
JSONObject params = args.getJSONObject(1);
|
||||
JSONObject headers = args.getJSONObject(2);
|
||||
String filePath = args.getString(3);
|
||||
String uploadName = args.getString(4);
|
||||
int timeout = args.getInt(5) * 1000;
|
||||
|
||||
CordovaHttpUpload upload = new CordovaHttpUpload(url, params, headers,
|
||||
filePath, uploadName, timeout, callbackContext);
|
||||
|
||||
cordova.getThreadPool().execute(upload);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean downloadFile(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
|
||||
String url = args.getString(0);
|
||||
JSONObject params = args.getJSONObject(1);
|
||||
JSONObject headers = args.getJSONObject(2);
|
||||
String filePath = args.getString(3);
|
||||
int timeout = args.getInt(4) * 1000;
|
||||
|
||||
CordovaHttpDownload download = new CordovaHttpDownload(url, params, headers, filePath, timeout, callbackContext);
|
||||
|
||||
cordova.getThreadPool().execute(download);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean setSSLCertMode(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
|
||||
String mode = args.getString(0);
|
||||
|
||||
// HttpRequest.clearCerts();
|
||||
|
||||
if (mode.equals("legacy")) {
|
||||
// HttpRequest.setSSLCertMode(HttpRequest.CERT_MODE_DEFAULT);
|
||||
callbackContext.success();
|
||||
} else if (mode.equals("nocheck")) {
|
||||
// HttpRequest.setSSLCertMode(HttpRequest.CERT_MODE_TRUSTALL);
|
||||
callbackContext.success();
|
||||
} else if (mode.equals("pinned")) {
|
||||
try {
|
||||
this.loadSSLCertsFromBundle();
|
||||
// HttpRequest.setSSLCertMode(HttpRequest.CERT_MODE_PINNED);
|
||||
callbackContext.success();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
callbackContext.error("There was an error setting up ssl pinning");
|
||||
}
|
||||
} else if (mode.equals("default")) {
|
||||
try {
|
||||
this.pinSSLCertsFromCAStore();
|
||||
callbackContext.success();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
callbackContext.error("There was an error loading system's CA certificates");
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean disableRedirect(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
|
||||
followRedirects = !args.getBoolean(0);
|
||||
|
||||
callbackContext.success();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void pinSSLCertsFromCAStore() throws GeneralSecurityException, IOException {
|
||||
this.loadSSLCertsFromKeyStore("AndroidCAStore");
|
||||
// HttpRequest.setSSLCertMode(HttpRequest.CERT_MODE_PINNED);
|
||||
}
|
||||
|
||||
private void loadSSLCertsFromKeyStore(String storeType) throws GeneralSecurityException, IOException {
|
||||
KeyStore ks = KeyStore.getInstance(storeType);
|
||||
ks.load(null);
|
||||
Enumeration<String> aliases = ks.aliases();
|
||||
|
||||
while (aliases.hasMoreElements()) {
|
||||
String alias = aliases.nextElement();
|
||||
TrustedCertificateEntry certEntry = (TrustedCertificateEntry) ks.getEntry(alias, null);
|
||||
Certificate cert = certEntry.getTrustedCertificate();
|
||||
// HttpRequest.addCert(cert);
|
||||
}
|
||||
}
|
||||
|
||||
private void loadSSLCertsFromBundle() throws GeneralSecurityException, IOException {
|
||||
AssetManager assetManager = cordova.getActivity().getAssets();
|
||||
String[] files = assetManager.list("www/certificates");
|
||||
ArrayList<String> cerFiles = new ArrayList<String>();
|
||||
|
||||
for (int i = 0; i < files.length; i++) {
|
||||
int index = files[i].lastIndexOf('.');
|
||||
if (index != -1) {
|
||||
if (files[i].substring(index).equals(".cer")) {
|
||||
cerFiles.add("www/certificates/" + files[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < cerFiles.size(); i++) {
|
||||
InputStream in = cordova.getActivity().getAssets().open(cerFiles.get(i));
|
||||
InputStream caInput = new BufferedInputStream(in);
|
||||
// HttpRequest.addCert(caInput);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,10 @@
|
||||
package com.silkimen.cordovahttp;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import javax.net.ssl.SSLHandshakeException;
|
||||
@@ -10,16 +12,18 @@ import javax.net.ssl.SSLHandshakeException;
|
||||
import com.silkimen.http.HttpBodyDecoder;
|
||||
import com.silkimen.http.HttpRequest;
|
||||
import com.silkimen.http.HttpRequest.HttpRequestException;
|
||||
import com.silkimen.http.HttpResponse;
|
||||
import com.silkimen.http.JsonUtils;
|
||||
|
||||
import org.apache.cordova.CallbackContext;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
public class CordovaHttpRequest implements Runnable {
|
||||
private static final String TAG = "Cordova-Plugin-HTTP";
|
||||
|
||||
private String method;
|
||||
private String url;
|
||||
private String serializer = "none";
|
||||
@@ -54,7 +58,7 @@ public class CordovaHttpRequest implements Runnable {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
HttpResponse response = new HttpResponse();
|
||||
CordovaHttpResponse response = new CordovaHttpResponse();
|
||||
|
||||
try {
|
||||
String processedUrl = HttpRequest.encode(HttpRequest.append(this.url, JsonUtils.getObjectMap(this.params)));
|
||||
@@ -82,33 +86,31 @@ public class CordovaHttpRequest implements Runnable {
|
||||
|
||||
if (request.code() >= 200 && request.code() < 300) {
|
||||
response.setBody(decodedBody);
|
||||
this.callbackContext.success(response.toJSON());
|
||||
} else {
|
||||
response.setErrorMessage(decodedBody);
|
||||
this.callbackContext.error(response.toJSON());
|
||||
}
|
||||
} catch (HttpRequestException e) {
|
||||
if (e.getCause() instanceof SSLHandshakeException) {
|
||||
response.setStatus(-2);
|
||||
response.setErrorMessage("SSL handshake failed: " + e.getMessage());
|
||||
Log.w("Cordova-Plugin-HTTP", "SSL handshake failed", e);
|
||||
Log.w(TAG, "SSL handshake failed", e);
|
||||
} else if (e.getCause() instanceof UnknownHostException) {
|
||||
response.setStatus(-3);
|
||||
response.setErrorMessage("Host could not be resolved: " + e.getMessage());
|
||||
Log.w("Cordova-Plugin-HTTP", "Host could not be resolved", e);
|
||||
Log.w(TAG, "Host could not be resolved", e);
|
||||
} else if (e.getCause() instanceof SocketTimeoutException) {
|
||||
response.setStatus(-4);
|
||||
response.setErrorMessage("Request timed out: " + e.getMessage());
|
||||
Log.w("Cordova-Plugin-HTTP", "Request timed out", e);
|
||||
Log.w(TAG, "Request timed out", e);
|
||||
} else {
|
||||
response.setStatus(-1);
|
||||
response.setErrorMessage("There was an error with the request: " + e.getCause().getMessage());
|
||||
Log.w("Cordova-Plugin-HTTP", "Generic request error", e);
|
||||
Log.w(TAG, "Generic request error", e);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
response.setStatus(-1);
|
||||
response.setErrorMessage(e.getMessage());
|
||||
Log.e("Cordova-Plugin-HTTP", "An unexpected error occured", e);
|
||||
Log.e(TAG, "An unexpected error occured", e);
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -118,7 +120,7 @@ public class CordovaHttpRequest implements Runnable {
|
||||
this.callbackContext.success(response.toJSON());
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
Log.e("Cordova-Plugin-HTTP", "An unexpected error occured while processing HTTP response", e);
|
||||
Log.e(TAG, "An unexpected error occured while processing HTTP response", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.silkimen.http;
|
||||
package com.silkimen.cordovahttp;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@@ -10,12 +10,14 @@ import org.json.JSONObject;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
public class HttpResponse {
|
||||
public class CordovaHttpResponse {
|
||||
private int status;
|
||||
private String url;
|
||||
private Map<String, List<String>> headers;
|
||||
private String body;
|
||||
private boolean failed;
|
||||
private JSONObject fileEntry;
|
||||
private boolean hasFailed;
|
||||
private boolean isFileOperation;
|
||||
private String error;
|
||||
|
||||
public void setStatus(int status) {
|
||||
@@ -34,13 +36,18 @@ public class HttpResponse {
|
||||
this.body = body;
|
||||
}
|
||||
|
||||
public void setFileEntry(JSONObject entry) {
|
||||
this.isFileOperation = true;
|
||||
this.fileEntry = entry;
|
||||
}
|
||||
|
||||
public void setErrorMessage(String message) {
|
||||
this.failed = true;
|
||||
this.hasFailed = true;
|
||||
this.error = message;
|
||||
}
|
||||
|
||||
public boolean hasFailed() {
|
||||
return this.failed;
|
||||
return this.hasFailed;
|
||||
}
|
||||
|
||||
public JSONObject toJSON() throws JSONException {
|
||||
@@ -49,8 +56,11 @@ public class HttpResponse {
|
||||
json.put("status", this.status);
|
||||
json.put("url", this.url);
|
||||
|
||||
if (this.failed) {
|
||||
if (this.hasFailed) {
|
||||
json.put("error", this.error);
|
||||
} else if (this.isFileOperation) {
|
||||
json.put("headers", new JSONObject(getFilteredHeaders()));
|
||||
json.put("file", this.fileEntry);
|
||||
} else {
|
||||
json.put("headers", new JSONObject(getFilteredHeaders()));
|
||||
json.put("data", this.body);
|
||||
128
src/android/com/silkimen/cordovahttp/CordovaHttpUpload.java
Normal file
128
src/android/com/silkimen/cordovahttp/CordovaHttpUpload.java
Normal file
@@ -0,0 +1,128 @@
|
||||
package com.silkimen.cordovahttp;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import javax.net.ssl.SSLHandshakeException;
|
||||
|
||||
import com.silkimen.http.HttpBodyDecoder;
|
||||
import com.silkimen.http.HttpRequest;
|
||||
import com.silkimen.http.HttpRequest.HttpRequestException;
|
||||
import com.silkimen.http.JsonUtils;
|
||||
|
||||
import org.apache.cordova.CallbackContext;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import android.webkit.MimeTypeMap;
|
||||
import android.util.Log;
|
||||
|
||||
class CordovaHttpUpload implements Runnable {
|
||||
private static final String TAG = "Cordova-Plugin-HTTP";
|
||||
|
||||
private String url;
|
||||
private JSONObject params;
|
||||
private JSONObject headers;
|
||||
private String filePath;
|
||||
private String uploadName;
|
||||
private int timeout;
|
||||
private CallbackContext callbackContext;
|
||||
|
||||
public CordovaHttpUpload(String url, JSONObject params, JSONObject headers, String filePath, String uploadName,
|
||||
int timeout, CallbackContext callbackContext) {
|
||||
|
||||
this.url = url;
|
||||
this.params = params;
|
||||
this.headers = headers;
|
||||
this.filePath = filePath;
|
||||
this.uploadName = uploadName;
|
||||
this.timeout = timeout;
|
||||
this.callbackContext = callbackContext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
CordovaHttpResponse response = new CordovaHttpResponse();
|
||||
|
||||
try {
|
||||
String processedUrl = HttpRequest.encode(HttpRequest.append(this.url, JsonUtils.getObjectMap(this.params)));
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
|
||||
HttpRequest request = new HttpRequest(processedUrl, "POST")
|
||||
.followRedirects(true /* @TODO */)
|
||||
.readTimeout(this.timeout)
|
||||
.acceptCharset("UTF-8")
|
||||
.uncompress(true)
|
||||
.headers(JsonUtils.getStringMap(this.headers));
|
||||
|
||||
int filenameIndex = filePath.lastIndexOf('/');
|
||||
String filename = filePath.substring(filenameIndex + 1);
|
||||
|
||||
int extIndex = filePath.lastIndexOf('.');
|
||||
String ext = filePath.substring(extIndex + 1);
|
||||
|
||||
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
|
||||
String mimeType = mimeTypeMap.getMimeTypeFromExtension(ext);
|
||||
|
||||
request.part(this.uploadName, filename, mimeType, new File(new URI(this.filePath)))
|
||||
.receive(outputStream);
|
||||
|
||||
ByteBuffer rawOutput = ByteBuffer.wrap(outputStream.toByteArray());
|
||||
String decodedBody = HttpBodyDecoder.decodeBody(rawOutput, request.charset());
|
||||
|
||||
response.setStatus(request.code());
|
||||
response.setUrl(request.url().toString());
|
||||
response.setHeaders(request.headers());
|
||||
|
||||
if (request.code() >= 200 && request.code() < 300) {
|
||||
response.setBody(decodedBody);
|
||||
} else {
|
||||
response.setErrorMessage(decodedBody);
|
||||
}
|
||||
} catch (HttpRequestException e) {
|
||||
if (e.getCause() instanceof SSLHandshakeException) {
|
||||
response.setStatus(-2);
|
||||
response.setErrorMessage("SSL handshake failed: " + e.getMessage());
|
||||
Log.w(TAG, "SSL handshake failed", e);
|
||||
} else if (e.getCause() instanceof UnknownHostException) {
|
||||
response.setStatus(-3);
|
||||
response.setErrorMessage("Host could not be resolved: " + e.getMessage());
|
||||
Log.w(TAG, "Host could not be resolved", e);
|
||||
} else if (e.getCause() instanceof SocketTimeoutException) {
|
||||
response.setStatus(-4);
|
||||
response.setErrorMessage("Request timed out: " + e.getMessage());
|
||||
Log.w(TAG, "Request timed out", e);
|
||||
} else {
|
||||
response.setStatus(-1);
|
||||
response.setErrorMessage("There was an error with the request: " + e.getCause().getMessage());
|
||||
Log.w(TAG, "Generic request error", e);
|
||||
}
|
||||
} catch (URISyntaxException e) {
|
||||
response.setStatus(-1);
|
||||
response.setErrorMessage("An error occured while loading file");
|
||||
Log.e(TAG, "An error occured while loading file", e);
|
||||
} catch (Exception e) {
|
||||
response.setStatus(-1);
|
||||
response.setErrorMessage(e.getMessage());
|
||||
Log.e(TAG, "An unexpected error occured", e);
|
||||
}
|
||||
|
||||
try {
|
||||
if (response.hasFailed()) {
|
||||
this.callbackContext.error(response.toJSON());
|
||||
} else {
|
||||
this.callbackContext.success(response.toJSON());
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
Log.e(TAG, "An unexpected error occured while processing HTTP response", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,66 +0,0 @@
|
||||
/**
|
||||
* A HTTP plugin for Cordova / Phonegap
|
||||
*/
|
||||
package com.synconset.cordovahttp;
|
||||
|
||||
import com.silkimen.http.HttpRequest;
|
||||
import com.silkimen.http.HttpRequest.HttpRequestException;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.net.UnknownHostException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
import javax.net.ssl.SSLHandshakeException;
|
||||
|
||||
import org.apache.cordova.CallbackContext;
|
||||
import org.apache.cordova.file.FileUtils;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
class CordovaHttpDownload extends CordovaHttp implements Runnable {
|
||||
private String filePath;
|
||||
|
||||
public CordovaHttpDownload(String urlString, Object params, JSONObject headers, String filePath, int timeout, CallbackContext callbackContext) {
|
||||
super(urlString, params, headers, timeout, callbackContext);
|
||||
this.filePath = filePath;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
HttpRequest request = HttpRequest.get(this.getUrlString(), this.getParamsMap(), false);
|
||||
|
||||
this.prepareRequest(request);
|
||||
|
||||
JSONObject response = new JSONObject();
|
||||
int code = request.code();
|
||||
|
||||
response.put("status", code);
|
||||
response.put("url", request.url().toString());
|
||||
this.addResponseHeaders(request, response);
|
||||
|
||||
if (code >= 200 && code < 300) {
|
||||
URI uri = new URI(filePath);
|
||||
File file = new File(uri);
|
||||
request.receive(file);
|
||||
JSONObject fileEntry = FileUtils.getFilePlugin().getEntryForFile(file);
|
||||
response.put("file", fileEntry);
|
||||
this.getCallbackContext().success(response);
|
||||
} else {
|
||||
response.put("error", "There was an error downloading the file");
|
||||
this.getCallbackContext().error(response);
|
||||
}
|
||||
} catch(URISyntaxException e) {
|
||||
this.respondWithError("There was an error with the given filePath");
|
||||
} catch (JSONException e) {
|
||||
this.respondWithError("There was an error generating the response");
|
||||
} catch (HttpRequestException e) {
|
||||
this.handleHttpRequestException(e);
|
||||
} catch (Exception e) {
|
||||
this.respondWithError(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,206 +0,0 @@
|
||||
/**
|
||||
* A HTTP plugin for Cordova / Phonegap
|
||||
*/
|
||||
package com.synconset.cordovahttp;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.security.KeyStore;
|
||||
import java.security.KeyStore.TrustedCertificateEntry;
|
||||
import java.security.cert.Certificate;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Enumeration;
|
||||
|
||||
import org.apache.cordova.CallbackContext;
|
||||
import org.apache.cordova.CordovaInterface;
|
||||
import org.apache.cordova.CordovaPlugin;
|
||||
import org.apache.cordova.CordovaWebView;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import android.content.res.AssetManager;
|
||||
|
||||
import com.silkimen.http.HttpRequest;
|
||||
import com.silkimen.cordovahttp.CordovaHttpRequest;
|
||||
|
||||
public class CordovaHttpPlugin extends CordovaPlugin {
|
||||
private static final String TAG = "CordovaHTTP";
|
||||
|
||||
@Override
|
||||
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
|
||||
super.initialize(cordova, webView);
|
||||
|
||||
try {
|
||||
//HttpRequest.clearCerts();
|
||||
this.pinSSLCertsFromCAStore();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
System.err.println("There was an error loading system's CA certificates");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean execute(String action, final JSONArray args, final CallbackContext callbackContext) throws JSONException {
|
||||
if (action.equals("post")) {
|
||||
String url = args.getString(0);
|
||||
Object data = args.get(1);
|
||||
String serializer = args.getString(2);
|
||||
JSONObject headers = args.getJSONObject(3);
|
||||
int timeout = args.getInt(4) * 1000;
|
||||
|
||||
CordovaHttpRequest post = new CordovaHttpRequest("POST", url, serializer, data, headers, timeout, callbackContext);
|
||||
|
||||
cordova.getThreadPool().execute(post);
|
||||
} else if (action.equals("get")) {
|
||||
String url = args.getString(0);
|
||||
JSONObject params = args.getJSONObject(1);
|
||||
JSONObject headers = args.getJSONObject(2);
|
||||
int timeout = args.getInt(3) * 1000;
|
||||
|
||||
CordovaHttpRequest get = new CordovaHttpRequest("GET", url, params, headers, timeout, callbackContext);
|
||||
|
||||
cordova.getThreadPool().execute(get);
|
||||
} else if (action.equals("put")) {
|
||||
String url = args.getString(0);
|
||||
Object data = args.get(1);
|
||||
String serializer = args.getString(2);
|
||||
JSONObject headers = args.getJSONObject(3);
|
||||
int timeout = args.getInt(4) * 1000;
|
||||
|
||||
CordovaHttpRequest put = new CordovaHttpRequest("PUT", url, serializer, data, headers, timeout, callbackContext);
|
||||
|
||||
cordova.getThreadPool().execute(put);
|
||||
} else if (action.equals("patch")) {
|
||||
String url = args.getString(0);
|
||||
Object data = args.get(1);
|
||||
String serializer = args.getString(2);
|
||||
JSONObject headers = args.getJSONObject(3);
|
||||
int timeout = args.getInt(4) * 1000;
|
||||
|
||||
CordovaHttpRequest patch = new CordovaHttpRequest("PATCH", url, serializer, data, headers, timeout, callbackContext);
|
||||
|
||||
cordova.getThreadPool().execute(patch);
|
||||
}
|
||||
else if (action.equals("delete")) {
|
||||
String url = args.getString(0);
|
||||
JSONObject params = args.getJSONObject(1);
|
||||
JSONObject headers = args.getJSONObject(2);
|
||||
int timeout = args.getInt(3) * 1000;
|
||||
|
||||
CordovaHttpRequest delete = new CordovaHttpRequest("DELETE", url, params, headers, timeout, callbackContext);
|
||||
|
||||
cordova.getThreadPool().execute(delete);
|
||||
} else if (action.equals("head")) {
|
||||
String url = args.getString(0);
|
||||
JSONObject params = args.getJSONObject(1);
|
||||
JSONObject headers = args.getJSONObject(2);
|
||||
int timeout = args.getInt(3) * 1000;
|
||||
|
||||
CordovaHttpRequest head = new CordovaHttpRequest("HEAD", url, params, headers, timeout, callbackContext);
|
||||
|
||||
cordova.getThreadPool().execute(head);
|
||||
} else if (action.equals("setSSLCertMode")) {
|
||||
String mode = args.getString(0);
|
||||
|
||||
//HttpRequest.clearCerts();
|
||||
|
||||
if (mode.equals("legacy")) {
|
||||
//HttpRequest.setSSLCertMode(HttpRequest.CERT_MODE_DEFAULT);
|
||||
callbackContext.success();
|
||||
} else if (mode.equals("nocheck")) {
|
||||
//HttpRequest.setSSLCertMode(HttpRequest.CERT_MODE_TRUSTALL);
|
||||
callbackContext.success();
|
||||
} else if (mode.equals("pinned")) {
|
||||
try {
|
||||
this.loadSSLCertsFromBundle();
|
||||
//HttpRequest.setSSLCertMode(HttpRequest.CERT_MODE_PINNED);
|
||||
callbackContext.success();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
callbackContext.error("There was an error setting up ssl pinning");
|
||||
}
|
||||
} else if (mode.equals("default")) {
|
||||
try {
|
||||
this.pinSSLCertsFromCAStore();
|
||||
callbackContext.success();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
callbackContext.error("There was an error loading system's CA certificates");
|
||||
}
|
||||
}
|
||||
} else if (action.equals("uploadFile")) {
|
||||
String url = args.getString(0);
|
||||
Object params = args.get(1);
|
||||
JSONObject headers = args.getJSONObject(2);
|
||||
String filePath = args.getString(3);
|
||||
String name = args.getString(4);
|
||||
int timeout = args.getInt(5) * 1000;
|
||||
|
||||
CordovaHttpUpload upload = new CordovaHttpUpload(url, params, headers, filePath, name, timeout, callbackContext);
|
||||
|
||||
cordova.getThreadPool().execute(upload);
|
||||
} else if (action.equals("downloadFile")) {
|
||||
String url = args.getString(0);
|
||||
Object params = args.get(1);
|
||||
JSONObject headers = args.getJSONObject(2);
|
||||
String filePath = args.getString(3);
|
||||
int timeout = args.getInt(4) * 1000;
|
||||
|
||||
CordovaHttpDownload download = new CordovaHttpDownload(url, params, headers, filePath, timeout, callbackContext);
|
||||
|
||||
cordova.getThreadPool().execute(download);
|
||||
} else if (action.equals("disableRedirect")) {
|
||||
boolean disable = args.getBoolean(0);
|
||||
CordovaHttp.disableRedirect(disable);
|
||||
callbackContext.success();
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private void pinSSLCertsFromCAStore() throws GeneralSecurityException, IOException {
|
||||
this.loadSSLCertsFromKeyStore("AndroidCAStore");
|
||||
//HttpRequest.setSSLCertMode(HttpRequest.CERT_MODE_PINNED);
|
||||
}
|
||||
|
||||
private void loadSSLCertsFromKeyStore(String storeType) throws GeneralSecurityException, IOException {
|
||||
KeyStore ks = KeyStore.getInstance(storeType);
|
||||
ks.load(null);
|
||||
Enumeration<String> aliases = ks.aliases();
|
||||
|
||||
while (aliases.hasMoreElements()) {
|
||||
String alias = aliases.nextElement();
|
||||
TrustedCertificateEntry certEntry = (TrustedCertificateEntry) ks.getEntry(alias, null);
|
||||
Certificate cert = certEntry.getTrustedCertificate();
|
||||
//HttpRequest.addCert(cert);
|
||||
}
|
||||
}
|
||||
|
||||
private void loadSSLCertsFromBundle() throws GeneralSecurityException, IOException {
|
||||
AssetManager assetManager = cordova.getActivity().getAssets();
|
||||
String[] files = assetManager.list("www/certificates");
|
||||
ArrayList<String> cerFiles = new ArrayList<String>();
|
||||
|
||||
for (int i = 0; i < files.length; i++) {
|
||||
int index = files[i].lastIndexOf('.');
|
||||
if (index != -1) {
|
||||
if (files[i].substring(index).equals(".cer")) {
|
||||
cerFiles.add("www/certificates/" + files[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < cerFiles.size(); i++) {
|
||||
InputStream in = cordova.getActivity().getAssets().open(cerFiles.get(i));
|
||||
InputStream caInput = new BufferedInputStream(in);
|
||||
//HttpRequest.addCert(caInput);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,87 +0,0 @@
|
||||
/**
|
||||
* A HTTP plugin for Cordova / Phonegap
|
||||
*/
|
||||
package com.synconset.cordovahttp;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.net.UnknownHostException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.cordova.CallbackContext;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import javax.net.ssl.SSLHandshakeException;
|
||||
|
||||
import android.webkit.MimeTypeMap;
|
||||
|
||||
import com.silkimen.http.HttpRequest;
|
||||
import com.silkimen.http.HttpRequest.HttpRequestException;
|
||||
|
||||
class CordovaHttpUpload extends CordovaHttp implements Runnable {
|
||||
private String filePath;
|
||||
private String name;
|
||||
|
||||
public CordovaHttpUpload(String urlString, Object params, JSONObject headers, String filePath, String name, int timeout, CallbackContext callbackContext) {
|
||||
super(urlString, params, headers, timeout, callbackContext);
|
||||
this.filePath = filePath;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
HttpRequest request = HttpRequest.post(this.getUrlString());
|
||||
|
||||
this.prepareRequest(request);
|
||||
|
||||
URI uri = new URI(filePath);
|
||||
|
||||
int filenameIndex = filePath.lastIndexOf('/');
|
||||
String filename = filePath.substring(filenameIndex + 1);
|
||||
|
||||
int extIndex = filePath.lastIndexOf('.');
|
||||
String ext = filePath.substring(extIndex + 1);
|
||||
|
||||
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
|
||||
String mimeType = mimeTypeMap.getMimeTypeFromExtension(ext);
|
||||
|
||||
Set<?> set = (Set<?>)this.getParamsMap().entrySet();
|
||||
Iterator<?> i = set.iterator();
|
||||
|
||||
while (i.hasNext()) {
|
||||
Entry<?, ?> e = (Entry<?, ?>)i.next();
|
||||
String key = (String)e.getKey();
|
||||
Object value = e.getValue();
|
||||
if (value instanceof Number) {
|
||||
request.part(key, (Number)value);
|
||||
} else if (value instanceof String) {
|
||||
request.part(key, (String)value);
|
||||
} else {
|
||||
this.respondWithError("All parameters must be Numbers or Strings");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
request.part(this.name, filename, mimeType, new File(uri));
|
||||
|
||||
this.returnResponseObject(request);
|
||||
} catch (URISyntaxException e) {
|
||||
this.respondWithError("There was an error loading the file");
|
||||
} catch (JSONException e) {
|
||||
this.respondWithError("There was an error generating the response");
|
||||
} catch (HttpRequestException e) {
|
||||
this.handleHttpRequestException(e);
|
||||
} catch (Exception e) {
|
||||
this.respondWithError(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user