1
0
mirror of https://github.com/owncloud/android-library.git synced 2025-06-07 16:06:08 +00:00

clean up HttpClient

This commit is contained in:
Christian Schabesberger 2020-09-17 16:32:08 +02:00
parent 46028c5c25
commit 412047b4c8

View File

@ -64,35 +64,45 @@ public class HttpClient {
final X509TrustManager trustManager = new AdvancedX509TrustManager( final X509TrustManager trustManager = new AdvancedX509TrustManager(
NetworkUtils.getKnownServersStore(sContext)); NetworkUtils.getKnownServersStore(sContext));
SSLContext sslContext; final SSLContext sslContext = getSSLContext();
sslContext.init(null, new TrustManager[]{trustManager}, null);
final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
// Automatic cookie handling, NOT PERSISTENT
final CookieJar cookieJar = getNewCookieJar();
sOkHttpClient = buildOkHttpClient(cookieJar, sslSocketFactory, trustManager);
} catch (Exception e) {
Timber.e(e, "Could not setup SSL system.");
}
}
return sOkHttpClient;
}
private static SSLContext getSSLContext() throws NoSuchAlgorithmException {
try { try {
sslContext = SSLContext.getInstance("TLSv1.3"); return SSLContext.getInstance("TLSv1.3");
} catch (NoSuchAlgorithmException tlsv13Exception) { } catch (NoSuchAlgorithmException tlsv13Exception) {
try { try {
Timber.w("TLSv1.3 is not supported in this device; falling through TLSv1.2"); Timber.w("TLSv1.3 is not supported in this device; falling through TLSv1.2");
sslContext = SSLContext.getInstance("TLSv1.2"); return SSLContext.getInstance("TLSv1.2");
} catch (NoSuchAlgorithmException tlsv12Exception) { } catch (NoSuchAlgorithmException tlsv12Exception) {
try { try {
Timber.w("TLSv1.2 is not supported in this device; falling through TLSv1.1"); Timber.w("TLSv1.2 is not supported in this device; falling through TLSv1.1");
sslContext = SSLContext.getInstance("TLSv1.1"); return SSLContext.getInstance("TLSv1.1");
} catch (NoSuchAlgorithmException tlsv11Exception) { } catch (NoSuchAlgorithmException tlsv11Exception) {
Timber.w("TLSv1.1 is not supported in this device; falling through TLSv1.0"); Timber.w("TLSv1.1 is not supported in this device; falling through TLSv1.0");
sslContext = SSLContext.getInstance("TLSv1"); return SSLContext.getInstance("TLSv1");
// should be available in any device; see reference of supported protocols in // should be available in any device; see reference of supported protocols in
// http://developer.android.com/reference/javax/net/ssl/SSLSocket.html // http://developer.android.com/reference/javax/net/ssl/SSLSocket.html
} }
} }
} }
}
sslContext.init(null, new TrustManager[]{trustManager}, null); private static CookieJar getNewCookieJar() {
return new CookieJar() {
SSLSocketFactory sslSocketFactory;
sslSocketFactory = sslContext.getSocketFactory();
// Automatic cookie handling, NOT PERSISTENT
CookieJar cookieJar = new CookieJar() {
@Override @Override
public void saveFromResponse(HttpUrl url, List<Cookie> cookies) { public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
// Avoid duplicated cookies // Avoid duplicated cookies
@ -100,15 +110,20 @@ public class HttpClient {
List<Cookie> nonDuplicatedCookiesList = new ArrayList<>(nonDuplicatedCookiesSet); List<Cookie> nonDuplicatedCookiesList = new ArrayList<>(nonDuplicatedCookiesSet);
sCookieStore.put(url.host(), nonDuplicatedCookiesList); sCookieStore.put(url.host(), nonDuplicatedCookiesList);
System.out.println("set cookiestore size " + url.toString() + " " + sCookieStore.size());
} }
@Override @Override
public List<Cookie> loadForRequest(HttpUrl url) { public List<Cookie> loadForRequest(HttpUrl url) {
System.out.println("get cookiestore size " + url.toString() + " " + sCookieStore.size());
List<Cookie> cookies = sCookieStore.get(url.host()); List<Cookie> cookies = sCookieStore.get(url.host());
return cookies != null ? cookies : new ArrayList<>(); return cookies != null ? cookies : new ArrayList<>();
} }
}; };
}
private static OkHttpClient buildOkHttpClient(CookieJar cookieJar, SSLSocketFactory sslSocketFactory,
X509TrustManager trustManager) {
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder() OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder()
.protocols(Arrays.asList(Protocol.HTTP_1_1)) .protocols(Arrays.asList(Protocol.HTTP_1_1))
.readTimeout(HttpConstants.DEFAULT_DATA_TIMEOUT, TimeUnit.MILLISECONDS) .readTimeout(HttpConstants.DEFAULT_DATA_TIMEOUT, TimeUnit.MILLISECONDS)
@ -116,17 +131,11 @@ public class HttpClient {
.connectTimeout(HttpConstants.DEFAULT_CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS) .connectTimeout(HttpConstants.DEFAULT_CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS)
.followRedirects(false) .followRedirects(false)
.sslSocketFactory(sslSocketFactory, trustManager) .sslSocketFactory(sslSocketFactory, trustManager)
.hostnameVerifier((asdf, usdf) -> true) .hostnameVerifier((placeholder1, placeholder2) -> true)
.cookieJar(cookieJar); .cookieJar(cookieJar);
// TODO: Not verifying the hostname against certificate. ask owncloud security human if this is ok. // TODO: Not verifying the hostname against certificate. ask owncloud security human if this is ok.
//.hostnameVerifier(new BrowserCompatHostnameVerifier()); //.hostnameVerifier(new BrowserCompatHostnameVerifier());
sOkHttpClient = clientBuilder.build(); return clientBuilder.build();
} catch (Exception e) {
Timber.e(e, "Could not setup SSL system.");
}
}
return sOkHttpClient;
} }
public Context getContext() { public Context getContext() {