package okhttp3.internal.http;

import java.io.EOFException;
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 java.util.Iterator;
import java.util.List;
import java.util.regex.Pattern;
import javax.net.ssl.SSLHandshakeException;
import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.SSLSocketFactory;
import kotlin.jvm.internal.o;
import n2.AbstractC2613d;
import okhttp3.Address;
import okhttp3.CertificatePinner;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.Route;
import okhttp3.internal.Util;
import okhttp3.internal.connection.Exchange;
import okhttp3.internal.connection.ExchangeFinder;
import okhttp3.internal.connection.RealCall;
import okhttp3.internal.connection.RealConnection;
import okhttp3.internal.connection.RealConnectionPool;
import okhttp3.internal.connection.RouteException;
import okhttp3.internal.connection.RouteSelector;
import okhttp3.internal.http2.ConnectionShutdownException;
import okhttp3.internal.tls.OkHostnameVerifier;
import x4.AbstractC3251q;
import x4.y;

/* JADX INFO: loaded from: classes3.dex */
public final class RetryAndFollowUpInterceptor implements Interceptor {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final OkHttpClient f22397a;

    public static final class Companion {
        public /* synthetic */ Companion(int i6) {
            this();
        }

        private Companion() {
        }
    }

    static {
        new Companion(0);
    }

    public RetryAndFollowUpInterceptor(OkHttpClient client) {
        o.h(client, "client");
        this.f22397a = client;
    }

    public static int d(Response response, int i6) {
        String strB = Response.b("Retry-After", response);
        if (strB == null) {
            return i6;
        }
        Pattern patternCompile = Pattern.compile("\\d+");
        o.g(patternCompile, "compile(...)");
        if (!patternCompile.matcher(strB).matches()) {
            return Integer.MAX_VALUE;
        }
        Integer numValueOf = Integer.valueOf(strB);
        o.g(numValueOf, "valueOf(header)");
        return numValueOf.intValue();
    }

    /* JADX WARN: Code restructure failed: missing block: B:25:0x0086, code lost:
    
        if (r9 == null) goto L34;
     */
    /* JADX WARN: Code restructure failed: missing block: B:26:0x0088, code lost:
    
        r0 = r0.d();
        r4 = r9.d();
        r4.f22159g = null;
        r4 = r4.a();
     */
    /* JADX WARN: Code restructure failed: missing block: B:27:0x0099, code lost:
    
        if (r4.f22146j != null) goto L90;
     */
    /* JADX WARN: Code restructure failed: missing block: B:28:0x009b, code lost:
    
        r0.f22162j = r4;
        r0 = r0.a();
     */
    /* JADX WARN: Code restructure failed: missing block: B:29:0x00a1, code lost:
    
        r9 = r0;
     */
    /* JADX WARN: Code restructure failed: missing block: B:33:0x00ae, code lost:
    
        throw new java.lang.IllegalArgumentException("priorResponse.body != null");
     */
    /* JADX WARN: Code restructure failed: missing block: B:35:0x00b1, code lost:
    
        r4 = b(r9, r3.f22325m);
     */
    /* JADX WARN: Code restructure failed: missing block: B:36:0x00b7, code lost:
    
        if (r4 != null) goto L39;
     */
    /* JADX WARN: Code restructure failed: missing block: B:37:0x00b9, code lost:
    
        r3.e(false);
     */
    /* JADX WARN: Code restructure failed: missing block: B:38:0x00bd, code lost:
    
        return r9;
     */
    /* JADX WARN: Code restructure failed: missing block: B:39:0x00be, code lost:
    
        r0 = r9.f22146j;
     */
    /* JADX WARN: Code restructure failed: missing block: B:40:0x00c0, code lost:
    
        if (r0 == null) goto L42;
     */
    /* JADX WARN: Code restructure failed: missing block: B:41:0x00c2, code lost:
    
        okhttp3.internal.Util.c(r0);
     */
    /* JADX WARN: Code restructure failed: missing block: B:42:0x00c5, code lost:
    
        r10 = r10 + 1;
     */
    /* JADX WARN: Code restructure failed: missing block: B:43:0x00c9, code lost:
    
        if (r10 > 20) goto L92;
     */
    /* JADX WARN: Code restructure failed: missing block: B:46:0x00e7, code lost:
    
        throw new java.net.ProtocolException("Too many follow-up requests: " + r10);
     */
    @Override // okhttp3.Interceptor
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Response a(RealInterceptorChain realInterceptorChain) {
        SSLSocketFactory sSLSocketFactory;
        OkHostnameVerifier okHostnameVerifier;
        CertificatePinner certificatePinner;
        Request request = realInterceptorChain.f22388e;
        RealCall realCall = realInterceptorChain.f22384a;
        List listT0 = y.f26065a;
        Response response = null;
        int i6 = 0;
        Request request2 = request;
        while (true) {
            boolean z6 = true;
            while (true) {
                realCall.getClass();
                o.h(request2, "request");
                if (realCall.f22325m != null) {
                    throw new IllegalStateException("Check failed.");
                }
                synchronized (realCall) {
                    try {
                        if (realCall.f22327o) {
                            throw new IllegalStateException("cannot make a new request because the previous response is still open: please call response.close()");
                        }
                        if (realCall.f22326n) {
                            throw new IllegalStateException("Check failed.");
                        }
                    } catch (Throwable th) {
                        throw th;
                    }
                }
                if (z6) {
                    RealConnectionPool realConnectionPool = realCall.f22319c;
                    HttpUrl httpUrl = request2.f22125a;
                    boolean z7 = httpUrl.f22033j;
                    OkHttpClient okHttpClient = realCall.f22317a;
                    if (z7) {
                        SSLSocketFactory sSLSocketFactory2 = okHttpClient.f22086s;
                        if (sSLSocketFactory2 == null) {
                            throw new IllegalStateException("CLEARTEXT-only client");
                        }
                        OkHostnameVerifier okHostnameVerifier2 = okHttpClient.f22090w;
                        certificatePinner = okHttpClient.f22091x;
                        sSLSocketFactory = sSLSocketFactory2;
                        okHostnameVerifier = okHostnameVerifier2;
                    } else {
                        sSLSocketFactory = null;
                        okHostnameVerifier = null;
                        certificatePinner = null;
                    }
                    realCall.f22324k = new ExchangeFinder(realConnectionPool, new Address(httpUrl.f22027d, httpUrl.f22028e, okHttpClient.f22082o, okHttpClient.f22085r, sSLSocketFactory, okHostnameVerifier, certificatePinner, okHttpClient.f22084q, okHttpClient.f22089v, okHttpClient.f22088u, okHttpClient.f22083p), realCall, realCall.f22320d);
                }
                try {
                    if (realCall.f22329q) {
                        throw new IOException("Canceled");
                    }
                    try {
                        Response responseB = realInterceptorChain.b(request2);
                        break;
                    } catch (IOException e6) {
                        if (!c(e6, realCall, request2, !(e6 instanceof ConnectionShutdownException))) {
                            Iterator it = listT0.iterator();
                            while (it.hasNext()) {
                                AbstractC2613d.b(e6, (Exception) it.next());
                            }
                            throw e6;
                        }
                        listT0 = AbstractC3251q.t0(listT0, e6);
                        realCall.e(true);
                        z6 = false;
                    } catch (RouteException e7) {
                        if (!c(e7.f22365b, realCall, request2, false)) {
                            IOException iOException = e7.f22364a;
                            o.h(iOException, "<this>");
                            Iterator it2 = listT0.iterator();
                            while (it2.hasNext()) {
                                AbstractC2613d.b(iOException, (Exception) it2.next());
                            }
                            throw iOException;
                        }
                        listT0 = AbstractC3251q.t0(listT0, e7.f22364a);
                        realCall.e(true);
                        z6 = false;
                    }
                } catch (Throwable th2) {
                    realCall.e(true);
                    throw th2;
                }
            }
            realCall.e(true);
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:66:0x00b5  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Request b(Response response, Exchange exchange) throws ProtocolException, EOFException {
        String strB;
        HttpUrl.Builder builder;
        Response response2;
        RealConnection realConnection;
        Route route = (exchange == null || (realConnection = exchange.f22295f) == null) ? null : realConnection.f22337b;
        int i6 = response.f22143d;
        String str = response.f22140a.f22126b;
        if (i6 == 307 || i6 == 308) {
            OkHttpClient okHttpClient = this.f22397a;
            if (okHttpClient.f22079k && (strB = Response.b("Location", response)) != null) {
                Request request = response.f22140a;
                HttpUrl httpUrl = request.f22125a;
                httpUrl.getClass();
                try {
                    builder = new HttpUrl.Builder();
                    builder.b(httpUrl, strB);
                } catch (IllegalArgumentException unused) {
                    builder = null;
                }
                HttpUrl httpUrlA = builder != null ? builder.a() : null;
                if (httpUrlA != null) {
                    if (o.c(httpUrlA.f22024a, request.f22125a.f22024a) || okHttpClient.l) {
                        Request.Builder builderB = request.b();
                        if (HttpMethod.b(str)) {
                            HttpMethod.f22383a.getClass();
                            boolean zEquals = str.equals("PROPFIND");
                            int i7 = response.f22143d;
                            boolean z6 = zEquals || i7 == 308 || i7 == 307;
                            if (str.equals("PROPFIND") || i7 == 308 || i7 == 307) {
                                builderB.g(str, z6 ? request.f22128d : null);
                            } else {
                                builderB.g("GET", null);
                            }
                            if (!z6) {
                                builderB.f22133c.g("Transfer-Encoding");
                                builderB.f22133c.g("Content-Length");
                                builderB.f22133c.g("Content-Type");
                            }
                        }
                        if (!Util.a(request.f22125a, httpUrlA)) {
                            builderB.f22133c.g("Authorization");
                        }
                        builderB.f22131a = httpUrlA;
                        return builderB.b();
                    }
                }
            }
        } else {
            if (i6 == 401) {
                return this.f22397a.f22078j.a(route, response);
            }
            if (i6 != 421) {
                if (i6 == 503) {
                    Response response3 = response.f22148m;
                    if ((response3 == null || response3.f22143d != 503) && d(response, Integer.MAX_VALUE) == 0) {
                        return response.f22140a;
                    }
                } else {
                    if (i6 == 407) {
                        o.e(route);
                        if (route.f22169b.type() == Proxy.Type.HTTP) {
                            return this.f22397a.f22084q.a(route, response);
                        }
                        throw new ProtocolException("Received HTTP_PROXY_AUTH (407) code while not using proxy");
                    }
                    if (i6 != 408) {
                        switch (i6) {
                            case 300:
                            case 301:
                            case 302:
                            case 303:
                            default:
                                return null;
                        }
                    } else if (this.f22397a.f22077f && (((response2 = response.f22148m) == null || response2.f22143d != 408) && d(response, 0) <= 0)) {
                        return response.f22140a;
                    }
                }
            } else if (exchange != null && !o.c(exchange.f22292c.f22308b.f21896h.f22027d, exchange.f22295f.f22337b.f22168a.f21896h.f22027d)) {
                RealConnection realConnection2 = exchange.f22295f;
                synchronized (realConnection2) {
                    realConnection2.f22346k = true;
                }
                return response.f22140a;
            }
        }
        return null;
    }

    /* JADX WARN: Removed duplicated region for block: B:33:0x0047  */
    /* JADX WARN: Removed duplicated region for block: B:36:0x004c  */
    /* JADX WARN: Removed duplicated region for block: B:63:0x0083  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final boolean c(IOException iOException, RealCall realCall, Request request, boolean z6) {
        ExchangeFinder exchangeFinder;
        int i6;
        boolean zA;
        RouteSelector routeSelector;
        RealConnection realConnection;
        if (!this.f22397a.f22077f || ((z6 && (iOException instanceof FileNotFoundException)) || (iOException instanceof ProtocolException))) {
            return false;
        }
        if (!(iOException instanceof InterruptedIOException)) {
            if (((iOException instanceof SSLHandshakeException) && (iOException.getCause() instanceof CertificateException)) || (iOException instanceof SSLPeerUnverifiedException)) {
                return false;
            }
            exchangeFinder = realCall.f22324k;
            o.e(exchangeFinder);
            i6 = exchangeFinder.f22313g;
            if (i6 != 0) {
                if (exchangeFinder.f22316j == null) {
                }
            }
        } else if ((iOException instanceof SocketTimeoutException) && !z6) {
            exchangeFinder = realCall.f22324k;
            o.e(exchangeFinder);
            i6 = exchangeFinder.f22313g;
            if (i6 != 0 && exchangeFinder.f22314h == 0 && exchangeFinder.f22315i == 0) {
                zA = false;
            } else if (exchangeFinder.f22316j == null) {
                zA = true;
            } else {
                Route route = null;
                if (i6 <= 1 && exchangeFinder.f22314h <= 1 && exchangeFinder.f22315i <= 0 && (realConnection = exchangeFinder.f22309c.l) != null) {
                    synchronized (realConnection) {
                        if (realConnection.l == 0 && Util.a(realConnection.f22337b.f22168a.f21896h, exchangeFinder.f22308b.f21896h)) {
                            route = realConnection.f22337b;
                        }
                    }
                }
                if (route != null) {
                    exchangeFinder.f22316j = route;
                } else {
                    RouteSelector.Selection selection = exchangeFinder.f22311e;
                    if ((selection == null || !selection.a()) && (routeSelector = exchangeFinder.f22312f) != null) {
                        zA = routeSelector.a();
                    }
                }
                zA = true;
            }
            if (zA) {
                return true;
            }
        }
        return false;
    }
}
