package okhttp3.internal.http;

import androidx.core.app.NotificationCompat;
import com.home.utils.Utils;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.ProtocolException;
import java.net.Proxy;
import java.net.SocketTimeoutException;
import java.security.cert.CertificateException;
import javax.net.ssl.SSLHandshakeException;
import javax.net.ssl.SSLPeerUnverifiedException;
import kotlin.Metadata;
import kotlin.jvm.internal.Intrinsics;
import kotlin.text.Regex;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.Route;
import okhttp3.internal.Util;
import okhttp3.internal.connection.Exchange;
import okhttp3.internal.connection.RealCall;
import okhttp3.internal.connection.RealConnection;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpHead;

/* compiled from: RetryAndFollowUpInterceptor.kt */
@Metadata(bv = {1, 0, 3}, d1 = {"\u0000R\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u000e\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u000b\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0010\b\n\u0002\b\u0003\u0018\u0000 \u001e2\u00020\u0001:\u0001\u001eB\r\u0012\u0006\u0010\u0002\u001a\u00020\u0003¢\u0006\u0002\u0010\u0004J\u001a\u0010\u0005\u001a\u0004\u0018\u00010\u00062\u0006\u0010\u0007\u001a\u00020\b2\u0006\u0010\t\u001a\u00020\nH\u0002J\u001c\u0010\u000b\u001a\u0004\u0018\u00010\u00062\u0006\u0010\u0007\u001a\u00020\b2\b\u0010\f\u001a\u0004\u0018\u00010\rH\u0002J\u0010\u0010\u000e\u001a\u00020\b2\u0006\u0010\u000f\u001a\u00020\u0010H\u0016J\u0018\u0010\u0011\u001a\u00020\u00122\u0006\u0010\u0013\u001a\u00020\u00142\u0006\u0010\u0015\u001a\u00020\u0012H\u0002J(\u0010\u0016\u001a\u00020\u00122\u0006\u0010\u0013\u001a\u00020\u00142\u0006\u0010\u0017\u001a\u00020\u00182\u0006\u0010\u0019\u001a\u00020\u00062\u0006\u0010\u0015\u001a\u00020\u0012H\u0002J\u0018\u0010\u001a\u001a\u00020\u00122\u0006\u0010\u0013\u001a\u00020\u00142\u0006\u0010\u0019\u001a\u00020\u0006H\u0002J\u0018\u0010\u001b\u001a\u00020\u001c2\u0006\u0010\u0007\u001a\u00020\b2\u0006\u0010\u001d\u001a\u00020\u001cH\u0002R\u000e\u0010\u0002\u001a\u00020\u0003X\u0082\u0004¢\u0006\u0002\n\u0000¨\u0006\u001f"}, d2 = {"Lokhttp3/internal/http/RetryAndFollowUpInterceptor;", "Lokhttp3/Interceptor;", "client", "Lokhttp3/OkHttpClient;", "(Lokhttp3/OkHttpClient;)V", "buildRedirectRequest", "Lokhttp3/Request;", "userResponse", "Lokhttp3/Response;", Utils.RESPONSE_METHOD, "", "followUpRequest", "exchange", "Lokhttp3/internal/connection/Exchange;", "intercept", "chain", "Lokhttp3/Interceptor$Chain;", "isRecoverable", "", "e", "Ljava/io/IOException;", "requestSendStarted", "recover", NotificationCompat.CATEGORY_CALL, "Lokhttp3/internal/connection/RealCall;", "userRequest", "requestIsOneShot", "retryAfter", "", "defaultDelay", "Companion", "okhttp"}, k = 1, mv = {1, 1, 16})
/* loaded from: classes2.dex */
public final class RetryAndFollowUpInterceptor implements Interceptor {
    private static final int MAX_FOLLOW_UPS = 20;
    private final OkHttpClient client;

    public RetryAndFollowUpInterceptor(OkHttpClient client) {
        Intrinsics.checkParameterIsNotNull(client, "client");
        this.client = client;
    }

    /* JADX WARN: Code restructure failed: missing block: B:11:0x0025, code lost:
    
        if (r6 == null) goto L10;
     */
    /* JADX WARN: Code restructure failed: missing block: B:12:0x0027, code lost:
    
        r0 = r0.newBuilder().priorResponse(r6.newBuilder().body(null).build()).build();
     */
    /* JADX WARN: Code restructure failed: missing block: B:13:0x003f, code lost:
    
        r6 = r0;
        r0 = r1.getInterceptorScopedExchange();
        r5 = followUpRequest(r6, r0);
     */
    /* JADX WARN: Code restructure failed: missing block: B:14:0x0048, code lost:
    
        if (r5 != null) goto L18;
     */
    /* JADX WARN: Code restructure failed: missing block: B:15:0x0059, code lost:
    
        r0 = r5.body();
     */
    /* JADX WARN: Code restructure failed: missing block: B:16:0x005d, code lost:
    
        if (r0 == null) goto L24;
     */
    /* JADX WARN: Code restructure failed: missing block: B:18:0x0063, code lost:
    
        if (r0.isOneShot() == false) goto L24;
     */
    /* JADX WARN: Code restructure failed: missing block: B:20:0x0065, code lost:
    
        r1.exitNetworkInterceptorExchange$okhttp(false);
     */
    /* JADX WARN: Code restructure failed: missing block: B:21:0x0068, code lost:
    
        return r6;
     */
    /* JADX WARN: Code restructure failed: missing block: B:23:0x0069, code lost:
    
        r0 = r6.body();
     */
    /* JADX WARN: Code restructure failed: missing block: B:24:0x006d, code lost:
    
        if (r0 == null) goto L27;
     */
    /* JADX WARN: Code restructure failed: missing block: B:25:0x006f, code lost:
    
        okhttp3.internal.Util.closeQuietly(r0);
     */
    /* JADX WARN: Code restructure failed: missing block: B:26:0x0074, code lost:
    
        r7 = r7 + 1;
     */
    /* JADX WARN: Code restructure failed: missing block: B:27:0x0078, code lost:
    
        if (r7 > 20) goto L57;
     */
    /* JADX WARN: Code restructure failed: missing block: B:31:0x0099, code lost:
    
        throw new java.net.ProtocolException("Too many follow-up requests: " + r7);
     */
    /* JADX WARN: Code restructure failed: missing block: B:33:0x004a, code lost:
    
        if (r0 == null) goto L16;
     */
    /* JADX WARN: Code restructure failed: missing block: B:35:0x0050, code lost:
    
        if (r0.getIsDuplex() == false) goto L16;
     */
    /* JADX WARN: Code restructure failed: missing block: B:36:0x0052, code lost:
    
        r1.timeoutEarlyExit();
     */
    /* JADX WARN: Code restructure failed: missing block: B:37:0x0055, code lost:
    
        r1.exitNetworkInterceptorExchange$okhttp(false);
     */
    /* JADX WARN: Code restructure failed: missing block: B:38:0x0058, code lost:
    
        return r6;
     */
    @Override // okhttp3.Interceptor
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public okhttp3.Response intercept(okhttp3.Interceptor.Chain r10) throws java.io.IOException {
        /*
            r9 = this;
            java.lang.String r0 = "chain"
            kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull(r10, r0)
            okhttp3.internal.http.RealInterceptorChain r10 = (okhttp3.internal.http.RealInterceptorChain) r10
            okhttp3.Request r0 = r10.getRequest()
            okhttp3.internal.connection.RealCall r1 = r10.getCall()
            r2 = 0
            r3 = r2
            okhttp3.Response r3 = (okhttp3.Response) r3
            r3 = 0
            r4 = 1
            r6 = r2
            r7 = r3
        L17:
            r5 = r4
        L18:
            r1.enterNetworkInterceptorExchange(r0, r5)
            boolean r5 = r1.isCanceled()     // Catch: java.lang.Throwable -> Lca
            if (r5 != 0) goto Lc0
            okhttp3.Response r0 = r10.proceed(r0)     // Catch: java.io.IOException -> L9a okhttp3.internal.connection.RouteException -> La8 java.lang.Throwable -> Lca
            if (r6 == 0) goto L3f
            okhttp3.Response$Builder r0 = r0.newBuilder()     // Catch: java.lang.Throwable -> Lca
            okhttp3.Response$Builder r5 = r6.newBuilder()     // Catch: java.lang.Throwable -> Lca
            okhttp3.Response$Builder r5 = r5.body(r2)     // Catch: java.lang.Throwable -> Lca
            okhttp3.Response r5 = r5.build()     // Catch: java.lang.Throwable -> Lca
            okhttp3.Response$Builder r0 = r0.priorResponse(r5)     // Catch: java.lang.Throwable -> Lca
            okhttp3.Response r0 = r0.build()     // Catch: java.lang.Throwable -> Lca
        L3f:
            r6 = r0
            okhttp3.internal.connection.Exchange r0 = r1.getInterceptorScopedExchange()     // Catch: java.lang.Throwable -> Lca
            okhttp3.Request r5 = r9.followUpRequest(r6, r0)     // Catch: java.lang.Throwable -> Lca
            if (r5 != 0) goto L59
            if (r0 == 0) goto L55
            boolean r10 = r0.getIsDuplex()     // Catch: java.lang.Throwable -> Lca
            if (r10 == 0) goto L55
            r1.timeoutEarlyExit()     // Catch: java.lang.Throwable -> Lca
        L55:
            r1.exitNetworkInterceptorExchange$okhttp(r3)
            return r6
        L59:
            okhttp3.RequestBody r0 = r5.body()     // Catch: java.lang.Throwable -> Lca
            if (r0 == 0) goto L69
            boolean r0 = r0.isOneShot()     // Catch: java.lang.Throwable -> Lca
            if (r0 == 0) goto L69
            r1.exitNetworkInterceptorExchange$okhttp(r3)
            return r6
        L69:
            okhttp3.ResponseBody r0 = r6.body()     // Catch: java.lang.Throwable -> Lca
            if (r0 == 0) goto L74
            java.io.Closeable r0 = (java.io.Closeable) r0     // Catch: java.lang.Throwable -> Lca
            okhttp3.internal.Util.closeQuietly(r0)     // Catch: java.lang.Throwable -> Lca
        L74:
            int r7 = r7 + 1
            r0 = 20
            if (r7 > r0) goto L7f
            r1.exitNetworkInterceptorExchange$okhttp(r4)
            r0 = r5
            goto L17
        L7f:
            java.net.ProtocolException r10 = new java.net.ProtocolException     // Catch: java.lang.Throwable -> Lca
            java.lang.StringBuilder r0 = new java.lang.StringBuilder     // Catch: java.lang.Throwable -> Lca
            r0.<init>()     // Catch: java.lang.Throwable -> Lca
            java.lang.String r2 = "Too many follow-up requests: "
            java.lang.StringBuilder r0 = r0.append(r2)     // Catch: java.lang.Throwable -> Lca
            java.lang.StringBuilder r0 = r0.append(r7)     // Catch: java.lang.Throwable -> Lca
            java.lang.String r0 = r0.toString()     // Catch: java.lang.Throwable -> Lca
            r10.<init>(r0)     // Catch: java.lang.Throwable -> Lca
            java.lang.Throwable r10 = (java.lang.Throwable) r10     // Catch: java.lang.Throwable -> Lca
            throw r10     // Catch: java.lang.Throwable -> Lca
        L9a:
            r5 = move-exception
            boolean r8 = r5 instanceof okhttp3.internal.http2.ConnectionShutdownException     // Catch: java.lang.Throwable -> Lca
            r8 = r8 ^ r4
            boolean r8 = r9.recover(r5, r1, r0, r8)     // Catch: java.lang.Throwable -> Lca
            if (r8 == 0) goto La5
            goto Lb3
        La5:
            java.lang.Throwable r5 = (java.lang.Throwable) r5     // Catch: java.lang.Throwable -> Lca
            throw r5     // Catch: java.lang.Throwable -> Lca
        La8:
            r5 = move-exception
            java.io.IOException r8 = r5.getLastConnectException()     // Catch: java.lang.Throwable -> Lca
            boolean r8 = r9.recover(r8, r1, r0, r3)     // Catch: java.lang.Throwable -> Lca
            if (r8 == 0) goto Lb9
        Lb3:
            r1.exitNetworkInterceptorExchange$okhttp(r4)
            r5 = r3
            goto L18
        Lb9:
            java.io.IOException r10 = r5.getFirstConnectException()     // Catch: java.lang.Throwable -> Lca
            java.lang.Throwable r10 = (java.lang.Throwable) r10     // Catch: java.lang.Throwable -> Lca
            throw r10     // Catch: java.lang.Throwable -> Lca
        Lc0:
            java.io.IOException r10 = new java.io.IOException     // Catch: java.lang.Throwable -> Lca
            java.lang.String r0 = "Canceled"
            r10.<init>(r0)     // Catch: java.lang.Throwable -> Lca
            java.lang.Throwable r10 = (java.lang.Throwable) r10     // Catch: java.lang.Throwable -> Lca
            throw r10     // Catch: java.lang.Throwable -> Lca
        Lca:
            r10 = move-exception
            r1.exitNetworkInterceptorExchange$okhttp(r4)
            throw r10
        */
        throw new UnsupportedOperationException("Method not decompiled: okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(okhttp3.Interceptor$Chain):okhttp3.Response");
    }

    private final boolean recover(IOException e, RealCall call, Request userRequest, boolean requestSendStarted) {
        if (this.client.retryOnConnectionFailure()) {
            return !(requestSendStarted && requestIsOneShot(e, userRequest)) && isRecoverable(e, requestSendStarted) && call.retryAfterFailure();
        }
        return false;
    }

    private final boolean requestIsOneShot(IOException e, Request userRequest) {
        RequestBody body = userRequest.body();
        return (body != null && body.isOneShot()) || (e instanceof FileNotFoundException);
    }

    private final boolean isRecoverable(IOException e, boolean requestSendStarted) {
        if (e instanceof ProtocolException) {
            return false;
        }
        return e instanceof InterruptedIOException ? (e instanceof SocketTimeoutException) && !requestSendStarted : (((e instanceof SSLHandshakeException) && (e.getCause() instanceof CertificateException)) || (e instanceof SSLPeerUnverifiedException)) ? false : true;
    }

    private final Request followUpRequest(Response userResponse, Exchange exchange) throws IOException {
        RealConnection connection;
        Route route = (exchange == null || (connection = exchange.getConnection()) == null) ? null : connection.getRoute();
        int code = userResponse.code();
        String method = userResponse.request().method();
        if (code == 307 || code == 308) {
            if (Intrinsics.areEqual(method, HttpGet.METHOD_NAME) || Intrinsics.areEqual(method, HttpHead.METHOD_NAME)) {
                return buildRedirectRequest(userResponse, method);
            }
            return null;
        }
        if (code == 401) {
            return this.client.authenticator().authenticate(route, userResponse);
        }
        if (code == 421) {
            RequestBody body = userResponse.request().body();
            if ((body != null && body.isOneShot()) || exchange == null || !exchange.isCoalescedConnection$okhttp()) {
                return null;
            }
            exchange.getConnection().noCoalescedConnections();
            return userResponse.request();
        }
        if (code == 503) {
            Response priorResponse = userResponse.priorResponse();
            if ((priorResponse == null || priorResponse.code() != 503) && retryAfter(userResponse, Integer.MAX_VALUE) == 0) {
                return userResponse.request();
            }
            return null;
        }
        if (code == 407) {
            if (route == null) {
                Intrinsics.throwNpe();
            }
            if (route.proxy().type() != Proxy.Type.HTTP) {
                throw new ProtocolException("Received HTTP_PROXY_AUTH (407) code while not using proxy");
            }
            return this.client.proxyAuthenticator().authenticate(route, userResponse);
        }
        if (code != 408) {
            switch (code) {
                case 300:
                case HttpStatus.SC_MOVED_PERMANENTLY /* 301 */:
                case HttpStatus.SC_MOVED_TEMPORARILY /* 302 */:
                case HttpStatus.SC_SEE_OTHER /* 303 */:
                    return buildRedirectRequest(userResponse, method);
                default:
                    return null;
            }
        }
        if (!this.client.retryOnConnectionFailure()) {
            return null;
        }
        RequestBody body2 = userResponse.request().body();
        if (body2 != null && body2.isOneShot()) {
            return null;
        }
        Response priorResponse2 = userResponse.priorResponse();
        if ((priorResponse2 == null || priorResponse2.code() != 408) && retryAfter(userResponse, 0) <= 0) {
            return userResponse.request();
        }
        return null;
    }

    private final Request buildRedirectRequest(Response userResponse, String method) {
        String header$default;
        HttpUrl resolve;
        if (!this.client.followRedirects() || (header$default = Response.header$default(userResponse, org.apache.http.HttpHeaders.LOCATION, null, 2, null)) == null || (resolve = userResponse.request().url().resolve(header$default)) == null) {
            return null;
        }
        if (!Intrinsics.areEqual(resolve.scheme(), userResponse.request().url().scheme()) && !this.client.followSslRedirects()) {
            return null;
        }
        Request.Builder newBuilder = userResponse.request().newBuilder();
        if (HttpMethod.permitsRequestBody(method)) {
            boolean redirectsWithBody = HttpMethod.INSTANCE.redirectsWithBody(method);
            if (HttpMethod.INSTANCE.redirectsToGet(method)) {
                newBuilder.method(HttpGet.METHOD_NAME, null);
            } else {
                newBuilder.method(method, redirectsWithBody ? userResponse.request().body() : null);
            }
            if (!redirectsWithBody) {
                newBuilder.removeHeader("Transfer-Encoding");
                newBuilder.removeHeader("Content-Length");
                newBuilder.removeHeader("Content-Type");
            }
        }
        if (!Util.canReuseConnectionFor(userResponse.request().url(), resolve)) {
            newBuilder.removeHeader("Authorization");
        }
        return newBuilder.url(resolve).build();
    }

    private final int retryAfter(Response userResponse, int defaultDelay) {
        String header$default = Response.header$default(userResponse, org.apache.http.HttpHeaders.RETRY_AFTER, null, 2, null);
        if (header$default == null) {
            return defaultDelay;
        }
        if (!new Regex("\\d+").matches(header$default)) {
            return Integer.MAX_VALUE;
        }
        Integer valueOf = Integer.valueOf(header$default);
        Intrinsics.checkExpressionValueIsNotNull(valueOf, "Integer.valueOf(header)");
        return valueOf.intValue();
    }
}
