Search in sources :

Example 1 with UserAgentInterceptor

use of de.danoeh.antennapod.core.service.UserAgentInterceptor in project AntennaPod by AntennaPod.

the class AntennapodHttpClient method newBuilder.

/**
 * Creates a new HTTP client.  Most users should just use
 * getHttpClient() to get the standard AntennaPod client,
 * but sometimes it's necessary for others to have their own
 * copy so that the clients don't share state.
 * @return http client
 */
@NonNull
public static OkHttpClient.Builder newBuilder() {
    Log.d(TAG, "Creating new instance of HTTP client");
    System.setProperty("http.maxConnections", String.valueOf(MAX_CONNECTIONS));
    OkHttpClient.Builder builder = new OkHttpClient.Builder();
    // detect 301 Moved permanently and 308 Permanent Redirect
    builder.networkInterceptors().add(chain -> {
        Request request = chain.request();
        Response response = chain.proceed(request);
        if (response.code() == HttpURLConnection.HTTP_MOVED_PERM || response.code() == StatusLine.HTTP_PERM_REDIRECT) {
            String location = response.header("Location");
            if (location == null) {
                return response;
            }
            if (location.startsWith("/")) {
                // URL is not absolute, but relative
                HttpUrl url = request.url();
                location = url.scheme() + "://" + url.host() + location;
            } else if (!location.toLowerCase().startsWith("http://") && !location.toLowerCase().startsWith("https://")) {
                // Reference is relative to current path
                HttpUrl url = request.url();
                String path = url.encodedPath();
                String newPath = path.substring(0, path.lastIndexOf("/") + 1) + location;
                location = url.scheme() + "://" + url.host() + newPath;
            }
            try {
                DBWriter.updateFeedDownloadURL(request.url().toString(), location).get();
            } catch (Exception e) {
                Log.e(TAG, Log.getStackTraceString(e));
            }
        }
        return response;
    });
    builder.interceptors().add(new BasicAuthorizationInterceptor());
    builder.networkInterceptors().add(new UserAgentInterceptor());
    // set cookie handler
    CookieManager cm = new CookieManager();
    cm.setCookiePolicy(CookiePolicy.ACCEPT_ORIGINAL_SERVER);
    builder.cookieJar(new JavaNetCookieJar(cm));
    // set timeouts
    builder.connectTimeout(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
    builder.readTimeout(READ_TIMEOUT, TimeUnit.MILLISECONDS);
    builder.writeTimeout(READ_TIMEOUT, TimeUnit.MILLISECONDS);
    // 20MB
    builder.cache(new Cache(cacheDirectory, 20L * 1000000));
    // configure redirects
    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) {
            builder.proxyAuthenticator((route, response) -> {
                String credentials = Credentials.basic(config.username, config.password);
                return response.request().newBuilder().header("Proxy-Authorization", credentials).build();
            });
        }
    }
    SslClientSetup.installCertificates(builder);
    return builder;
}
Also used : JavaNetCookieJar(okhttp3.JavaNetCookieJar) OkHttpClient(okhttp3.OkHttpClient) UserAgentInterceptor(de.danoeh.antennapod.core.service.UserAgentInterceptor) Request(okhttp3.Request) BasicAuthorizationInterceptor(de.danoeh.antennapod.core.service.BasicAuthorizationInterceptor) HttpUrl(okhttp3.HttpUrl) Response(okhttp3.Response) Proxy(java.net.Proxy) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) CookieManager(java.net.CookieManager) Cache(okhttp3.Cache) NonNull(androidx.annotation.NonNull)

Aggregations

NonNull (androidx.annotation.NonNull)1 BasicAuthorizationInterceptor (de.danoeh.antennapod.core.service.BasicAuthorizationInterceptor)1 UserAgentInterceptor (de.danoeh.antennapod.core.service.UserAgentInterceptor)1 CookieManager (java.net.CookieManager)1 InetSocketAddress (java.net.InetSocketAddress)1 Proxy (java.net.Proxy)1 SocketAddress (java.net.SocketAddress)1 Cache (okhttp3.Cache)1 HttpUrl (okhttp3.HttpUrl)1 JavaNetCookieJar (okhttp3.JavaNetCookieJar)1 OkHttpClient (okhttp3.OkHttpClient)1 Request (okhttp3.Request)1 Response (okhttp3.Response)1