2015-10-10 13:47:44 +00:00
|
|
|
|
/*
|
|
|
|
|
* Copyright © 2013 – 2015 Ricki Hirner (bitfire web engineering).
|
|
|
|
|
* All rights reserved. This program and the accompanying materials
|
|
|
|
|
* are made available under the terms of the GNU Public License v3.0
|
|
|
|
|
* which accompanies this distribution, and is available at
|
|
|
|
|
* http://www.gnu.org/licenses/gpl.html
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package at.bitfire.davdroid;
|
|
|
|
|
|
2015-10-13 00:34:24 +00:00
|
|
|
|
import android.content.Context;
|
2015-10-11 23:56:28 +00:00
|
|
|
|
import android.os.Build;
|
|
|
|
|
|
2015-10-18 14:20:26 +00:00
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
|
2015-10-18 22:04:58 +00:00
|
|
|
|
import java.io.BufferedReader;
|
2015-10-10 13:47:44 +00:00
|
|
|
|
import java.io.IOException;
|
2015-10-18 22:04:58 +00:00
|
|
|
|
import java.io.StringReader;
|
2015-10-11 23:56:28 +00:00
|
|
|
|
import java.text.SimpleDateFormat;
|
2015-11-27 13:04:24 +00:00
|
|
|
|
import java.util.Date;
|
2015-10-18 22:04:58 +00:00
|
|
|
|
import java.util.Locale;
|
2015-10-10 13:47:44 +00:00
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
2015-10-16 17:29:33 +00:00
|
|
|
|
import at.bitfire.dav4android.BasicDigestAuthenticator;
|
2015-10-13 00:34:24 +00:00
|
|
|
|
import de.duenndns.ssl.MemorizingTrustManager;
|
2016-01-17 16:10:30 +00:00
|
|
|
|
import lombok.NonNull;
|
2015-10-10 13:47:44 +00:00
|
|
|
|
import lombok.RequiredArgsConstructor;
|
2016-01-17 16:10:30 +00:00
|
|
|
|
import okhttp3.Credentials;
|
|
|
|
|
import okhttp3.Interceptor;
|
|
|
|
|
import okhttp3.OkHttpClient;
|
|
|
|
|
import okhttp3.Request;
|
|
|
|
|
import okhttp3.Response;
|
|
|
|
|
import okhttp3.internal.tls.OkHostnameVerifier;
|
|
|
|
|
import okhttp3.logging.HttpLoggingInterceptor;
|
2015-10-10 13:47:44 +00:00
|
|
|
|
|
2016-01-16 20:34:41 +00:00
|
|
|
|
public class HttpClient {
|
|
|
|
|
private static final int MAX_LOG_LINE_LENGTH = 85;
|
2015-10-18 22:04:58 +00:00
|
|
|
|
|
2016-01-16 20:34:41 +00:00
|
|
|
|
private static final OkHttpClient client = new OkHttpClient();
|
|
|
|
|
private static final UserAgentInterceptor userAgentInterceptor = new UserAgentInterceptor();
|
2015-10-11 23:56:28 +00:00
|
|
|
|
|
2016-01-16 20:34:41 +00:00
|
|
|
|
private static final String userAgent;
|
2015-10-11 23:56:28 +00:00
|
|
|
|
static {
|
2015-11-27 13:04:24 +00:00
|
|
|
|
String date = new SimpleDateFormat("yyyy/MM/dd", Locale.US).format(new Date(BuildConfig.buildTime));
|
2016-01-16 20:34:41 +00:00
|
|
|
|
userAgent = "DAVdroid/" + BuildConfig.VERSION_NAME + " (" + date + "; dav4android; okhttp3) Android/" + Build.VERSION.RELEASE;
|
2015-10-11 23:56:28 +00:00
|
|
|
|
}
|
2015-10-10 13:47:44 +00:00
|
|
|
|
|
2016-01-16 20:34:41 +00:00
|
|
|
|
private HttpClient() {
|
|
|
|
|
}
|
2015-10-10 13:47:44 +00:00
|
|
|
|
|
2016-01-16 20:34:41 +00:00
|
|
|
|
public static OkHttpClient create(Context context) {
|
|
|
|
|
OkHttpClient.Builder builder = client.newBuilder();
|
2015-10-13 00:34:24 +00:00
|
|
|
|
|
|
|
|
|
if (context != null) {
|
|
|
|
|
// use MemorizingTrustManager to manage self-signed certificates
|
|
|
|
|
MemorizingTrustManager mtm = new MemorizingTrustManager(context);
|
2016-01-16 20:34:41 +00:00
|
|
|
|
builder.sslSocketFactory(new SSLSocketFactoryCompat(mtm));
|
|
|
|
|
builder.hostnameVerifier(mtm.wrapHostnameVerifier(OkHostnameVerifier.INSTANCE));
|
2015-10-13 00:34:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-10-12 12:16:26 +00:00
|
|
|
|
// set timeouts
|
2016-01-16 20:34:41 +00:00
|
|
|
|
builder.connectTimeout(30, TimeUnit.SECONDS);
|
|
|
|
|
builder.writeTimeout(30, TimeUnit.SECONDS);
|
|
|
|
|
builder.readTimeout(120, TimeUnit.SECONDS);
|
|
|
|
|
|
|
|
|
|
// don't allow redirects, because it would break PROPFIND handling
|
|
|
|
|
builder.followRedirects(false);
|
2015-10-11 23:56:28 +00:00
|
|
|
|
|
|
|
|
|
// add User-Agent to every request
|
2016-01-16 20:34:41 +00:00
|
|
|
|
builder.addNetworkInterceptor(userAgentInterceptor);
|
2015-10-12 12:16:26 +00:00
|
|
|
|
|
2015-11-08 17:51:19 +00:00
|
|
|
|
// add cookie store for non-persistent cookies (some services like Horde use cookies for session tracking)
|
2016-01-16 20:34:41 +00:00
|
|
|
|
builder.cookieJar(MemoryCookieStore.INSTANCE);
|
2015-11-08 17:51:19 +00:00
|
|
|
|
|
2016-01-16 20:34:41 +00:00
|
|
|
|
return builder.build();
|
2015-10-10 13:47:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-01-16 20:34:41 +00:00
|
|
|
|
public static OkHttpClient addAuthentication(@NonNull OkHttpClient httpClient, @NonNull String username, @NonNull String password, boolean preemptive) {
|
|
|
|
|
OkHttpClient.Builder builder = httpClient.newBuilder();
|
2015-10-17 09:33:35 +00:00
|
|
|
|
if (preemptive)
|
2016-01-16 20:34:41 +00:00
|
|
|
|
builder.addNetworkInterceptor(new PreemptiveAuthenticationInterceptor(username, password));
|
2015-10-17 09:33:35 +00:00
|
|
|
|
else
|
2016-01-16 20:34:41 +00:00
|
|
|
|
builder.authenticator(new BasicDigestAuthenticator(null, username, password));
|
|
|
|
|
return builder.build();
|
2015-10-17 09:33:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-01-17 16:10:30 +00:00
|
|
|
|
public static OkHttpClient addAuthentication(@NonNull OkHttpClient httpClient, @NonNull String host, @NonNull String username, @NonNull String password) {
|
2016-01-16 20:34:41 +00:00
|
|
|
|
return httpClient.newBuilder()
|
|
|
|
|
.authenticator(new BasicDigestAuthenticator(host, username, password))
|
|
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static OkHttpClient addLogger(@NonNull OkHttpClient httpClient, @NonNull final Logger logger) {
|
|
|
|
|
// enable verbose logs, if requested
|
|
|
|
|
if (logger.isTraceEnabled()) {
|
|
|
|
|
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
|
|
|
|
|
@Override
|
|
|
|
|
public void log(String message) {
|
|
|
|
|
BufferedReader reader = new BufferedReader(new StringReader(message));
|
|
|
|
|
String line;
|
|
|
|
|
try {
|
|
|
|
|
while ((line = reader.readLine()) != null) {
|
|
|
|
|
int len = line.length();
|
|
|
|
|
for (int pos = 0; pos < len; pos += MAX_LOG_LINE_LENGTH)
|
|
|
|
|
if (pos < len - MAX_LOG_LINE_LENGTH)
|
|
|
|
|
logger.trace(line.substring(pos, pos + MAX_LOG_LINE_LENGTH) + "\\");
|
|
|
|
|
else
|
|
|
|
|
logger.trace(line.substring(pos));
|
|
|
|
|
}
|
|
|
|
|
} catch(IOException e) {
|
|
|
|
|
// for some reason, we couldn't split our message
|
|
|
|
|
logger.trace(message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
|
2015-10-17 09:33:35 +00:00
|
|
|
|
|
2016-01-16 20:34:41 +00:00
|
|
|
|
return httpClient.newBuilder()
|
|
|
|
|
.addInterceptor(loggingInterceptor)
|
|
|
|
|
.build();
|
|
|
|
|
} else
|
|
|
|
|
return httpClient;
|
2015-10-11 23:56:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static class UserAgentInterceptor implements Interceptor {
|
|
|
|
|
@Override
|
|
|
|
|
public Response intercept(Chain chain) throws IOException {
|
2016-01-16 23:34:26 +00:00
|
|
|
|
Locale locale = Locale.getDefault();
|
2015-10-11 23:56:28 +00:00
|
|
|
|
Request request = chain.request().newBuilder()
|
|
|
|
|
.header("User-Agent", userAgent)
|
2016-01-16 23:34:26 +00:00
|
|
|
|
.header("Accept-Language", locale.getLanguage() + "-" + locale.getCountry() + ", " + locale.getLanguage() + ";q=0.7, *;q=0.5")
|
2015-10-11 23:56:28 +00:00
|
|
|
|
.build();
|
|
|
|
|
return chain.proceed(request);
|
|
|
|
|
}
|
2015-10-10 13:47:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@RequiredArgsConstructor
|
|
|
|
|
static class PreemptiveAuthenticationInterceptor implements Interceptor {
|
|
|
|
|
final String username, password;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Response intercept(Chain chain) throws IOException {
|
2015-10-11 23:56:28 +00:00
|
|
|
|
Request request = chain.request().newBuilder()
|
2015-10-10 13:47:44 +00:00
|
|
|
|
.header("Authorization", Credentials.basic(username, password))
|
|
|
|
|
.build();
|
|
|
|
|
return chain.proceed(request);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|