mirror of
https://github.com/silkimen/cordova-plugin-advanced-http.git
synced 2026-04-24 00:00:03 +08:00
add acceptAllHosts call to test self-signed certificate in dev environment (still performs pinning but skips host validation)
TODO: iOS
This commit is contained in:
@@ -35,7 +35,8 @@ 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 String urlString;
|
||||
private Map<?, ?> params;
|
||||
private Map<String, String> headers;
|
||||
@@ -61,7 +62,11 @@ public abstract class CordovaHttp {
|
||||
sslPinning.set(false);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void acceptAllHosts(boolean accept) {
|
||||
acceptAllHosts.set(accept);
|
||||
}
|
||||
|
||||
protected String getUrlString() {
|
||||
return this.urlString;
|
||||
}
|
||||
@@ -81,10 +86,11 @@ public abstract class CordovaHttp {
|
||||
protected HttpRequest setupSecurity(HttpRequest request) {
|
||||
if (acceptAllCerts.get()) {
|
||||
request.trustAllCerts();
|
||||
request.trustAllHosts();
|
||||
request.trustAllHosts(true);
|
||||
}
|
||||
if (sslPinning.get()) {
|
||||
request.pinToCerts();
|
||||
request.trustAllHosts(acceptAllHosts.get());
|
||||
}
|
||||
return request;
|
||||
}
|
||||
|
||||
@@ -84,6 +84,10 @@ public class CordovaHttpPlugin extends CordovaPlugin {
|
||||
boolean accept = args.getBoolean(0);
|
||||
CordovaHttp.acceptAllCerts(accept);
|
||||
callbackContext.success();
|
||||
} else if (action.equals("acceptAllHosts")) {
|
||||
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);
|
||||
|
||||
@@ -3252,11 +3252,17 @@ public class HttpRequest {
|
||||
*
|
||||
* @return this request
|
||||
*/
|
||||
public HttpRequest trustAllHosts() {
|
||||
public HttpRequest trustAllHosts(boolean enable) {
|
||||
final HttpURLConnection connection = getConnection();
|
||||
if (connection instanceof HttpsURLConnection)
|
||||
((HttpsURLConnection) connection)
|
||||
.setHostnameVerifier(getTrustedVerifier());
|
||||
if (connection instanceof HttpsURLConnection) {
|
||||
if (enable) {
|
||||
((HttpsURLConnection) connection)
|
||||
.setHostnameVerifier(getTrustedVerifier());
|
||||
} else {
|
||||
((HttpsURLConnection) connection)
|
||||
.setHostnameVerifier(HttpsURLConnection.getDefaultHostnameVerifier());
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user