Merge pull request #5854 from ByteHamster/decouple-httpclient

Decouple HttpClient
This commit is contained in:
ByteHamster 2022-04-25 22:16:26 +02:00 committed by GitHub
commit 26faaf52c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 38 additions and 29 deletions

View File

@ -165,7 +165,9 @@ public class ProxyDialog {
if (!TextUtils.isEmpty(port)) {
portValue = Integer.parseInt(port);
}
UserPreferences.setProxyConfig(new ProxyConfig(typeEnum, host, portValue, username, password));
ProxyConfig config = new ProxyConfig(typeEnum, host, portValue, username, password);
UserPreferences.setProxyConfig(config);
AntennapodHttpClient.setProxyConfig(config);
}
private final TextWatcher requireTestOnChange = new TextWatcher() {

View File

@ -43,6 +43,7 @@ public class ClientConfig {
SslProviderInstaller.install(context);
NetworkUtils.init(context);
AntennapodHttpClient.setCacheDirectory(new File(context.getCacheDir(), "okhttp"));
AntennapodHttpClient.setProxyConfig(UserPreferences.getProxyConfig());
SleepTimerPreferences.init(context);
NotificationUtils.createChannels(context);
initialized = true;

View File

@ -4,7 +4,7 @@ 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.service.download.HttpCredentialEncoder;
import de.danoeh.antennapod.core.storage.DBReader;
import de.danoeh.antennapod.core.util.URIUtil;
import java.io.IOException;
@ -67,8 +67,7 @@ public class BasicAuthorizationInterceptor implements Interceptor {
String password = userInfo.substring(userInfo.indexOf(':') + 1);
Log.d(TAG, "Authorization failed, re-trying with ISO-8859-1 encoded credentials");
String credentials = HttpDownloader.encodeCredentials(username, password, "ISO-8859-1");
newRequest.header(HEADER_AUTHORIZATION, credentials);
newRequest.header(HEADER_AUTHORIZATION, HttpCredentialEncoder.encode(username, password, "ISO-8859-1"));
response = chain.proceed(newRequest.build());
if (response.code() != HttpURLConnection.HTTP_UNAUTHORIZED) {
@ -76,8 +75,7 @@ public class BasicAuthorizationInterceptor implements Interceptor {
}
Log.d(TAG, "Authorization failed, re-trying with UTF-8 encoded credentials");
credentials = HttpDownloader.encodeCredentials(username, password, "UTF-8");
newRequest.header(HEADER_AUTHORIZATION, credentials);
newRequest.header(HEADER_AUTHORIZATION, HttpCredentialEncoder.encode(username, password, "UTF-8"));
return chain.proceed(newRequest.build());
}
}

View File

@ -3,7 +3,6 @@ package de.danoeh.antennapod.core.service.download;
import android.text.TextUtils;
import android.util.Log;
import androidx.annotation.NonNull;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.service.BasicAuthorizationInterceptor;
import de.danoeh.antennapod.core.service.UserAgentInterceptor;
import de.danoeh.antennapod.net.ssl.SslClientSetup;
@ -28,6 +27,7 @@ public class AntennapodHttpClient {
private static final int READ_TIMEOUT = 30000;
private static final int MAX_CONNECTIONS = 8;
private static File cacheDirectory;
private static ProxyConfig proxyConfig;
private static volatile OkHttpClient httpClient = null;
@ -81,14 +81,13 @@ public class AntennapodHttpClient {
builder.followRedirects(true);
builder.followSslRedirects(true);
ProxyConfig config = UserPreferences.getProxyConfig();
if (config.type != Proxy.Type.DIRECT && !TextUtils.isEmpty(config.host)) {
int port = config.port > 0 ? config.port : ProxyConfig.DEFAULT_PORT;
SocketAddress address = InetSocketAddress.createUnresolved(config.host, port);
builder.proxy(new Proxy(config.type, address));
if (!TextUtils.isEmpty(config.username) && config.password != null) {
if (proxyConfig != null && proxyConfig.type != Proxy.Type.DIRECT && !TextUtils.isEmpty(proxyConfig.host)) {
int port = proxyConfig.port > 0 ? proxyConfig.port : ProxyConfig.DEFAULT_PORT;
SocketAddress address = InetSocketAddress.createUnresolved(proxyConfig.host, port);
builder.proxy(new Proxy(proxyConfig.type, address));
if (!TextUtils.isEmpty(proxyConfig.username) && proxyConfig.password != null) {
builder.proxyAuthenticator((route, response) -> {
String credentials = Credentials.basic(config.username, config.password);
String credentials = Credentials.basic(proxyConfig.username, proxyConfig.password);
return response.request().newBuilder()
.header("Proxy-Authorization", credentials)
.build();
@ -103,4 +102,8 @@ public class AntennapodHttpClient {
public static void setCacheDirectory(File cacheDirectory) {
AntennapodHttpClient.cacheDirectory = cacheDirectory;
}
public static void setProxyConfig(ProxyConfig proxyConfig) {
AntennapodHttpClient.proxyConfig = proxyConfig;
}
}

View File

@ -0,0 +1,18 @@
package de.danoeh.antennapod.core.service.download;
import okio.ByteString;
import java.io.UnsupportedEncodingException;
public abstract class HttpCredentialEncoder {
public static String encode(String username, String password, String charset) {
try {
String credentials = username + ":" + password;
byte[] bytes = credentials.getBytes(charset);
String encoded = ByteString.of(bytes).base64();
return "Basic " + encoded;
} catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
}
}

View File

@ -15,7 +15,6 @@ import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URI;
@ -35,7 +34,6 @@ import okhttp3.Protocol;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okio.ByteString;
public class HttpDownloader extends Downloader {
private static final String TAG = "HttpDownloader";
@ -338,15 +336,4 @@ public class HttpDownloader extends Downloader {
}
}
}
public static String encodeCredentials(String username, String password, String charset) {
try {
String credentials = username + ":" + password;
byte[] bytes = credentials.getBytes(charset);
String encoded = ByteString.of(bytes).base64();
return "Basic " + encoded;
} catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
}
}

View File

@ -38,7 +38,7 @@ import de.danoeh.antennapod.core.ClientConfig;
import de.danoeh.antennapod.core.R;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.service.download.AntennapodHttpClient;
import de.danoeh.antennapod.core.service.download.HttpDownloader;
import de.danoeh.antennapod.core.service.download.HttpCredentialEncoder;
import de.danoeh.antennapod.core.util.NetworkUtils;
import de.danoeh.antennapod.core.util.playback.IPlayer;
import de.danoeh.antennapod.playback.base.PlaybackServiceMediaPlayer;
@ -222,7 +222,7 @@ public class ExoPlayerWrapper implements IPlayer {
final HashMap<String, String> requestProperties = new HashMap<>();
requestProperties.put(
"Authorization",
HttpDownloader.encodeCredentials(user, password, "ISO-8859-1")
HttpCredentialEncoder.encode(user, password, "ISO-8859-1")
);
httpDataSourceFactory.setDefaultRequestProperties(requestProperties);
}