mirror of
https://github.com/silkimen/cordova-plugin-advanced-http.git
synced 2026-05-31 00:00:07 +08:00
Now using http-request for android. Android is now fully working
This commit is contained in:
+3
-2
@@ -80,8 +80,9 @@
|
||||
<source-file src="src/Android/com/synconset/CordovaHTTP/CordovaHttp.java" target-dir="src/com/synconset" />
|
||||
<source-file src="src/Android/com/synconset/CordovaHTTP/CordovaHttpGet.java" target-dir="src/com/synconset" />
|
||||
<source-file src="src/Android/com/synconset/CordovaHTTP/CordovaHttpPost.java" target-dir="src/com/synconset" />
|
||||
<source-file src="src/Android/com/synconset/CordovaHTTP/CordovaHttpUpload.java" target-dir="src/com/synconset" />
|
||||
<source-file src="src/Android/com/synconset/CordovaHTTP/CordovaHttpDownload.java" target-dir="src/com/synconset" />
|
||||
<source-file src="src/Android/com/synconset/CordovaHTTP/CordovaHttpPlugin.java" target-dir="src/com/synconset" />
|
||||
<source-file src="src/Android/com/synconset/CordovaHTTP/VeryTrustingTrustManager.java" target-dir="src/com/synconset" />
|
||||
<source-file src="src/Android/com/synconset/CordovaHTTP/VeryTrustingHostnameVerifier.java" target-dir="src/com/synconset" />
|
||||
<source-file src="src/Android/com/synconset/CordovaHTTP/HttpRequest.java" target-dir="src/com/github/kevinsawicki/http" />
|
||||
</platform>
|
||||
</plugin>
|
||||
@@ -12,6 +12,7 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.BufferedReader;
|
||||
import java.util.Map;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
@@ -25,25 +26,39 @@ import java.util.Iterator;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
public class CordovaHttp {
|
||||
public abstract class CordovaHttp {
|
||||
protected static final String TAG = "CordovaHTTP";
|
||||
protected static final String CHARSET = "UTF-8";
|
||||
|
||||
protected String charset = "UTF-8";
|
||||
private static boolean sslPinning;
|
||||
private static boolean acceptAllCerts;
|
||||
|
||||
private String urlString;
|
||||
private JSONObject params;
|
||||
private JSONObject headers;
|
||||
private SSLContext sslContext;
|
||||
private HostnameVerifier hostnameVerifier;
|
||||
private Map<?, ?> params;
|
||||
private Map<String, String> headers;
|
||||
private CallbackContext callbackContext;
|
||||
|
||||
public CordovaHttp(String urlString, JSONObject params, JSONObject headers, SSLContext sslContext, HostnameVerifier hostnameVerifier, CallbackContext callbackContext) {
|
||||
public CordovaHttp(String urlString, Map<?, ?> params, Map<String, String> headers, CallbackContext callbackContext) {
|
||||
this.urlString = urlString;
|
||||
this.params = params;
|
||||
this.headers = headers;
|
||||
this.sslContext = sslContext;
|
||||
this.sslPinning = sslPinning;
|
||||
this.acceptAllCerts = acceptAllCerts;
|
||||
this.callbackContext = callbackContext;
|
||||
this.hostnameVerifier = hostnameVerifier;
|
||||
}
|
||||
|
||||
public static void enableSSLPinning(boolean enable) {
|
||||
sslPinning = enable;
|
||||
if (sslPinning) {
|
||||
acceptAllCerts = false;
|
||||
}
|
||||
}
|
||||
|
||||
public static void acceptAllCerts(boolean accept) {
|
||||
acceptAllCerts = accept;
|
||||
if (acceptAllCerts) {
|
||||
sslPinning = false;
|
||||
}
|
||||
}
|
||||
|
||||
protected String getUrlString() {
|
||||
@@ -54,19 +69,19 @@ public class CordovaHttp {
|
||||
this.urlString = urlString;
|
||||
}
|
||||
|
||||
protected JSONObject getParams() {
|
||||
protected Map<?, ?> getParams() {
|
||||
return this.params;
|
||||
}
|
||||
|
||||
protected void setParams(JSONObject params) {
|
||||
protected void setParams(Map<?, ?> params) {
|
||||
this.params = params;
|
||||
}
|
||||
|
||||
protected JSONObject getHeaders() {
|
||||
protected Map<String, String> getHeaders() {
|
||||
return this.headers;
|
||||
}
|
||||
|
||||
protected void setHeaders(JSONObject headers) {
|
||||
protected void setHeaders(Map<String, String> headers) {
|
||||
this.headers = headers;
|
||||
}
|
||||
|
||||
@@ -74,74 +89,22 @@ public class CordovaHttp {
|
||||
return this.callbackContext;
|
||||
}
|
||||
|
||||
protected HttpsURLConnection openConnection(String urlString) throws MalformedURLException, IOException {
|
||||
URL url = new URL(urlString);
|
||||
HttpsURLConnection conn = (HttpsURLConnection)url.openConnection();
|
||||
if (this.hostnameVerifier != null) {
|
||||
conn.setHostnameVerifier(this.hostnameVerifier);
|
||||
}
|
||||
if (this.sslContext != null) {
|
||||
conn.setSSLSocketFactory(this.sslContext.getSocketFactory());
|
||||
}
|
||||
return conn;
|
||||
protected boolean sslPinning() {
|
||||
return sslPinning;
|
||||
}
|
||||
|
||||
protected void addHeaders(URLConnection conn) throws JSONException {
|
||||
Iterator<?> i = this.headers.keys();
|
||||
|
||||
Log.d(TAG, this.headers.toString(3));
|
||||
while (i.hasNext()) {
|
||||
String key = (String)i.next();
|
||||
conn.setRequestProperty(key, this.headers.getString(key));
|
||||
}
|
||||
protected boolean acceptAllCerts() {
|
||||
return acceptAllCerts;
|
||||
}
|
||||
|
||||
protected String getQueryString() throws JSONException {
|
||||
Iterator<?> i = this.params.keys();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
boolean first = true;
|
||||
|
||||
while (i.hasNext()) {
|
||||
String key = (String)i.next();
|
||||
|
||||
if (!first) {
|
||||
sb.append("&");
|
||||
} else {
|
||||
first = false;
|
||||
}
|
||||
sb.append(key);
|
||||
sb.append("=");
|
||||
sb.append(this.params.getString(key));
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
protected String readInputStream(InputStream is) throws IOException {
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
try {
|
||||
String line = reader.readLine();
|
||||
while (line != null) {
|
||||
sb.append(line);
|
||||
line = reader.readLine();
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
} finally {
|
||||
reader.close();
|
||||
}
|
||||
}
|
||||
|
||||
protected void respondWithError(CallbackContext callbackContext, String msg) {
|
||||
protected void respondWithError(String msg) {
|
||||
try {
|
||||
JSONObject response = new JSONObject();
|
||||
response.put("status", 500);
|
||||
response.put("error", msg);
|
||||
callbackContext.error(response);
|
||||
this.callbackContext.error(response);
|
||||
} catch (JSONException e) {
|
||||
callbackContext.error(msg);
|
||||
this.callbackContext.error(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
/**
|
||||
* A HTTP plugin for Cordova / Phonegap
|
||||
*/
|
||||
package com.synconset;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import com.github.kevinsawicki.http.HttpRequest;
|
||||
import com.github.kevinsawicki.http.HttpRequest.HttpRequestException;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.cordova.CallbackContext;
|
||||
import org.apache.cordova.file.FileUtils;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
public class CordovaHttpDownload extends CordovaHttp implements Runnable {
|
||||
private String filePath;
|
||||
|
||||
public CordovaHttpDownload(String urlString, Map<?, ?> params, Map<String, String> headers, CallbackContext callbackContext, String filePath) {
|
||||
super(urlString, params, headers, callbackContext);
|
||||
this.filePath = filePath;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
HttpRequest request = HttpRequest.get(this.getUrlString(), this.getParams(), true);
|
||||
if (this.acceptAllCerts()) {
|
||||
request.trustAllCerts();
|
||||
request.trustAllHosts();
|
||||
}
|
||||
if (this.sslPinning()) {
|
||||
request.pinToCerts();
|
||||
}
|
||||
request.acceptCharset(CHARSET);
|
||||
request.headers(this.getHeaders());
|
||||
int code = request.code();
|
||||
|
||||
JSONObject response = new JSONObject();
|
||||
response.put("status", code);
|
||||
if (code >= 200 && code < 300) {
|
||||
URI uri = new URI(filePath);
|
||||
File file = new File(uri);
|
||||
request.receive(file);
|
||||
JSONObject fileEntry = FileUtils.getEntry(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) {
|
||||
Log.d(TAG, e.getMessage());
|
||||
this.respondWithError("There was an error with the request");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
import javax.net.ssl.SSLContext;
|
||||
@@ -17,61 +18,48 @@ import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import com.github.kevinsawicki.http.HttpRequest;
|
||||
import com.github.kevinsawicki.http.HttpRequest.HttpRequestException;
|
||||
|
||||
public class CordovaHttpGet extends CordovaHttp implements Runnable {
|
||||
public CordovaHttpGet(String urlString, JSONObject params, JSONObject headers, SSLContext sslContext, HostnameVerifier hostnameVerifier, CallbackContext callbackContext) {
|
||||
super(urlString, params, headers, sslContext, hostnameVerifier, callbackContext);
|
||||
public CordovaHttpGet(String urlString, Map<?, ?> params, Map<String, String> headers, CallbackContext callbackContext) {
|
||||
super(urlString, params, headers, callbackContext);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
JSONObject params = this.getParams();
|
||||
String urlString = this.getUrlString();
|
||||
CallbackContext callbackContext = this.getCallbackContext();
|
||||
|
||||
InputStream is = null;
|
||||
HttpsURLConnection conn = null;
|
||||
try {
|
||||
if (params.length() > 0) {
|
||||
urlString = urlString + "?" + this.getQueryString();
|
||||
HttpRequest request = HttpRequest.get(this.getUrlString(), this.getParams(), true);
|
||||
if (this.acceptAllCerts()) {
|
||||
request.trustAllCerts();
|
||||
request.trustAllHosts();
|
||||
}
|
||||
conn = this.openConnection(urlString);
|
||||
conn.setRequestMethod("GET");
|
||||
conn.setDoInput(true);
|
||||
conn.setRequestProperty("Accept-Charset", charset);
|
||||
this.addHeaders(conn);
|
||||
conn.connect();
|
||||
int status = conn.getResponseCode();
|
||||
if (status >= 200 && status < 300) {
|
||||
is = conn.getInputStream();
|
||||
String responseData = this.readInputStream(is);
|
||||
JSONObject response = new JSONObject();
|
||||
response.put("status", status);
|
||||
response.put("data", responseData);
|
||||
callbackContext.success(response);
|
||||
if (this.sslPinning()) {
|
||||
request.pinToCerts();
|
||||
}
|
||||
request.acceptCharset(CHARSET);
|
||||
request.headers(this.getHeaders());
|
||||
int code = request.code();
|
||||
String body = request.body(CHARSET);
|
||||
|
||||
Log.d(TAG, Integer.toString(code));
|
||||
Log.d(TAG, body);
|
||||
|
||||
JSONObject response = new JSONObject();
|
||||
response.put("status", code);
|
||||
if (code >= 200 && code < 300) {
|
||||
response.put("data", body);
|
||||
this.getCallbackContext().success(response);
|
||||
} else {
|
||||
is = conn.getErrorStream();
|
||||
String responseData = this.readInputStream(is);
|
||||
JSONObject response = new JSONObject();
|
||||
response.put("status", status);
|
||||
response.put("error", responseData);
|
||||
callbackContext.error(response);
|
||||
response.put("error", body);
|
||||
this.getCallbackContext().error(response);
|
||||
}
|
||||
} catch (MalformedURLException e) {
|
||||
this.respondWithError(callbackContext, "There is an error with the url");
|
||||
} catch (JSONException e) {
|
||||
this.respondWithError(callbackContext, "There was an error with the params, headers or generating the response");
|
||||
} catch (IOException e) {
|
||||
this.respondWithError(callbackContext, "There was an error with the request");
|
||||
} finally {
|
||||
if (is != null) {
|
||||
try {
|
||||
is.close();
|
||||
} catch (IOException e) {}
|
||||
}
|
||||
if (conn != null) {
|
||||
conn.disconnect();
|
||||
}
|
||||
this.respondWithError("There was an error generating the response");
|
||||
} catch (HttpRequestException e) {
|
||||
Log.d(TAG, e.getMessage());
|
||||
this.respondWithError("There was an error with the request");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,17 +6,18 @@ package com.synconset;
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.security.KeyManagementException;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.security.KeyStore;
|
||||
import java.security.KeyStoreException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.cert.Certificate;
|
||||
import java.security.cert.CertificateException;
|
||||
import java.security.cert.CertificateFactory;
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.util.Iterator;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.TrustManagerFactory;
|
||||
import javax.net.ssl.TrustManager;
|
||||
@@ -33,20 +34,18 @@ import org.json.JSONObject;
|
||||
import android.content.res.AssetManager;
|
||||
import android.util.Base64;
|
||||
import android.util.Log;
|
||||
|
||||
|
||||
import com.github.kevinsawicki.http.HttpRequest;
|
||||
|
||||
public class CordovaHttpPlugin extends CordovaPlugin {
|
||||
private static final String TAG = "CordovaHTTP";
|
||||
|
||||
private SSLContext sslContext;
|
||||
private HostnameVerifier hostnameVerifier;
|
||||
private JSONObject globalHeaders;
|
||||
private HashMap<String, String> globalHeaders;
|
||||
|
||||
@Override
|
||||
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
|
||||
super.initialize(cordova, webView);
|
||||
this.globalHeaders = new JSONObject();
|
||||
this.sslContext = null;
|
||||
this.hostnameVerifier = null;
|
||||
this.globalHeaders = new HashMap<String, String>();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -55,113 +54,121 @@ public class CordovaHttpPlugin extends CordovaPlugin {
|
||||
String urlString = args.getString(0);
|
||||
JSONObject params = args.getJSONObject(1);
|
||||
JSONObject headers = args.getJSONObject(2);
|
||||
this.addToJSONObject(headers, this.globalHeaders);
|
||||
CordovaHttpGet get = new CordovaHttpGet(urlString, params, headers, this.sslContext, this.hostnameVerifier, callbackContext);
|
||||
HashMap<?, ?> paramsMap = this.getMapFromJSONObject(params);
|
||||
HashMap<String, String> headersMap = this.addToMap(this.globalHeaders, headers);
|
||||
CordovaHttpGet get = new CordovaHttpGet(urlString, paramsMap, headersMap, callbackContext);
|
||||
cordova.getThreadPool().execute(get);
|
||||
} else if (action.equals("post")) {
|
||||
String urlString = args.getString(0);
|
||||
JSONObject params = args.getJSONObject(1);
|
||||
JSONObject headers = args.getJSONObject(2);
|
||||
this.addToJSONObject(headers, this.globalHeaders);
|
||||
CordovaHttpPost post = new CordovaHttpPost(urlString, params, headers, this.sslContext, this.hostnameVerifier, callbackContext);
|
||||
HashMap<?, ?> paramsMap = this.getMapFromJSONObject(params);
|
||||
HashMap<String, String> headersMap = this.addToMap(this.globalHeaders, headers);
|
||||
CordovaHttpPost post = new CordovaHttpPost(urlString, paramsMap, headersMap, callbackContext);
|
||||
cordova.getThreadPool().execute(post);
|
||||
} else if (action.equals("setAuthorizationHeaderWithUsernameAndPassword")) {
|
||||
} else if (action.equals("useBasicAuth")) {
|
||||
String username = args.getString(0);
|
||||
String password = args.getString(1);
|
||||
this.setAuthorizationHeaderWithUsernameAndPassword(username, password);
|
||||
this.useBasicAuth(username, password);
|
||||
callbackContext.success();
|
||||
} else if (action.equals("enableSSLPinning")) {
|
||||
try {
|
||||
this.enableSSLPinning();
|
||||
boolean enable = args.getBoolean(0);
|
||||
this.enableSSLPinning(enable);
|
||||
callbackContext.success();
|
||||
} catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
Log.d(TAG, e.getMessage());
|
||||
callbackContext.error("There was an error setting up ssl pinning");
|
||||
}
|
||||
} else if (action.equals("allowInvalidCertificates")) {
|
||||
try {
|
||||
boolean allow = args.getBoolean(0);
|
||||
this.allowInvalidCertificates(allow);
|
||||
} catch(Exception e) {
|
||||
callbackContext.error("There was an error allowing or disallowing invalide certificates");
|
||||
}
|
||||
} else if (action.equals("acceptAllCerts")) {
|
||||
boolean accept = args.getBoolean(0);
|
||||
CordovaHttp.acceptAllCerts(accept);
|
||||
} else if (action.equals("setHeader")) {
|
||||
String header = args.getString(0);
|
||||
String value = args.getString(1);
|
||||
this.setHeader(header, value);
|
||||
callbackContext.success();
|
||||
} else if (action.equals("uploadFile")) {
|
||||
String urlString = args.getString(0);
|
||||
JSONObject params = args.getJSONObject(1);
|
||||
JSONObject headers = args.getJSONObject(2);
|
||||
HashMap<?, ?> paramsMap = this.getMapFromJSONObject(params);
|
||||
HashMap<String, String> headersMap = this.addToMap(this.globalHeaders, headers);
|
||||
String filePath = args.getString(3);
|
||||
String name = args.getString(4);
|
||||
CordovaHttpUpload upload = new CordovaHttpUpload(urlString, paramsMap, headersMap, callbackContext, filePath, name);
|
||||
cordova.getThreadPool().execute(upload);
|
||||
} else if (action.equals("downloadFile")) {
|
||||
String urlString = args.getString(0);
|
||||
JSONObject params = args.getJSONObject(1);
|
||||
JSONObject headers = args.getJSONObject(2);
|
||||
HashMap<?, ?> paramsMap = this.getMapFromJSONObject(params);
|
||||
HashMap<String, String> headersMap = this.addToMap(this.globalHeaders, headers);
|
||||
String filePath = args.getString(3);
|
||||
CordovaHttpDownload download = new CordovaHttpDownload(urlString, paramsMap, headersMap, callbackContext, filePath);
|
||||
cordova.getThreadPool().execute(download);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private void setAuthorizationHeaderWithUsernameAndPassword(String username, String password) throws JSONException {
|
||||
private void useBasicAuth(String username, String password) {
|
||||
String loginInfo = username + ":" + password;
|
||||
loginInfo = "Basic " + Base64.encodeToString(loginInfo.getBytes(), Base64.NO_WRAP);
|
||||
globalHeaders.put("Authorization", loginInfo);
|
||||
this.globalHeaders.put("Authorization", loginInfo);
|
||||
}
|
||||
|
||||
private void enableSSLPinning() throws CertificateException, IOException, KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
|
||||
// Create a KeyStore containing our trusted CAs
|
||||
String keyStoreType = KeyStore.getDefaultType();
|
||||
KeyStore keyStore = KeyStore.getInstance(keyStoreType);
|
||||
keyStore.load(null, null);
|
||||
|
||||
|
||||
AssetManager assetManager = cordova.getActivity().getAssets();
|
||||
String[] files = assetManager.list("");
|
||||
int index;
|
||||
ArrayList<String> cerFiles = new ArrayList<String>();
|
||||
for (int i = 0; i < files.length; i++) {
|
||||
index = files[i].lastIndexOf('.');
|
||||
if (index != -1) {
|
||||
if (files[i].substring(index).equals(".cer")) {
|
||||
cerFiles.add(files[i]);
|
||||
private void setHeader(String header, String value) {
|
||||
this.globalHeaders.put(header, value);
|
||||
}
|
||||
|
||||
private void enableSSLPinning(boolean enable) throws GeneralSecurityException, IOException {
|
||||
if (enable) {
|
||||
AssetManager assetManager = cordova.getActivity().getAssets();
|
||||
String[] files = assetManager.list("");
|
||||
int index;
|
||||
ArrayList<String> cerFiles = new ArrayList<String>();
|
||||
for (int i = 0; i < files.length; i++) {
|
||||
index = files[i].lastIndexOf('.');
|
||||
if (index != -1) {
|
||||
if (files[i].substring(index).equals(".cer")) {
|
||||
cerFiles.add(files[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
CertificateFactory cf = CertificateFactory.getInstance("X.509");
|
||||
for (int i = 0; i < cerFiles.size(); i++) {
|
||||
InputStream in = cordova.getActivity().getAssets().open(cerFiles.get(i));
|
||||
InputStream caInput = new BufferedInputStream(in);
|
||||
Certificate ca;
|
||||
try {
|
||||
ca = cf.generateCertificate(caInput);
|
||||
System.out.println("ca=" + ((X509Certificate) ca).getSubjectDN());
|
||||
} finally {
|
||||
caInput.close();
|
||||
}
|
||||
|
||||
keyStore.setCertificateEntry(cerFiles.get(i), ca);
|
||||
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);
|
||||
}
|
||||
CordovaHttp.enableSSLPinning(true);
|
||||
} else {
|
||||
CordovaHttp.enableSSLPinning(false);
|
||||
}
|
||||
|
||||
// Create a TrustManager that trusts the CAs in our KeyStore
|
||||
String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
|
||||
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
|
||||
tmf.init(keyStore);
|
||||
|
||||
// Create an SSLContext that uses our TrustManager
|
||||
sslContext = SSLContext.getInstance("TLS");
|
||||
sslContext.init(null, tmf.getTrustManagers(), null);
|
||||
hostnameVerifier = null;
|
||||
}
|
||||
|
||||
private void allowInvalidCertificates(boolean allow) throws NoSuchAlgorithmException, KeyManagementException {
|
||||
if (allow) {
|
||||
VeryTrustingTrustManager vttm = new VeryTrustingTrustManager();
|
||||
sslContext = SSLContext.getInstance("TLS");
|
||||
sslContext.init(null, new TrustManager[]{vttm}, null);
|
||||
hostnameVerifier = new VeryTrustingHostnameVerifier();
|
||||
} else {
|
||||
sslContext = null;
|
||||
hostnameVerifier = null;
|
||||
}
|
||||
}
|
||||
|
||||
private void addToJSONObject(JSONObject object, JSONObject objectToAdd) throws JSONException {
|
||||
Iterator<?> i = objectToAdd.keys();
|
||||
private HashMap<String, String> addToMap(HashMap<String, String> map, JSONObject object) throws JSONException {
|
||||
HashMap<String, String> newMap = (HashMap<String, String>)map.clone();
|
||||
Iterator<?> i = object.keys();
|
||||
|
||||
while (i.hasNext()) {
|
||||
String key = (String)i.next();
|
||||
if (!object.has(key)) {
|
||||
object.put(key, objectToAdd.getString(key));
|
||||
}
|
||||
newMap.put(key, object.getString(key));
|
||||
}
|
||||
return newMap;
|
||||
}
|
||||
|
||||
private HashMap<String, Object> getMapFromJSONObject(JSONObject object) throws JSONException {
|
||||
HashMap<String, Object> map = new HashMap<String, Object>();
|
||||
Iterator<?> i = object.keys();
|
||||
|
||||
while(i.hasNext()) {
|
||||
String key = (String)i.next();
|
||||
map.put(key, object.get(key));
|
||||
}
|
||||
return map;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,85 +3,61 @@
|
||||
*/
|
||||
package com.synconset;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.net.MalformedURLException;
|
||||
|
||||
import java.net.URL;
|
||||
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.HostnameVerifier;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.cordova.CallbackContext;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import com.github.kevinsawicki.http.HttpRequest;
|
||||
import com.github.kevinsawicki.http.HttpRequest.HttpRequestException;
|
||||
|
||||
public class CordovaHttpPost extends CordovaHttp implements Runnable {
|
||||
public CordovaHttpPost(String urlString, JSONObject params, JSONObject headers, SSLContext sslContext, HostnameVerifier hostnameVerifier, CallbackContext callbackContext) {
|
||||
super(urlString, params, headers, sslContext, hostnameVerifier, callbackContext);
|
||||
public CordovaHttpPost(String urlString, Map<?, ?> params, Map<String, String> headers, CallbackContext callbackContext) {
|
||||
super(urlString, params, headers, callbackContext);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
String urlString = this.getUrlString();
|
||||
CallbackContext callbackContext = this.getCallbackContext();
|
||||
|
||||
InputStream is = null;
|
||||
HttpsURLConnection conn = null;
|
||||
try {
|
||||
conn = this.openConnection(urlString);
|
||||
conn.setRequestMethod("POST");
|
||||
conn.setDoInput(true);
|
||||
conn.setDoOutput(true);
|
||||
conn.setChunkedStreamingMode(0);
|
||||
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
||||
conn.setRequestProperty("Accept-Charset", charset);
|
||||
this.addHeaders(conn);
|
||||
|
||||
OutputStream out = conn.getOutputStream();
|
||||
OutputStreamWriter writer = new OutputStreamWriter(out, charset);
|
||||
writer.write(this.getQueryString());
|
||||
writer.close();
|
||||
out.close();
|
||||
|
||||
conn.connect();
|
||||
int status = conn.getResponseCode();
|
||||
if (status >= 200 && status < 300) {
|
||||
is = conn.getInputStream();
|
||||
String responseData = this.readInputStream(is);
|
||||
JSONObject response = new JSONObject();
|
||||
response.put("status", status);
|
||||
response.put("data", responseData);
|
||||
callbackContext.success(response);
|
||||
Log.d(TAG, this.getParams().toString());
|
||||
HttpRequest request = HttpRequest.post(this.getUrlString());
|
||||
if (this.acceptAllCerts()) {
|
||||
request.trustAllCerts();
|
||||
request.trustAllHosts();
|
||||
}
|
||||
if (this.sslPinning()) {
|
||||
Log.d(TAG, "ssl pinning");
|
||||
request.pinToCerts();
|
||||
}
|
||||
request.acceptCharset(CHARSET);
|
||||
request.headers(this.getHeaders());
|
||||
|
||||
request.form(this.getParams());
|
||||
|
||||
int code = request.code();
|
||||
String body = request.body(CHARSET);
|
||||
|
||||
Log.d(TAG, Integer.toString(code));
|
||||
Log.d(TAG, body);
|
||||
|
||||
JSONObject response = new JSONObject();
|
||||
response.put("status", code);
|
||||
if (code >= 200 && code < 300) {
|
||||
response.put("data", body);
|
||||
this.getCallbackContext().success(response);
|
||||
} else {
|
||||
is = conn.getErrorStream();
|
||||
String responseData = this.readInputStream(is);
|
||||
JSONObject response = new JSONObject();
|
||||
response.put("status", status);
|
||||
response.put("error", responseData);
|
||||
callbackContext.error(response);
|
||||
response.put("error", body);
|
||||
this.getCallbackContext().error(response);
|
||||
}
|
||||
} catch (MalformedURLException e) {
|
||||
this.respondWithError(callbackContext, "There is an error with the url");
|
||||
} catch (JSONException e) {
|
||||
this.respondWithError(callbackContext, "There was an error with the params, headers or generating the response");
|
||||
} catch (IOException e) {
|
||||
Log.d(TAG, e.getMessage());
|
||||
this.respondWithError(callbackContext, "There was an error with the request");
|
||||
} finally {
|
||||
if (is != null) {
|
||||
try {
|
||||
is.close();
|
||||
} catch (IOException e) {}
|
||||
}
|
||||
if (conn != null) {
|
||||
conn.disconnect();
|
||||
}
|
||||
this.respondWithError("There was an error generating the response");
|
||||
} catch (HttpRequestException e) {
|
||||
Log.d(TAG, e.getMessage());
|
||||
this.respondWithError("There was an error with the request");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
/**
|
||||
* A HTTP plugin for Cordova / Phonegap
|
||||
*/
|
||||
package com.synconset;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.cordova.CallbackContext;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import com.github.kevinsawicki.http.HttpRequest;
|
||||
import com.github.kevinsawicki.http.HttpRequest.HttpRequestException;
|
||||
|
||||
public class CordovaHttpUpload extends CordovaHttp implements Runnable {
|
||||
private String filePath;
|
||||
private String name;
|
||||
|
||||
public CordovaHttpUpload(String urlString, Map<?, ?> params, Map<String, String> headers, CallbackContext callbackContext, String filePath, String name) {
|
||||
super(urlString, params, headers, callbackContext);
|
||||
this.filePath = filePath;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
HttpRequest request = HttpRequest.post(this.getUrlString());
|
||||
if (this.acceptAllCerts()) {
|
||||
request.trustAllCerts();
|
||||
request.trustAllHosts();
|
||||
}
|
||||
if (this.sslPinning()) {
|
||||
request.pinToCerts();
|
||||
}
|
||||
request.acceptCharset(CHARSET);
|
||||
request.headers(this.getHeaders());
|
||||
URI uri = new URI(filePath);
|
||||
Log.d(TAG, uri.toString());
|
||||
Log.d(TAG, name);
|
||||
int index = filePath.lastIndexOf('/');
|
||||
String filename = filePath.substring(index);
|
||||
request.part(this.name, filename, "image/jpeg", new File(uri));
|
||||
|
||||
Set<?> set = (Set<?>)this.getParams().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;
|
||||
}
|
||||
}
|
||||
|
||||
int code = request.code();
|
||||
String body = request.body(CHARSET);
|
||||
|
||||
JSONObject response = new JSONObject();
|
||||
response.put("status", code);
|
||||
if (code >= 200 && code < 300) {
|
||||
response.put("data", body);
|
||||
this.getCallbackContext().success(response);
|
||||
} else {
|
||||
response.put("error", body);
|
||||
this.getCallbackContext().error(response);
|
||||
}
|
||||
} 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.respondWithError("There was an error with the request");
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,11 +0,0 @@
|
||||
package com.synconset;
|
||||
|
||||
import javax.net.ssl.HostnameVerifier;
|
||||
import javax.net.ssl.SSLSession;
|
||||
|
||||
public class VeryTrustingHostnameVerifier implements HostnameVerifier {
|
||||
@Override
|
||||
public boolean verify(String hostname, SSLSession session) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
package com.synconset;
|
||||
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.security.cert.CertificateException;
|
||||
import javax.net.ssl.X509TrustManager;
|
||||
|
||||
public class VeryTrustingTrustManager implements X509TrustManager {
|
||||
@Override
|
||||
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException, IllegalArgumentException { }
|
||||
|
||||
@Override
|
||||
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException, IllegalArgumentException { }
|
||||
|
||||
@Override
|
||||
public X509Certificate[] getAcceptedIssuers() {
|
||||
return new X509Certificate[0];
|
||||
}
|
||||
}
|
||||
Vendored
+24
-48
@@ -7,40 +7,28 @@
|
||||
var exec = require('cordova/exec');
|
||||
|
||||
var http = {
|
||||
setAuthorizationHeaderWithUsernameAndPassword: function(username, password, success, failure) {
|
||||
return exec(success, failure, "CordovaHttpPlugin", "setAuthorizationHeaderWithUsernameAndPassword", [username, password]);
|
||||
useBasicAuth: function(username, password, success, failure) {
|
||||
return exec(success, failure, "CordovaHttpPlugin", "useBasicAuth", [username, password]);
|
||||
},
|
||||
setHeader: function(header, value, success, failure) {
|
||||
return exec(success, failure, "CordovaHttpPlugin", "setHeader", [header, value]);
|
||||
},
|
||||
enableSSLPinning: function(success, failure) {
|
||||
return exec(success, failure, "CordovaHttpPlugin", "enableSSLPinning", []);
|
||||
enableSSLPinning: function(enable, success, failure) {
|
||||
return exec(success, failure, "CordovaHttpPlugin", "enableSSLPinning", [enable]);
|
||||
},
|
||||
validateEntireCertificateChain: function(validateChain, success, failure) {
|
||||
return exec(success, failure, "CordovaHttpPlugin", "validateEntireCertificateChain", [validateChain]);
|
||||
},
|
||||
allowInvalidCertificates: function(allow, success, failure) {
|
||||
return exec(success, failure, "CordovaHttpPlugin", "allowInvalidCertificates", [allow]);
|
||||
},
|
||||
acceptText: function(success, failure) {
|
||||
return exec(success, failure, "CordovaHttpPlugin", "acceptText", []);
|
||||
},
|
||||
acceptData: function(success, failure) {
|
||||
return exec(success, failure, "CordovaHttpPlugin", "acceptData", []);
|
||||
},
|
||||
setAcceptableContentTypes: function(contentTypes, success, failure) {
|
||||
return exec(success, failure, "CordovaHttpPlugin", "setAcceptableContentTypes", contentTypes);
|
||||
acceptAllCerts: function(allow, success, failure) {
|
||||
return exec(success, failure, "CordovaHttpPlugin", "acceptAllCerts", [allow]);
|
||||
},
|
||||
post: function(url, params, headers, success, failure) {
|
||||
return exec(success, failure, "CordovaHttpPlugin", "post", [url, params, headers]);
|
||||
},
|
||||
get: function(url, params, success, failure) {
|
||||
return exec(success, failure, "CordovaHttpPlugin", "get", [url, params]);
|
||||
get: function(url, params, headers, success, failure) {
|
||||
return exec(success, failure, "CordovaHttpPlugin", "get", [url, params, headers]);
|
||||
},
|
||||
uploadFile: function(url, params, filePath, name, success, failure) {
|
||||
return exec(success, failure, "CordovaHttpPlugin", "uploadFile", [url, params, filePath, name]);
|
||||
uploadFile: function(url, params, headers, filePath, name, success, failure) {
|
||||
return exec(success, failure, "CordovaHttpPlugin", "uploadFile", [url, params, headers, filePath, name]);
|
||||
},
|
||||
downloadFile: function(url, params, filePath, success, failure) {
|
||||
downloadFile: function(url, params, headers, filePath, success, failure) {
|
||||
/*
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
@@ -71,7 +59,7 @@ var http = {
|
||||
entry.fullPath = result.file.fullPath;
|
||||
success(entry);
|
||||
};
|
||||
return exec(win, failure, "CordovaHttpPlugin", "downloadFile", [url, params, filePath]);
|
||||
return exec(win, failure, "CordovaHttpPlugin", "downloadFile", [url, params, headers, filePath]);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -109,41 +97,29 @@ if (angular) {
|
||||
}
|
||||
|
||||
var cordovaHTTP = {
|
||||
setAuthorizationHeaderWithUsernameAndPassword: function(username, password) {
|
||||
return makePromise(http.setAuthorizationHeaderWithUsernameAndPassword, [username, password]);
|
||||
useBasicAuth: function(username, password) {
|
||||
return makePromise(http.useBasicAuth, [username, password]);
|
||||
},
|
||||
setHeader: function(header, value) {
|
||||
return makePromise(http.setHeader, [header, value]);
|
||||
},
|
||||
enableSSLPinning: function() {
|
||||
return makePromise(http.enableSSLPinning, []);
|
||||
enableSSLPinning: function(enable) {
|
||||
return makePromise(http.enableSSLPinning, [enable]);
|
||||
},
|
||||
validateEntireCertificateChain: function(validateChain) {
|
||||
return makePromise(http.validateEntireCertificateChain, [validateChain]);
|
||||
},
|
||||
allowInvalidCertificates: function(allow) {
|
||||
return makePromise(http.allowInvalidCertificates, [allow]);
|
||||
},
|
||||
acceptText: function() {
|
||||
return makePromise(http.acceptText, []);
|
||||
},
|
||||
acceptData: function() {
|
||||
return makePromise(http.acceptData, []);
|
||||
},
|
||||
setAcceptableContentTypes: function(contentTypes) {
|
||||
return makePromise(http.setAcceptableContentTypes, [contentTypes]);
|
||||
acceptAllCerts: function(allow) {
|
||||
return makePromise(http.acceptAllCerts, [allow]);
|
||||
},
|
||||
post: function(url, params, headers) {
|
||||
return makePromise(http.post, [url, params, headers], true);
|
||||
},
|
||||
get: function(url, params) {
|
||||
return makePromise(http.get, [url, params], true);
|
||||
get: function(url, params, headers) {
|
||||
return makePromise(http.get, [url, params, headers], true);
|
||||
},
|
||||
uploadFile: function(url, params, filePath, name) {
|
||||
return makePromise(http.uploadFile, [url, params, filePath, name], true);
|
||||
uploadFile: function(url, params, headers, filePath, name) {
|
||||
return makePromise(http.uploadFile, [url, params, headers, filePath, name], true);
|
||||
},
|
||||
downloadFile: function(url, params, filePath) {
|
||||
return makePromise(http.downloadFile, [url, params, filePath], true);
|
||||
downloadFile: function(url, params, headers, filePath) {
|
||||
return makePromise(http.downloadFile, [url, params, headers, filePath], true);
|
||||
}
|
||||
};
|
||||
return cordovaHTTP;
|
||||
|
||||
Reference in New Issue
Block a user