Skip to content

Commit f1c95ef

Browse files
committed
fixes silkimen#79
1 parent 3f1ee62 commit f1c95ef

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed

plugin.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,7 @@
6767
<source-file src="src/android/com/synconset/cordovahttp/CordovaHttpPut.java" target-dir="src/com/synconset/cordovahttp"/>
6868
<source-file src="src/android/com/synconset/cordovahttp/CordovaHttpPatch.java" target-dir="src/com/synconset/cordovahttp"/>
6969
<source-file src="src/android/com/synconset/cordovahttp/CordovaHttpUpload.java" target-dir="src/com/synconset/cordovahttp"/>
70+
71+
<framework src="com.squareup.okhttp3:okhttp-urlconnection:3.9.1" />
7072
</platform>
7173
</plugin>

src/android/com/github/kevinsawicki/http/TLSSocketFactory.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,45 +11,45 @@
1111

1212
public class TLSSocketFactory extends SSLSocketFactory {
1313

14-
private SSLSocketFactory internalSSLSocketFactory;
14+
private SSLSocketFactory delegate;
1515

1616
public TLSSocketFactory(SSLContext context) {
17-
internalSSLSocketFactory = context.getSocketFactory();
17+
delegate = context.getSocketFactory();
1818
}
1919

2020
@Override
2121
public String[] getDefaultCipherSuites() {
22-
return internalSSLSocketFactory.getDefaultCipherSuites();
22+
return delegate.getDefaultCipherSuites();
2323
}
2424

2525
@Override
2626
public String[] getSupportedCipherSuites() {
27-
return internalSSLSocketFactory.getSupportedCipherSuites();
27+
return delegate.getSupportedCipherSuites();
2828
}
2929

3030
@Override
3131
public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException {
32-
return enableTLSOnSocket(internalSSLSocketFactory.createSocket(s, host, port, autoClose));
32+
return enableTLSOnSocket(delegate.createSocket(s, host, port, autoClose));
3333
}
3434

3535
@Override
3636
public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
37-
return enableTLSOnSocket(internalSSLSocketFactory.createSocket(host, port));
37+
return enableTLSOnSocket(delegate.createSocket(host, port));
3838
}
3939

4040
@Override
4141
public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException, UnknownHostException {
42-
return enableTLSOnSocket(internalSSLSocketFactory.createSocket(host, port, localHost, localPort));
42+
return enableTLSOnSocket(delegate.createSocket(host, port, localHost, localPort));
4343
}
4444

4545
@Override
4646
public Socket createSocket(InetAddress host, int port) throws IOException {
47-
return enableTLSOnSocket(internalSSLSocketFactory.createSocket(host, port));
47+
return enableTLSOnSocket(delegate.createSocket(host, port));
4848
}
4949

5050
@Override
5151
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException {
52-
return enableTLSOnSocket(internalSSLSocketFactory.createSocket(address, port, localAddress, localPort));
52+
return enableTLSOnSocket(delegate.createSocket(address, port, localAddress, localPort));
5353
}
5454

5555
private Socket enableTLSOnSocket(Socket socket) {

src/android/com/synconset/cordovahttp/CordovaHttp.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@
3434

3535
import com.github.kevinsawicki.http.HttpRequest;
3636
import com.github.kevinsawicki.http.HttpRequest.HttpRequestException;
37+
import com.github.kevinsawicki.http.HttpRequest.ConnectionFactory;
38+
39+
import okhttp3.OkUrlFactory;
40+
import okhttp3.OkHttpClient;
41+
42+
import java.net.URL;
43+
import java.net.HttpURLConnection;
44+
import java.net.URLStreamHandler;
45+
import java.net.Proxy;
46+
3747

3848
abstract class CordovaHttp {
3949
protected static final String TAG = "CordovaHTTP";
@@ -231,9 +241,29 @@ protected HashMap<String, Object> getMapFromJSONObject(JSONObject object) throws
231241
return map;
232242
}
233243

244+
private ConnectionFactory getConnectionFactory() {
245+
final OkHttpClient okHttpClient = new OkHttpClient();
246+
247+
return new ConnectionFactory() {
248+
public HttpURLConnection create(URL url) {
249+
OkHttpClient okHttpClient = new OkHttpClient();
250+
OkUrlFactory okUrlFactory = new OkUrlFactory(okHttpClient);
251+
return (HttpURLConnection) okUrlFactory.open(url);
252+
}
253+
254+
public HttpURLConnection create(URL url, Proxy proxy) {
255+
OkHttpClient okHttpClient = new OkHttpClient.Builder().proxy(proxy).build();
256+
OkUrlFactory okUrlFactory = new OkUrlFactory(okHttpClient);
257+
return (HttpURLConnection) okUrlFactory.open(url);
258+
}
259+
};
260+
}
261+
234262
protected void prepareRequest(HttpRequest request) throws HttpRequestException, JSONException {
235263
this.setupRedirect(request);
236264
this.setupSecurity(request);
265+
266+
request.setConnectionFactory(getConnectionFactory());
237267
request.readTimeout(this.getRequestTimeout());
238268
request.acceptCharset(ACCEPTED_CHARSETS);
239269
request.headers(this.getHeadersMap());

0 commit comments

Comments
 (0)