Merge pull request #3716 from ByteHamster/only-one-httpclient

Do not create new httpclient for each download
This commit is contained in:
H. Lehmann 2020-01-11 17:54:54 +01:00 committed by GitHub
commit 9b50cbbe0e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 73 additions and 106 deletions

View File

@ -11,6 +11,7 @@ import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.model.ModelLoader;
import com.bumptech.glide.load.model.ModelLoaderFactory;
import de.danoeh.antennapod.core.service.BasicAuthorizationInterceptor;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
@ -49,7 +50,6 @@ class ApOkHttpUrlLoader implements ModelLoader<String, InputStream> {
if (internalClient == null) {
OkHttpClient.Builder builder = AntennapodHttpClient.newBuilder();
builder.interceptors().add(new NetworkAllowanceInterceptor());
builder.interceptors().add(new BasicAuthenticationInterceptor());
internalClient = builder.build();
}
}
@ -123,48 +123,5 @@ class ApOkHttpUrlLoader implements ModelLoader<String, InputStream> {
.build();
}
}
}
private static class BasicAuthenticationInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
String url = request.url().toString();
String authentication = DBReader.getImageAuthentication(url);
if(TextUtils.isEmpty(authentication)) {
Log.d(TAG, "no credentials for '" + url + "'");
return chain.proceed(request);
}
// add authentication
String[] auth = authentication.split(":");
if (auth.length != 2) {
Log.d(TAG, "Invalid credentials for '" + url + "'");
return chain.proceed(request);
}
String credentials = HttpDownloader.encodeCredentials(auth[0], auth[1], "ISO-8859-1");
Request newRequest = request
.newBuilder()
.addHeader("Authorization", credentials)
.build();
Log.d(TAG, "Basic authentication with ISO-8859-1 encoding");
Response response = chain.proceed(newRequest);
if (!response.isSuccessful() && response.code() == HttpURLConnection.HTTP_UNAUTHORIZED) {
credentials = HttpDownloader.encodeCredentials(auth[0], auth[1], "UTF-8");
newRequest = request
.newBuilder()
.addHeader("Authorization", credentials)
.build();
Log.d(TAG, "Basic authentication with UTF-8 encoding");
return chain.proceed(newRequest);
} else {
return response;
}
}
}
}

View File

@ -0,0 +1,67 @@
package de.danoeh.antennapod.core.service;
import android.text.TextUtils;
import android.util.Log;
import androidx.annotation.NonNull;
import de.danoeh.antennapod.core.service.download.DownloadRequest;
import de.danoeh.antennapod.core.service.download.HttpDownloader;
import de.danoeh.antennapod.core.storage.DBReader;
import de.danoeh.antennapod.core.util.URIUtil;
import java.io.IOException;
import java.net.HttpURLConnection;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
public class BasicAuthorizationInterceptor implements Interceptor {
private static final String TAG = "BasicAuthInterceptor";
@Override
@NonNull
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Response response = chain.proceed(request);
if (response.code() != HttpURLConnection.HTTP_UNAUTHORIZED) {
return response;
}
String userInfo;
if (request.tag() instanceof DownloadRequest) {
DownloadRequest downloadRequest = (DownloadRequest) request.tag();
userInfo = URIUtil.getURIFromRequestUrl(downloadRequest.getSource()).getUserInfo();
if (TextUtils.isEmpty(userInfo)) {
userInfo = downloadRequest.getUsername() + ":" + downloadRequest.getPassword();
}
} else {
userInfo = DBReader.getImageAuthentication(request.url().toString());
}
if (TextUtils.isEmpty(userInfo)) {
Log.d(TAG, "no credentials for '" + request.url() + "'");
return response;
}
String[] parts = userInfo.split(":");
if (parts.length != 2) {
Log.d(TAG, "Invalid credentials for '" + request.url() + "'");
return response;
}
Request.Builder newRequest = request.newBuilder();
Log.d(TAG, "Authorization failed, re-trying with ISO-8859-1 encoded credentials");
String credentials = HttpDownloader.encodeCredentials(parts[0], parts[1], "ISO-8859-1");
newRequest.header("Authorization", credentials);
response = chain.proceed(newRequest.build());
if (response.code() != HttpURLConnection.HTTP_UNAUTHORIZED) {
return response;
}
Log.d(TAG, "Authorization failed, re-trying with UTF-8 encoded credentials");
credentials = HttpDownloader.encodeCredentials(parts[0], parts[1], "UTF-8");
newRequest.header("Authorization", credentials);
return chain.proceed(newRequest.build());
}
}

View File

@ -5,6 +5,7 @@ import androidx.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;
import de.danoeh.antennapod.core.service.BasicAuthorizationInterceptor;
import java.io.IOException;
import java.net.CookieManager;
import java.net.CookiePolicy;
@ -112,6 +113,7 @@ public class AntennapodHttpClient {
}
return response;
});
builder.interceptors().add(new BasicAuthorizationInterceptor());
// set cookie handler
CookieManager cm = new CookieManager();

View File

@ -4,6 +4,7 @@ import androidx.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;
import de.danoeh.antennapod.core.service.BasicAuthorizationInterceptor;
import org.apache.commons.io.IOUtils;
import java.io.BufferedInputStream;
@ -54,9 +55,7 @@ public class HttpDownloader extends Downloader {
return;
}
OkHttpClient.Builder httpClientBuilder = AntennapodHttpClient.newBuilder();
httpClientBuilder.interceptors().add(new BasicAuthorizationInterceptor(request));
OkHttpClient httpClient = httpClientBuilder.build();
OkHttpClient httpClient = AntennapodHttpClient.getHttpClient();
RandomAccessFile out = null;
InputStream connection;
ResponseBody responseBody = null;
@ -65,6 +64,7 @@ public class HttpDownloader extends Downloader {
final URI uri = URIUtil.getURIFromRequestUrl(request.getSource());
Request.Builder httpReq = new Request.Builder().url(uri.toURL())
.header("User-Agent", ClientConfig.USER_AGENT);
httpReq.tag(request);
if (request.getFeedfileType() == FeedMedia.FEEDFILETYPE_FEEDMEDIA) {
// set header explicitly so that okhttp doesn't do transparent gzip
Log.d(TAG, "addHeader(\"Accept-Encoding\", \"identity\")");
@ -308,63 +308,4 @@ public class HttpDownloader extends Downloader {
throw new AssertionError(e);
}
}
private static class BasicAuthorizationInterceptor implements Interceptor {
private final DownloadRequest downloadRequest;
public BasicAuthorizationInterceptor(DownloadRequest downloadRequest) {
this.downloadRequest = downloadRequest;
}
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
String userInfo = URIUtil.getURIFromRequestUrl(downloadRequest.getSource()).getUserInfo();
Response response = chain.proceed(request);
if (response.code() != HttpURLConnection.HTTP_UNAUTHORIZED) {
return response;
}
Request.Builder newRequest = request.newBuilder();
Log.d(TAG, "Authorization failed, re-trying with ISO-8859-1 encoded credentials");
if (userInfo != null) {
String[] parts = userInfo.split(":");
if (parts.length == 2) {
String credentials = encodeCredentials(parts[0], parts[1], "ISO-8859-1");
newRequest.header("Authorization", credentials);
}
} else if (!TextUtils.isEmpty(downloadRequest.getUsername()) && downloadRequest.getPassword() != null) {
String credentials = encodeCredentials(downloadRequest.getUsername(), downloadRequest.getPassword(),
"ISO-8859-1");
newRequest.header("Authorization", credentials);
}
response = chain.proceed(newRequest.build());
if (response.code() != HttpURLConnection.HTTP_UNAUTHORIZED) {
return response;
}
Log.d(TAG, "Authorization failed, re-trying with UTF-8 encoded credentials");
if (userInfo != null) {
String[] parts = userInfo.split(":");
if (parts.length == 2) {
String credentials = encodeCredentials(parts[0], parts[1], "UTF-8");
newRequest.header("Authorization", credentials);
}
} else if (!TextUtils.isEmpty(downloadRequest.getUsername()) && downloadRequest.getPassword() != null) {
String credentials = encodeCredentials(downloadRequest.getUsername(), downloadRequest.getPassword(),
"UTF-8");
newRequest.header("Authorization", credentials);
}
return chain.proceed(newRequest.build());
}
}
}