Merge pull request #5854 from ByteHamster/decouple-httpclient
Decouple HttpClient
This commit is contained in:
commit
26faaf52c6
@ -165,7 +165,9 @@ public class ProxyDialog {
|
|||||||
if (!TextUtils.isEmpty(port)) {
|
if (!TextUtils.isEmpty(port)) {
|
||||||
portValue = Integer.parseInt(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() {
|
private final TextWatcher requireTestOnChange = new TextWatcher() {
|
||||||
|
@ -43,6 +43,7 @@ public class ClientConfig {
|
|||||||
SslProviderInstaller.install(context);
|
SslProviderInstaller.install(context);
|
||||||
NetworkUtils.init(context);
|
NetworkUtils.init(context);
|
||||||
AntennapodHttpClient.setCacheDirectory(new File(context.getCacheDir(), "okhttp"));
|
AntennapodHttpClient.setCacheDirectory(new File(context.getCacheDir(), "okhttp"));
|
||||||
|
AntennapodHttpClient.setProxyConfig(UserPreferences.getProxyConfig());
|
||||||
SleepTimerPreferences.init(context);
|
SleepTimerPreferences.init(context);
|
||||||
NotificationUtils.createChannels(context);
|
NotificationUtils.createChannels(context);
|
||||||
initialized = true;
|
initialized = true;
|
||||||
|
@ -4,7 +4,7 @@ import android.text.TextUtils;
|
|||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import de.danoeh.antennapod.core.service.download.DownloadRequest;
|
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.storage.DBReader;
|
||||||
import de.danoeh.antennapod.core.util.URIUtil;
|
import de.danoeh.antennapod.core.util.URIUtil;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -67,8 +67,7 @@ public class BasicAuthorizationInterceptor implements Interceptor {
|
|||||||
String password = userInfo.substring(userInfo.indexOf(':') + 1);
|
String password = userInfo.substring(userInfo.indexOf(':') + 1);
|
||||||
|
|
||||||
Log.d(TAG, "Authorization failed, re-trying with ISO-8859-1 encoded credentials");
|
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, HttpCredentialEncoder.encode(username, password, "ISO-8859-1"));
|
||||||
newRequest.header(HEADER_AUTHORIZATION, credentials);
|
|
||||||
response = chain.proceed(newRequest.build());
|
response = chain.proceed(newRequest.build());
|
||||||
|
|
||||||
if (response.code() != HttpURLConnection.HTTP_UNAUTHORIZED) {
|
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");
|
Log.d(TAG, "Authorization failed, re-trying with UTF-8 encoded credentials");
|
||||||
credentials = HttpDownloader.encodeCredentials(username, password, "UTF-8");
|
newRequest.header(HEADER_AUTHORIZATION, HttpCredentialEncoder.encode(username, password, "UTF-8"));
|
||||||
newRequest.header(HEADER_AUTHORIZATION, credentials);
|
|
||||||
return chain.proceed(newRequest.build());
|
return chain.proceed(newRequest.build());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,6 @@ package de.danoeh.antennapod.core.service.download;
|
|||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import de.danoeh.antennapod.core.preferences.UserPreferences;
|
|
||||||
import de.danoeh.antennapod.core.service.BasicAuthorizationInterceptor;
|
import de.danoeh.antennapod.core.service.BasicAuthorizationInterceptor;
|
||||||
import de.danoeh.antennapod.core.service.UserAgentInterceptor;
|
import de.danoeh.antennapod.core.service.UserAgentInterceptor;
|
||||||
import de.danoeh.antennapod.net.ssl.SslClientSetup;
|
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 READ_TIMEOUT = 30000;
|
||||||
private static final int MAX_CONNECTIONS = 8;
|
private static final int MAX_CONNECTIONS = 8;
|
||||||
private static File cacheDirectory;
|
private static File cacheDirectory;
|
||||||
|
private static ProxyConfig proxyConfig;
|
||||||
|
|
||||||
private static volatile OkHttpClient httpClient = null;
|
private static volatile OkHttpClient httpClient = null;
|
||||||
|
|
||||||
@ -81,14 +81,13 @@ public class AntennapodHttpClient {
|
|||||||
builder.followRedirects(true);
|
builder.followRedirects(true);
|
||||||
builder.followSslRedirects(true);
|
builder.followSslRedirects(true);
|
||||||
|
|
||||||
ProxyConfig config = UserPreferences.getProxyConfig();
|
if (proxyConfig != null && proxyConfig.type != Proxy.Type.DIRECT && !TextUtils.isEmpty(proxyConfig.host)) {
|
||||||
if (config.type != Proxy.Type.DIRECT && !TextUtils.isEmpty(config.host)) {
|
int port = proxyConfig.port > 0 ? proxyConfig.port : ProxyConfig.DEFAULT_PORT;
|
||||||
int port = config.port > 0 ? config.port : ProxyConfig.DEFAULT_PORT;
|
SocketAddress address = InetSocketAddress.createUnresolved(proxyConfig.host, port);
|
||||||
SocketAddress address = InetSocketAddress.createUnresolved(config.host, port);
|
builder.proxy(new Proxy(proxyConfig.type, address));
|
||||||
builder.proxy(new Proxy(config.type, address));
|
if (!TextUtils.isEmpty(proxyConfig.username) && proxyConfig.password != null) {
|
||||||
if (!TextUtils.isEmpty(config.username) && config.password != null) {
|
|
||||||
builder.proxyAuthenticator((route, response) -> {
|
builder.proxyAuthenticator((route, response) -> {
|
||||||
String credentials = Credentials.basic(config.username, config.password);
|
String credentials = Credentials.basic(proxyConfig.username, proxyConfig.password);
|
||||||
return response.request().newBuilder()
|
return response.request().newBuilder()
|
||||||
.header("Proxy-Authorization", credentials)
|
.header("Proxy-Authorization", credentials)
|
||||||
.build();
|
.build();
|
||||||
@ -103,4 +102,8 @@ public class AntennapodHttpClient {
|
|||||||
public static void setCacheDirectory(File cacheDirectory) {
|
public static void setCacheDirectory(File cacheDirectory) {
|
||||||
AntennapodHttpClient.cacheDirectory = cacheDirectory;
|
AntennapodHttpClient.cacheDirectory = cacheDirectory;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void setProxyConfig(ProxyConfig proxyConfig) {
|
||||||
|
AntennapodHttpClient.proxyConfig = proxyConfig;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -15,7 +15,6 @@ import java.io.File;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.RandomAccessFile;
|
import java.io.RandomAccessFile;
|
||||||
import java.io.UnsupportedEncodingException;
|
|
||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
import java.net.SocketTimeoutException;
|
import java.net.SocketTimeoutException;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
@ -35,7 +34,6 @@ import okhttp3.Protocol;
|
|||||||
import okhttp3.Request;
|
import okhttp3.Request;
|
||||||
import okhttp3.Response;
|
import okhttp3.Response;
|
||||||
import okhttp3.ResponseBody;
|
import okhttp3.ResponseBody;
|
||||||
import okio.ByteString;
|
|
||||||
|
|
||||||
public class HttpDownloader extends Downloader {
|
public class HttpDownloader extends Downloader {
|
||||||
private static final String TAG = "HttpDownloader";
|
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -38,7 +38,7 @@ import de.danoeh.antennapod.core.ClientConfig;
|
|||||||
import de.danoeh.antennapod.core.R;
|
import de.danoeh.antennapod.core.R;
|
||||||
import de.danoeh.antennapod.core.preferences.UserPreferences;
|
import de.danoeh.antennapod.core.preferences.UserPreferences;
|
||||||
import de.danoeh.antennapod.core.service.download.AntennapodHttpClient;
|
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.NetworkUtils;
|
||||||
import de.danoeh.antennapod.core.util.playback.IPlayer;
|
import de.danoeh.antennapod.core.util.playback.IPlayer;
|
||||||
import de.danoeh.antennapod.playback.base.PlaybackServiceMediaPlayer;
|
import de.danoeh.antennapod.playback.base.PlaybackServiceMediaPlayer;
|
||||||
@ -222,7 +222,7 @@ public class ExoPlayerWrapper implements IPlayer {
|
|||||||
final HashMap<String, String> requestProperties = new HashMap<>();
|
final HashMap<String, String> requestProperties = new HashMap<>();
|
||||||
requestProperties.put(
|
requestProperties.put(
|
||||||
"Authorization",
|
"Authorization",
|
||||||
HttpDownloader.encodeCredentials(user, password, "ISO-8859-1")
|
HttpCredentialEncoder.encode(user, password, "ISO-8859-1")
|
||||||
);
|
);
|
||||||
httpDataSourceFactory.setDefaultRequestProperties(requestProperties);
|
httpDataSourceFactory.setDefaultRequestProperties(requestProperties);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user