WIP: started refactoring

- one HTTP request class implementing Runnable for all request methods
 - broken SSL cert handling
This commit is contained in:
Sefa Ilkimen
2019-03-21 15:12:53 +01:00
parent 314314d7f9
commit 752b2cdcb7
23 changed files with 3896 additions and 342 deletions
@@ -0,0 +1,154 @@
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;
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 String method;
private String url;
private String serializer = "none";
private Object data;
private JSONObject params;
private JSONObject headers;
private int timeout;
private CallbackContext callbackContext;
public CordovaHttpRequest(String method, String url, String serializer, Object data, JSONObject headers,
int timeout, CallbackContext callbackContext) {
this.method = method;
this.url = url;
this.serializer = serializer;
this.data = data;
this.headers = headers;
this.timeout = timeout;
this.callbackContext = callbackContext;
}
public CordovaHttpRequest(String method, String url, JSONObject params, JSONObject headers, int timeout,
CallbackContext callbackContext) {
this.method = method;
this.url = url;
this.params = params;
this.headers = headers;
this.timeout = timeout;
this.callbackContext = callbackContext;
}
@Override
public void run() {
HttpResponse response = new HttpResponse();
try {
String processedUrl = HttpRequest.encode(HttpRequest.append(this.url, JsonUtils.getObjectMap(this.params)));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
HttpRequest request = new HttpRequest(processedUrl, this.method)
.followRedirects(true /* @TODO */)
.readTimeout(this.timeout)
.acceptCharset("UTF-8")
.uncompress(true);
// setup content type before applying headers, so user can override it
this.setContentType(request)
.headers(JsonUtils.getStringMap(this.headers));
this.sendBody(request)
.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);
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);
} 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);
} 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);
} 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);
}
} catch (Exception e) {
response.setStatus(-1);
response.setErrorMessage(e.getMessage());
Log.e("Cordova-Plugin-HTTP", "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("Cordova-Plugin-HTTP", "An unexpected error occured while processing HTTP response", e);
}
}
private HttpRequest setContentType(HttpRequest request) {
switch(this.serializer) {
case "json":
return request.contentType("application/json", "UTF-8");
case "utf8":
return request.contentType("text/plain", "UTF-8");
case "urlencoded":
// intentionally left blank, because content type is set in HttpRequest.form()
}
return request;
}
private HttpRequest sendBody(HttpRequest request) throws JSONException {
if (this.data == null) {
return request;
}
switch (this.serializer) {
case "json":
return request.send(this.data.toString());
case "utf8":
return request.send(((JSONObject) this.data).getString("text"));
case "urlencoded":
return request.form(JsonUtils.getObjectMap((JSONObject) this.data));
}
return request;
}
}
@@ -0,0 +1,19 @@
package com.silkimen.http;
import javax.net.ssl.HostnameVerifier;
public class HostnameVerfifierFactory {
private final HostnameVerifier noOpVerififer;
public HostnameVerifierFactory() {
this.noOpVerififer = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
}
public HostnameVerifier getNoOpVerifier() {
return this.noOpVerififer;
}
}
@@ -0,0 +1,48 @@
package com.silkimen.http;
import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CodingErrorAction;
import java.nio.charset.MalformedInputException;
public class HttpBodyDecoder {
private static final String[] ACCEPTED_CHARSETS = new String[] { "UTF-8", "ISO-8859-1" };
public static String decodeBody(ByteBuffer rawOutput, String charsetName)
throws CharacterCodingException, MalformedInputException {
if (charsetName == null) {
return tryDecodeByteBuffer(rawOutput);
}
return decodeByteBuffer(rawOutput, charsetName);
}
private static String tryDecodeByteBuffer(ByteBuffer rawOutput) throws CharacterCodingException, MalformedInputException {
for (int i = 0; i < ACCEPTED_CHARSETS.length - 1; i++) {
try {
return decodeByteBuffer(rawOutput, ACCEPTED_CHARSETS[i]);
} catch (MalformedInputException e) {
continue;
} catch (CharacterCodingException e) {
continue;
}
}
return decodeBody(rawOutput, ACCEPTED_CHARSETS[ACCEPTED_CHARSETS.length - 1]);
}
private static String decodeByteBuffer(ByteBuffer rawOutput, String charsetName)
throws CharacterCodingException, MalformedInputException {
return createCharsetDecoder(charsetName).decode(rawOutput).toString();
}
private static CharsetDecoder createCharsetDecoder(String charsetName) {
return Charset.forName(charsetName).newDecoder().onMalformedInput(CodingErrorAction.REPORT)
.onUnmappableCharacter(CodingErrorAction.REPORT);
}
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,80 @@
package com.silkimen.http;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
import android.text.TextUtils;
import android.util.Log;
public class HttpResponse {
private int status;
private String url;
private Map<String, List<String>> headers;
private String body;
private boolean failed;
private String error;
public void setStatus(int status) {
this.status = status;
}
public void setUrl(String url) {
this.url = url;
}
public void setHeaders(Map<String, List<String>> headers) {
this.headers = headers;
}
public void setBody(String body) {
this.body = body;
}
public void setErrorMessage(String message) {
this.failed = true;
this.error = message;
}
public boolean hasFailed() {
return this.failed;
}
public JSONObject toJSON() throws JSONException {
JSONObject json = new JSONObject();
json.put("status", this.status);
json.put("url", this.url);
if (this.failed) {
json.put("error", this.error);
} else {
json.put("headers", new JSONObject(getFilteredHeaders()));
json.put("data", this.body);
}
return json;
}
private Map<String, String> getFilteredHeaders() throws JSONException {
Map<String, String> filteredHeaders = new HashMap<String, String>();
if (this.headers == null || this.headers.isEmpty()) {
return filteredHeaders;
}
for (Map.Entry<String, List<String>> entry : this.headers.entrySet()) {
String key = entry.getKey();
List<String> value = entry.getValue();
if ((key != null) && (!value.isEmpty())) {
filteredHeaders.put(key.toLowerCase(), TextUtils.join(", ", value));
}
}
return filteredHeaders;
}
}
@@ -0,0 +1,58 @@
package com.silkimen.http;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class JsonUtils {
public static HashMap<String, String> getStringMap(JSONObject object) throws JSONException {
HashMap<String, String> map = new HashMap<String, String>();
if (object == null) {
return map;
}
Iterator<?> i = object.keys();
while (i.hasNext()) {
String key = (String) i.next();
map.put(key, object.getString(key));
}
return map;
}
public static HashMap<String, Object> getObjectMap(JSONObject object) throws JSONException {
HashMap<String, Object> map = new HashMap<String, Object>();
if (object == null) {
return map;
}
Iterator<?> i = object.keys();
while (i.hasNext()) {
String key = (String) i.next();
Object value = object.get(key);
if (value instanceof JSONArray) {
map.put(key, getObjectList((JSONArray) value));
} else {
map.put(key, object.get(key));
}
}
return map;
}
public static ArrayList<Object> getObjectList(JSONArray array) throws JSONException {
ArrayList<Object> list = new ArrayList<Object>();
for (int i = 0; i < array.length(); i++) {
list.add(array.get(i));
}
return list;
}
}
@@ -0,0 +1,34 @@
package com.silkimen.http;
import okhttp3.OkUrlFactory;
import okhttp3.OkHttpClient;
/**
* A {@link HttpRequest.ConnectionFactory connection factory} which uses OkHttp.
* <p/>
* Call {@link HttpRequest#setConnectionFactory(HttpRequest.ConnectionFactory)}
* with an instance of this class to enable.
*/
public class OkConnectionFactory implements HttpRequest.ConnectionFactory {
private final OkHttpClient client;
public OkConnectionFactory() {
this(new OkHttpClient());
}
public OkConnectionFactory(OkHttpClient client) {
if (client == null) {
throw new NullPointerException("Client must not be null.");
}
this.client = client;
}
public HttpURLConnection create(URL url) throws IOException {
return client.open(url);
}
public HttpURLConnection create(URL url, Proxy proxy) throws IOException {
throw new UnsupportedOperationException(
"Per-connection proxy is not supported. Use OkHttpClient's setProxy instead.");
}
}
@@ -0,0 +1,25 @@
package com.github.kevinsawicki.http;
import okhttp3.OkUrlFactory;
import okhttp3.OkHttpClient;
import java.net.URL;
import java.net.HttpURLConnection;
import java.net.URLStreamHandler;
import java.net.Proxy;
public class OkConnectionFactory implements HttpRequest.ConnectionFactory {
protected OkHttpClient okHttpClient = new OkHttpClient();
public HttpURLConnection create(URL url) {
OkUrlFactory okUrlFactory = new OkUrlFactory(okHttpClient);
return (HttpURLConnection) okUrlFactory.open(url);
}
public HttpURLConnection create(URL url, Proxy proxy) {
OkHttpClient okHttpClientWithProxy = okHttpClient.newBuilder().proxy(proxy).build();
OkUrlFactory okUrlFactory = new OkUrlFactory(okHttpClientWithProxy);
return (HttpURLConnection) okUrlFactory.open(url);
}
}
@@ -0,0 +1,63 @@
package com.silkimen.http;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
public class TLSSocketFactory extends SSLSocketFactory {
private SSLSocketFactory delegate;
public TLSSocketFactory(SSLContext context) {
delegate = context.getSocketFactory();
}
@Override
public String[] getDefaultCipherSuites() {
return delegate.getDefaultCipherSuites();
}
@Override
public String[] getSupportedCipherSuites() {
return delegate.getSupportedCipherSuites();
}
@Override
public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException {
return enableTLSOnSocket(delegate.createSocket(s, host, port, autoClose));
}
@Override
public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
return enableTLSOnSocket(delegate.createSocket(host, port));
}
@Override
public Socket createSocket(String host, int port, InetAddress localHost, int localPort)
throws IOException, UnknownHostException {
return enableTLSOnSocket(delegate.createSocket(host, port, localHost, localPort));
}
@Override
public Socket createSocket(InetAddress host, int port) throws IOException {
return enableTLSOnSocket(delegate.createSocket(host, port));
}
@Override
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort)
throws IOException {
return enableTLSOnSocket(delegate.createSocket(address, port, localAddress, localPort));
}
private Socket enableTLSOnSocket(Socket socket) {
if (socket != null && (socket instanceof SSLSocket)) {
((SSLSocket) socket).setEnabledProtocols(new String[] { "TLSv1", "TLSv1.1", "TLSv1.2" });
}
return socket;
}
}
@@ -0,0 +1,59 @@
package com.silkimen.http;
import java.security.KeyStore;
import java.security.cert.Certificate;
import java.util.ArrayList;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;
public class TrustManagersFactory {
private final TrustManager[] noOpTrustManager;
public TrustManagersFactory() {
this.noOpTrustManager = new TrustManager[] { new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
public void checkClientTrusted(X509Certificate[] chain, String authType) {
// Intentionally left blank
}
public void checkServerTrusted(X509Certificate[] chain, String authType) {
// Intentionally left blank
}
} };
}
public TrustManager[] getNoopTrustManagers() {
return this.noOpTrustManager;
}
public TrustManager[] getPinnedTrustManagers(ArrayList<Certificate> pinnedCerts) throws IOException {
if (pinnedCerts == null || pinnedCerts.size() == 0) {
throw new IOException("You must add at least 1 certificate in order to pin to certificates");
}
try {
String keyStoreType = KeyStore.getDefaultType();
KeyStore keyStore = KeyStore.getInstance(keyStoreType);
keyStore.load(null, null);
for (int i = 0; i < pinnedCerts.size(); i++) {
keyStore.setCertificateEntry("CA" + i, pinnedCerts.get(i));
}
// Create a TrustManager that trusts the CAs in our KeyStore
String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
tmf.init(keyStore);
return tmf.getTrustManagers();
} catch (GeneralSecurityException e) {
IOException ioException = new IOException("Security exception configuring SSL trust managers");
ioException.initCause(e);
throw new HttpRequestException(ioException);
}
}
}