Skip to content

Commit 301cc92

Browse files
committed
1 parent cebd029 commit 301cc92

File tree

2 files changed

+102
-1
lines changed

2 files changed

+102
-1
lines changed

library/src/com/loopj/android/http/AsyncHttpClient.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,15 @@ public class AsyncHttpClient {
119119
* Creates a new AsyncHttpClient.
120120
*/
121121
public AsyncHttpClient() {
122+
this(false);
123+
}
124+
125+
/**
126+
* Creates a new AsyncHttpClient.
127+
*
128+
* @param fixNoHttpResponseException See issue https://github.com/loopj/android-async-http/issues/143
129+
*/
130+
public AsyncHttpClient(boolean fixNoHttpResponseException) {
122131
BasicHttpParams httpParams = new BasicHttpParams();
123132

124133
ConnManagerParams.setTimeout(httpParams, socketTimeout);
@@ -133,9 +142,18 @@ public AsyncHttpClient() {
133142
HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
134143
HttpProtocolParams.setUserAgent(httpParams, String.format("android-async-http/%s (http://loopj.com/android-async-http)", VERSION));
135144

145+
// Fix to SSL flaw in API < ICS
146+
// See https://code.google.com/p/android/issues/detail?id=13117
147+
SSLSocketFactory sslSocketFactory;
148+
if(fixNoHttpResponseException)
149+
sslSocketFactory = MySSLSocketFactory.getFixedSocketFactory();
150+
else
151+
sslSocketFactory = SSLSocketFactory.getSocketFactory();
152+
136153
SchemeRegistry schemeRegistry = new SchemeRegistry();
137154
schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
138-
schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
155+
schemeRegistry.register(new Scheme("https", sslSocketFactory, 443));
156+
139157
ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(httpParams, schemeRegistry);
140158

141159
threadPool = (ThreadPoolExecutor) Executors.newCachedThreadPool();
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.loopj.android.http;
2+
3+
import org.apache.http.conn.ssl.SSLSocketFactory;
4+
5+
import java.io.IOException;
6+
import java.net.Socket;
7+
import java.net.UnknownHostException;
8+
import java.security.KeyManagementException;
9+
import java.security.KeyStore;
10+
import java.security.KeyStoreException;
11+
import java.security.NoSuchAlgorithmException;
12+
import java.security.UnrecoverableKeyException;
13+
14+
import javax.net.ssl.SSLContext;
15+
import javax.net.ssl.TrustManager;
16+
import javax.net.ssl.X509TrustManager;
17+
18+
/**
19+
* This file is introduced to fix HTTPS Post bug on API < ICS
20+
* see http://code.google.com/p/android/issues/detail?id=13117#c14
21+
*/
22+
public class MySSLSocketFactory extends SSLSocketFactory {
23+
SSLContext sslContext = SSLContext.getInstance("TLS");
24+
25+
public MySSLSocketFactory(KeyStore truststore)
26+
throws NoSuchAlgorithmException, KeyManagementException,
27+
KeyStoreException, UnrecoverableKeyException {
28+
super(truststore);
29+
30+
TrustManager tm = new X509TrustManager() {
31+
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
32+
return null;
33+
}
34+
35+
@Override
36+
public void checkClientTrusted(
37+
java.security.cert.X509Certificate[] chain, String authType)
38+
throws java.security.cert.CertificateException {
39+
}
40+
41+
@Override
42+
public void checkServerTrusted(
43+
java.security.cert.X509Certificate[] chain, String authType)
44+
throws java.security.cert.CertificateException {
45+
}
46+
};
47+
sslContext.init(null, new TrustManager[]{tm}, null);
48+
}
49+
50+
@Override
51+
public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException, UnknownHostException {
52+
return sslContext.getSocketFactory().createSocket(socket, host, port, autoClose);
53+
}
54+
55+
@Override
56+
public Socket createSocket() throws IOException {
57+
return sslContext.getSocketFactory().createSocket();
58+
}
59+
60+
public static KeyStore getKeystore() {
61+
KeyStore trustStore = null;
62+
try {
63+
trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
64+
trustStore.load(null, null);
65+
} catch (Throwable t) {
66+
t.printStackTrace();
67+
}
68+
return trustStore;
69+
}
70+
71+
public static SSLSocketFactory getFixedSocketFactory() {
72+
SSLSocketFactory socketFactory;
73+
try {
74+
socketFactory = new MySSLSocketFactory(getKeystore());
75+
socketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
76+
} catch (Throwable t) {
77+
t.printStackTrace();
78+
socketFactory = SSLSocketFactory.getSocketFactory();
79+
}
80+
return socketFactory;
81+
}
82+
83+
}

0 commit comments

Comments
 (0)