mirror of
https://github.com/silkimen/cordova-plugin-advanced-http.git
synced 2026-05-31 00:00:07 +08:00
updated http request and removed some android code that is now in js. Fixed upload file in iOS
This commit is contained in:
@@ -37,7 +37,7 @@ public abstract class CordovaHttp {
|
||||
|
||||
private static AtomicBoolean sslPinning = new AtomicBoolean(false);
|
||||
private static AtomicBoolean acceptAllCerts = new AtomicBoolean(false);
|
||||
private static AtomicBoolean acceptAllHosts = new AtomicBoolean(false);
|
||||
private static AtomicBoolean validateDomainName = new AtomicBoolean(true);
|
||||
|
||||
private String urlString;
|
||||
private Map<?, ?> params;
|
||||
@@ -65,8 +65,8 @@ public abstract class CordovaHttp {
|
||||
}
|
||||
}
|
||||
|
||||
public static void acceptAllHosts(boolean accept) {
|
||||
acceptAllHosts.set(accept);
|
||||
public static void validateDomainName(boolean accept) {
|
||||
validateDomainName.set(accept);
|
||||
}
|
||||
|
||||
protected String getUrlString() {
|
||||
@@ -88,11 +88,12 @@ public abstract class CordovaHttp {
|
||||
protected HttpRequest setupSecurity(HttpRequest request) {
|
||||
if (acceptAllCerts.get()) {
|
||||
request.trustAllCerts();
|
||||
request.trustAllHosts(true);
|
||||
}
|
||||
if (!validateDomainName.get()) {
|
||||
request.trustAllHosts();
|
||||
}
|
||||
if (sslPinning.get()) {
|
||||
request.pinToCerts();
|
||||
request.trustAllHosts(acceptAllHosts.get());
|
||||
}
|
||||
return request;
|
||||
}
|
||||
|
||||
@@ -40,12 +40,9 @@ import com.github.kevinsawicki.http.HttpRequest;
|
||||
public class CordovaHttpPlugin extends CordovaPlugin {
|
||||
private static final String TAG = "CordovaHTTP";
|
||||
|
||||
private HashMap<String, String> globalHeaders;
|
||||
|
||||
@Override
|
||||
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
|
||||
super.initialize(cordova, webView);
|
||||
this.globalHeaders = new HashMap<String, String>();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -55,7 +52,7 @@ public class CordovaHttpPlugin extends CordovaPlugin {
|
||||
JSONObject params = args.getJSONObject(1);
|
||||
JSONObject headers = args.getJSONObject(2);
|
||||
HashMap<?, ?> paramsMap = this.getMapFromJSONObject(params);
|
||||
HashMap<String, String> headersMap = this.addToMap(this.globalHeaders, headers);
|
||||
HashMap<String, String> headersMap = this.getStringMapFromJSONObject(headers);
|
||||
CordovaHttpGet get = new CordovaHttpGet(urlString, paramsMap, headersMap, callbackContext);
|
||||
cordova.getThreadPool().execute(get);
|
||||
} else if (action.equals("head")) {
|
||||
@@ -63,7 +60,7 @@ public class CordovaHttpPlugin extends CordovaPlugin {
|
||||
JSONObject params = args.getJSONObject(1);
|
||||
JSONObject headers = args.getJSONObject(2);
|
||||
HashMap<?, ?> paramsMap = this.getMapFromJSONObject(params);
|
||||
HashMap<String, String> headersMap = this.addToMap(this.globalHeaders, headers);
|
||||
HashMap<String, String> headersMap = this.getStringMapFromJSONObject(headers);
|
||||
CordovaHttpHead head = new CordovaHttpHead(urlString, paramsMap, headersMap, callbackContext);
|
||||
cordova.getThreadPool().execute(head);
|
||||
} else if (action.equals("post")) {
|
||||
@@ -71,14 +68,9 @@ public class CordovaHttpPlugin extends CordovaPlugin {
|
||||
JSONObject params = args.getJSONObject(1);
|
||||
JSONObject headers = args.getJSONObject(2);
|
||||
HashMap<?, ?> paramsMap = this.getMapFromJSONObject(params);
|
||||
HashMap<String, String> headersMap = this.addToMap(this.globalHeaders, headers);
|
||||
HashMap<String, String> headersMap = this.getStringMapFromJSONObject(headers);
|
||||
CordovaHttpPost post = new CordovaHttpPost(urlString, paramsMap, headersMap, callbackContext);
|
||||
cordova.getThreadPool().execute(post);
|
||||
} else if (action.equals("useBasicAuth")) {
|
||||
String username = args.getString(0);
|
||||
String password = args.getString(1);
|
||||
this.useBasicAuth(username, password);
|
||||
callbackContext.success();
|
||||
} else if (action.equals("enableSSLPinning")) {
|
||||
try {
|
||||
boolean enable = args.getBoolean(0);
|
||||
@@ -92,21 +84,16 @@ public class CordovaHttpPlugin extends CordovaPlugin {
|
||||
boolean accept = args.getBoolean(0);
|
||||
CordovaHttp.acceptAllCerts(accept);
|
||||
callbackContext.success();
|
||||
} else if (action.equals("acceptAllHosts")) {
|
||||
} else if (action.equals("validateDomainName")) {
|
||||
boolean accept = args.getBoolean(0);
|
||||
CordovaHttp.acceptAllHosts(accept);
|
||||
callbackContext.success();
|
||||
} else if (action.equals("setHeader")) {
|
||||
String header = args.getString(0);
|
||||
String value = args.getString(1);
|
||||
this.setHeader(header, value);
|
||||
CordovaHttp.validateDomainName(accept);
|
||||
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);
|
||||
HashMap<String, String> headersMap = this.getStringMapFromJSONObject(headers);
|
||||
String filePath = args.getString(3);
|
||||
String name = args.getString(4);
|
||||
CordovaHttpUpload upload = new CordovaHttpUpload(urlString, paramsMap, headersMap, callbackContext, filePath, name);
|
||||
@@ -116,7 +103,7 @@ public class CordovaHttpPlugin extends CordovaPlugin {
|
||||
JSONObject params = args.getJSONObject(1);
|
||||
JSONObject headers = args.getJSONObject(2);
|
||||
HashMap<?, ?> paramsMap = this.getMapFromJSONObject(params);
|
||||
HashMap<String, String> headersMap = this.addToMap(this.globalHeaders, headers);
|
||||
HashMap<String, String> headersMap = this.getStringMapFromJSONObject(headers);
|
||||
String filePath = args.getString(3);
|
||||
CordovaHttpDownload download = new CordovaHttpDownload(urlString, paramsMap, headersMap, callbackContext, filePath);
|
||||
cordova.getThreadPool().execute(download);
|
||||
@@ -126,16 +113,6 @@ public class CordovaHttpPlugin extends CordovaPlugin {
|
||||
return true;
|
||||
}
|
||||
|
||||
private void useBasicAuth(String username, String password) {
|
||||
String loginInfo = username + ":" + password;
|
||||
loginInfo = "Basic " + Base64.encodeToString(loginInfo.getBytes(), Base64.NO_WRAP);
|
||||
this.globalHeaders.put("Authorization", loginInfo);
|
||||
}
|
||||
|
||||
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();
|
||||
@@ -173,15 +150,15 @@ public class CordovaHttpPlugin extends CordovaPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
private HashMap<String, String> addToMap(HashMap<String, String> map, JSONObject object) throws JSONException {
|
||||
HashMap<String, String> newMap = (HashMap<String, String>)map.clone();
|
||||
private HashMap<String, String> getStringMapFromJSONObject(JSONObject object) throws JSONException {
|
||||
HashMap<String, String> map = new HashMap<String, String>();
|
||||
Iterator<?> i = object.keys();
|
||||
|
||||
while (i.hasNext()) {
|
||||
String key = (String)i.next();
|
||||
newMap.put(key, object.getString(key));
|
||||
map.put(key, object.getString(key));
|
||||
}
|
||||
return newMap;
|
||||
return map;
|
||||
}
|
||||
|
||||
private HashMap<String, Object> getMapFromJSONObject(JSONObject object) throws JSONException {
|
||||
|
||||
@@ -71,6 +71,7 @@ import java.security.cert.Certificate;
|
||||
import java.security.cert.CertificateFactory;
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
@@ -351,6 +352,32 @@ public class HttpRequest {
|
||||
return result;
|
||||
}
|
||||
|
||||
private static StringBuilder addParam(final Object key, Object value,
|
||||
final StringBuilder result) {
|
||||
if (value != null && value.getClass().isArray())
|
||||
value = arrayToList(value);
|
||||
|
||||
if (value instanceof Iterable<?>) {
|
||||
Iterator<?> iterator = ((Iterable<?>) value).iterator();
|
||||
while (iterator.hasNext()) {
|
||||
result.append(key);
|
||||
result.append("[]=");
|
||||
Object element = iterator.next();
|
||||
if (element != null)
|
||||
result.append(element);
|
||||
if (iterator.hasNext())
|
||||
result.append("&");
|
||||
}
|
||||
} else {
|
||||
result.append(key);
|
||||
result.append("=");
|
||||
if (value != null)
|
||||
result.append(value);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates {@link HttpURLConnection HTTP connections} for
|
||||
* {@link URL urls}.
|
||||
@@ -870,6 +897,36 @@ public class HttpRequest {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents array of any type as list of objects so we can easily iterate over it
|
||||
* @param array of elements
|
||||
* @return list with the same elements
|
||||
*/
|
||||
private static List<Object> arrayToList(final Object array) {
|
||||
if (array instanceof Object[])
|
||||
return Arrays.asList((Object[]) array);
|
||||
|
||||
List<Object> result = new ArrayList<Object>();
|
||||
// Arrays of the primitive types can't be cast to array of Object, so this:
|
||||
if (array instanceof int[])
|
||||
for (int value : (int[]) array) result.add(value);
|
||||
else if (array instanceof boolean[])
|
||||
for (boolean value : (boolean[]) array) result.add(value);
|
||||
else if (array instanceof long[])
|
||||
for (long value : (long[]) array) result.add(value);
|
||||
else if (array instanceof float[])
|
||||
for (float value : (float[]) array) result.add(value);
|
||||
else if (array instanceof double[])
|
||||
for (double value : (double[]) array) result.add(value);
|
||||
else if (array instanceof short[])
|
||||
for (short value : (short[]) array) result.add(value);
|
||||
else if (array instanceof byte[])
|
||||
for (byte value : (byte[]) array) result.add(value);
|
||||
else if (array instanceof char[])
|
||||
for (char value : (char[]) array) result.add(value);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode the given URL as an ASCII {@link String}
|
||||
* <p>
|
||||
@@ -933,23 +990,14 @@ public class HttpRequest {
|
||||
addParamPrefix(baseUrl, result);
|
||||
|
||||
Entry<?, ?> entry;
|
||||
Object value;
|
||||
Iterator<?> iterator = params.entrySet().iterator();
|
||||
entry = (Entry<?, ?>) iterator.next();
|
||||
result.append(entry.getKey().toString());
|
||||
result.append('=');
|
||||
value = entry.getValue();
|
||||
if (value != null)
|
||||
result.append(value);
|
||||
addParam(entry.getKey().toString(), entry.getValue(), result);
|
||||
|
||||
while (iterator.hasNext()) {
|
||||
result.append('&');
|
||||
entry = (Entry<?, ?>) iterator.next();
|
||||
result.append(entry.getKey().toString());
|
||||
result.append('=');
|
||||
value = entry.getValue();
|
||||
if (value != null)
|
||||
result.append(value);
|
||||
addParam(entry.getKey().toString(), entry.getValue(), result);
|
||||
}
|
||||
|
||||
return result.toString();
|
||||
@@ -980,20 +1028,11 @@ public class HttpRequest {
|
||||
addPathSeparator(baseUrl, result);
|
||||
addParamPrefix(baseUrl, result);
|
||||
|
||||
Object value;
|
||||
result.append(params[0]);
|
||||
result.append('=');
|
||||
value = params[1];
|
||||
if (value != null)
|
||||
result.append(value);
|
||||
addParam(params[0], params[1], result);
|
||||
|
||||
for (int i = 2; i < params.length; i += 2) {
|
||||
result.append('&');
|
||||
result.append(params[i]);
|
||||
result.append('=');
|
||||
value = params[i + 1];
|
||||
if (value != null)
|
||||
result.append(value);
|
||||
addParam(params[i], params[i + 1], result);
|
||||
}
|
||||
|
||||
return result.toString();
|
||||
@@ -1052,7 +1091,7 @@ public class HttpRequest {
|
||||
* the name/value query parameter pairs to include as part of the
|
||||
* baseUrl
|
||||
*
|
||||
* @see #append(CharSequence, String...)
|
||||
* @see #append(CharSequence, Object...)
|
||||
* @see #encode(CharSequence)
|
||||
*
|
||||
* @return request
|
||||
@@ -1116,7 +1155,7 @@ public class HttpRequest {
|
||||
* the name/value query parameter pairs to include as part of the
|
||||
* baseUrl
|
||||
*
|
||||
* @see #append(CharSequence, String...)
|
||||
* @see #append(CharSequence, Object...)
|
||||
* @see #encode(CharSequence)
|
||||
*
|
||||
* @return request
|
||||
@@ -1180,7 +1219,7 @@ public class HttpRequest {
|
||||
* the name/value query parameter pairs to include as part of the
|
||||
* baseUrl
|
||||
*
|
||||
* @see #append(CharSequence, String...)
|
||||
* @see #append(CharSequence, Object...)
|
||||
* @see #encode(CharSequence)
|
||||
*
|
||||
* @return request
|
||||
@@ -1244,7 +1283,7 @@ public class HttpRequest {
|
||||
* the name/value query parameter pairs to include as part of the
|
||||
* baseUrl
|
||||
*
|
||||
* @see #append(CharSequence, String...)
|
||||
* @see #append(CharSequence, Object...)
|
||||
* @see #encode(CharSequence)
|
||||
*
|
||||
* @return request
|
||||
@@ -1308,7 +1347,7 @@ public class HttpRequest {
|
||||
* the name/value query parameter pairs to include as part of the
|
||||
* baseUrl
|
||||
*
|
||||
* @see #append(CharSequence, String...)
|
||||
* @see #append(CharSequence, Object...)
|
||||
* @see #encode(CharSequence)
|
||||
*
|
||||
* @return request
|
||||
@@ -1388,7 +1427,7 @@ public class HttpRequest {
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the 'http.proxyHost' & 'https.proxyHost' properties to the given host
|
||||
* Set the 'http.proxyHost' and 'https.proxyHost' properties to the given host
|
||||
* value.
|
||||
* <p>
|
||||
* This setting will apply to all requests.
|
||||
@@ -1401,7 +1440,7 @@ public class HttpRequest {
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the 'http.proxyPort' & 'https.proxyPort' properties to the given port
|
||||
* Set the 'http.proxyPort' and 'https.proxyPort' properties to the given port
|
||||
* number.
|
||||
* <p>
|
||||
* This setting will apply to all requests.
|
||||
@@ -3252,17 +3291,11 @@ public class HttpRequest {
|
||||
*
|
||||
* @return this request
|
||||
*/
|
||||
public HttpRequest trustAllHosts(boolean enable) {
|
||||
public HttpRequest trustAllHosts() {
|
||||
final HttpURLConnection connection = getConnection();
|
||||
if (connection instanceof HttpsURLConnection) {
|
||||
if (enable) {
|
||||
((HttpsURLConnection) connection)
|
||||
.setHostnameVerifier(getTrustedVerifier());
|
||||
} else {
|
||||
((HttpsURLConnection) connection)
|
||||
.setHostnameVerifier(HttpsURLConnection.getDefaultHostnameVerifier());
|
||||
}
|
||||
}
|
||||
if (connection instanceof HttpsURLConnection)
|
||||
((HttpsURLConnection) connection)
|
||||
.setHostnameVerifier(getTrustedVerifier());
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@@ -152,7 +152,7 @@
|
||||
NSString *filePath = [command.arguments objectAtIndex: 3];
|
||||
NSString *name = [command.arguments objectAtIndex: 4];
|
||||
|
||||
NSURL *fileURL = [NSURL fileURLWithPath: filePath];
|
||||
NSURL *fileURL = [NSURL URLWithString: filePath];
|
||||
|
||||
[self setRequestHeaders: headers forManager: manager];
|
||||
|
||||
|
||||
Reference in New Issue
Block a user