package okhttp3.n0.http;

import com.ifpdos.sdk.httpd.binder.aidl.RequestLine;
import io.netty.handler.codec.http.HttpHeaders;
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.List;
import java.util.Objects;
import java.util.regex.Pattern;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLHandshakeException;
import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.SSLSocketFactory;
import kotlin.Metadata;
import kotlin.collections.EmptyList;
import kotlin.collections.j;
import kotlin.jvm.internal.o;
import okhttp3.Address;
import okhttp3.CertificatePinner;
import okhttp3.Handshake;
import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Protocol;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okhttp3.Route;
import okhttp3.n0.c;
import okhttp3.n0.connection.Exchange;
import okhttp3.n0.connection.ExchangeFinder;
import okhttp3.n0.connection.RealCall;
import okhttp3.n0.connection.RealConnection;
import okhttp3.n0.connection.RealConnectionPool;
import okhttp3.n0.connection.RouteException;
import okhttp3.n0.connection.RouteSelector;
import okhttp3.n0.http2.ConnectionShutdownException;

/* JADX INFO: renamed from: y.n0.h.i, reason: from Kotlin metadata */
/* JADX INFO: loaded from: classes2.dex */
@Metadata(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;", "method", "", "followUpRequest", "exchange", "Lokhttp3/internal/connection/Exchange;", "intercept", "chain", "Lokhttp3/Interceptor$Chain;", "isRecoverable", "", "e", "Ljava/io/IOException;", "requestSendStarted", "recover", "call", "Lokhttp3/internal/connection/RealCall;", "userRequest", "requestIsOneShot", "retryAfter", "", "defaultDelay", "Companion", "okhttp"}, k = 1, mv = {1, 6, 0}, xi = 48)
public final class RetryAndFollowUpInterceptor implements Interceptor {
    public final OkHttpClient a;

    public RetryAndFollowUpInterceptor(OkHttpClient okHttpClient) {
        o.f(okHttpClient, "client");
        this.a = okHttpClient;
    }

    public final Request a(Response response, Exchange exchange) throws ProtocolException {
        String strA;
        HttpUrl.a aVar;
        RealConnection realConnection;
        RequestBody requestBody = null;
        Route route = (exchange == null || (realConnection = exchange.f9748g) == null) ? null : realConnection.f9788b;
        int i2 = response.f9644o;
        Request request = response.f9641f;
        String str = request.f9621b;
        if (i2 != 307 && i2 != 308) {
            if (i2 == 401) {
                return this.a.f9569u.a(route, response);
            }
            if (i2 == 421) {
                RequestBody requestBody2 = request.f9623d;
                if ((requestBody2 != null && requestBody2.isOneShot()) || exchange == null || !(!o.a(exchange.f9744c.f9760b.f9541i.f10091f, exchange.f9748g.f9788b.a.f9541i.f10091f))) {
                    return null;
                }
                RealConnection realConnection2 = exchange.f9748g;
                synchronized (realConnection2) {
                    realConnection2.f9797k = true;
                }
                return response.f9641f;
            }
            if (i2 == 503) {
                Response response2 = response.f9650u;
                if ((response2 == null || response2.f9644o != 503) && c(response, Integer.MAX_VALUE) == 0) {
                    return response.f9641f;
                }
                return null;
            }
            if (i2 == 407) {
                o.c(route);
                if (route.f9690b.type() == Proxy.Type.HTTP) {
                    return this.a.A.a(route, response);
                }
                throw new ProtocolException("Received HTTP_PROXY_AUTH (407) code while not using proxy");
            }
            if (i2 == 408) {
                if (!this.a.f9568t) {
                    return null;
                }
                RequestBody requestBody3 = request.f9623d;
                if (requestBody3 != null && requestBody3.isOneShot()) {
                    return null;
                }
                Response response3 = response.f9650u;
                if ((response3 == null || response3.f9644o != 408) && c(response, 0) <= 0) {
                    return response.f9641f;
                }
                return null;
            }
            switch (i2) {
                case 300:
                case 301:
                case 302:
                case 303:
                    break;
                default:
                    return null;
            }
        }
        if (!this.a.f9570v || (strA = Response.a(response, HttpHeaders.Names.LOCATION, null, 2)) == null) {
            return null;
        }
        HttpUrl httpUrl = response.f9641f.a;
        Objects.requireNonNull(httpUrl);
        o.f(strA, "link");
        o.f(strA, "link");
        try {
            aVar = new HttpUrl.a();
            aVar.d(httpUrl, strA);
        } catch (IllegalArgumentException unused) {
            aVar = null;
        }
        HttpUrl httpUrlA = aVar == null ? null : aVar.a();
        if (httpUrlA == null) {
            return null;
        }
        if (!o.a(httpUrlA.f10088c, response.f9641f.a.f10088c) && !this.a.f9571w) {
            return null;
        }
        Request request2 = response.f9641f;
        Objects.requireNonNull(request2);
        Request.a aVar2 = new Request.a(request2);
        if (HttpMethod.a(str)) {
            int i3 = response.f9644o;
            o.f(str, "method");
            boolean z2 = o.a(str, "PROPFIND") || i3 == 308 || i3 == 307;
            o.f(str, "method");
            if ((!o.a(str, "PROPFIND")) && i3 != 308 && i3 != 307) {
                str = RequestLine.METHOD_GET;
            } else if (z2) {
                requestBody = response.f9641f.f9623d;
            }
            aVar2.c(str, requestBody);
            if (!z2) {
                aVar2.e(HttpHeaders.Names.TRANSFER_ENCODING);
                aVar2.e("Content-Length");
                aVar2.e("Content-Type");
            }
        }
        if (!c.b(response.f9641f.a, httpUrlA)) {
            aVar2.e("Authorization");
        }
        aVar2.g(httpUrlA);
        return aVar2.a();
    }

    public final boolean b(IOException iOException, RealCall realCall, Request request, boolean z2) {
        boolean zA;
        RouteSelector routeSelector;
        RealConnection realConnection;
        if (!this.a.f9568t) {
            return false;
        }
        if (z2) {
            RequestBody requestBody = request.f9623d;
            if ((requestBody != null && requestBody.isOneShot()) || (iOException instanceof FileNotFoundException)) {
                return false;
            }
        }
        if (!(!(iOException instanceof ProtocolException) && (!(iOException instanceof InterruptedIOException) ? ((iOException instanceof SSLHandshakeException) && (iOException.getCause() instanceof CertificateException)) || (iOException instanceof SSLPeerUnverifiedException) : !((iOException instanceof SocketTimeoutException) && !z2)))) {
            return false;
        }
        ExchangeFinder exchangeFinder = realCall.f9777t;
        o.c(exchangeFinder);
        int i2 = exchangeFinder.f9765g;
        if (i2 == 0 && exchangeFinder.f9766h == 0 && exchangeFinder.f9767i == 0) {
            zA = false;
        } else if (exchangeFinder.f9768j != null) {
            zA = true;
        } else {
            Route route = null;
            if (i2 <= 1 && exchangeFinder.f9766h <= 1 && exchangeFinder.f9767i <= 0 && (realConnection = exchangeFinder.f9761c.f9778u) != null) {
                synchronized (realConnection) {
                    if (realConnection.f9798l == 0 && c.b(realConnection.f9788b.a.f9541i, exchangeFinder.f9760b.f9541i)) {
                        route = realConnection.f9788b;
                    }
                }
            }
            if (route != null) {
                exchangeFinder.f9768j = route;
            } else {
                RouteSelector.a aVar = exchangeFinder.f9763e;
                if (!(aVar != null && aVar.a()) && (routeSelector = exchangeFinder.f9764f) != null) {
                    zA = routeSelector.a();
                }
            }
            zA = true;
        }
        return zA;
    }

    public final int c(Response response, int i2) {
        String strA = Response.a(response, "Retry-After", null, 2);
        if (strA == null) {
            return i2;
        }
        o.f("\\d+", "pattern");
        Pattern patternCompile = Pattern.compile("\\d+");
        o.e(patternCompile, "compile(pattern)");
        o.f(patternCompile, "nativePattern");
        o.f(strA, "input");
        if (!patternCompile.matcher(strA).matches()) {
            return Integer.MAX_VALUE;
        }
        Integer numValueOf = Integer.valueOf(strA);
        o.e(numValueOf, "valueOf(header)");
        return numValueOf.intValue();
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Type inference failed for: r16v0 */
    /* JADX WARN: Type inference failed for: r16v1 */
    /* JADX WARN: Type inference failed for: r16v2 */
    /* JADX WARN: Type inference failed for: r16v3 */
    /* JADX WARN: Type inference failed for: r16v4 */
    /* JADX WARN: Type inference failed for: r16v5 */
    /* JADX WARN: Type inference failed for: r1v0 */
    /* JADX WARN: Type inference failed for: r1v1 */
    /* JADX WARN: Type inference failed for: r1v10 */
    /* JADX WARN: Type inference failed for: r1v11, types: [y.n0.g.e] */
    /* JADX WARN: Type inference failed for: r1v12 */
    /* JADX WARN: Type inference failed for: r1v13 */
    /* JADX WARN: Type inference failed for: r1v14 */
    /* JADX WARN: Type inference failed for: r1v15, types: [y.n0.g.e] */
    /* JADX WARN: Type inference failed for: r1v16 */
    /* JADX WARN: Type inference failed for: r1v17 */
    /* JADX WARN: Type inference failed for: r1v3 */
    /* JADX WARN: Type inference failed for: r1v38, types: [y.n0.g.e] */
    /* JADX WARN: Type inference failed for: r1v39 */
    /* JADX WARN: Type inference failed for: r1v4 */
    /* JADX WARN: Type inference failed for: r1v41 */
    /* JADX WARN: Type inference failed for: r1v42 */
    /* JADX WARN: Type inference failed for: r1v43 */
    /* JADX WARN: Type inference failed for: r1v44 */
    /* JADX WARN: Type inference failed for: r1v45 */
    /* JADX WARN: Type inference failed for: r1v5 */
    /* JADX WARN: Type inference failed for: r1v6 */
    /* JADX WARN: Type inference failed for: r1v7 */
    /* JADX WARN: Type inference failed for: r1v8 */
    /* JADX WARN: Type inference failed for: r1v9, types: [y.n0.g.e] */
    /* JADX WARN: Type inference failed for: r2v10, types: [y.n0.h.i] */
    /* JADX WARN: Type inference failed for: r2v11 */
    /* JADX WARN: Type inference failed for: r2v13, types: [y.n0.h.i] */
    /* JADX WARN: Type inference failed for: r2v21 */
    /* JADX WARN: Type inference failed for: r2v22 */
    /* JADX WARN: Type inference failed for: r2v4 */
    /* JADX WARN: Type inference failed for: r2v5 */
    /* JADX WARN: Type inference failed for: r2v6 */
    /* JADX WARN: Type inference failed for: r2v7 */
    /* JADX WARN: Type inference failed for: r3v1, types: [java.lang.Object, y.n0.g.e] */
    /* JADX WARN: Type inference failed for: r3v11 */
    /* JADX WARN: Type inference failed for: r3v21 */
    /* JADX WARN: Type inference failed for: r3v22 */
    /* JADX WARN: Type inference failed for: r3v23 */
    /* JADX WARN: Type inference failed for: r3v8 */
    @Override // okhttp3.Interceptor
    public Response intercept(Interceptor.a aVar) throws Throwable {
        List list;
        int i2;
        ?? r1;
        RealInterceptorChain realInterceptorChain;
        boolean z2;
        ?? r2;
        ?? r12;
        ?? r3;
        ?? r13;
        ?? r16;
        RealInterceptorChain realInterceptorChain2;
        ?? r162;
        SSLSocketFactory sSLSocketFactory;
        HostnameVerifier hostnameVerifier;
        CertificatePinner certificatePinner;
        ?? r14 = this;
        o.f(aVar, "chain");
        RealInterceptorChain realInterceptorChain3 = (RealInterceptorChain) aVar;
        Request request = realInterceptorChain3.f9829e;
        RealCall realCall = realInterceptorChain3.a;
        boolean z3 = true;
        List list2 = EmptyList.f9307f;
        int i3 = 0;
        Response response = null;
        Request requestA = request;
        boolean z4 = true;
        ?? r32 = realCall;
        while (true) {
            Objects.requireNonNull(r32);
            o.f(requestA, "request");
            if (!(r32.f9780w == null ? z3 : false)) {
                throw new IllegalStateException("Check failed.".toString());
            }
            synchronized (r32) {
                try {
                    try {
                        if (!(r32.f9782y ^ z3)) {
                            throw new IllegalStateException("cannot make a new request because the previous response is still open: please call response.close()".toString());
                        }
                        if (!(r32.f9781x ^ z3)) {
                            throw new IllegalStateException("Check failed.".toString());
                        }
                    } catch (Throwable th) {
                        th = th;
                        throw th;
                    }
                } catch (Throwable th2) {
                    th = th2;
                    r14 = r32;
                }
            }
            if (z4) {
                RealConnectionPool realConnectionPool = r32.f9772o;
                HttpUrl httpUrl = requestA.a;
                if (httpUrl.f10097l) {
                    OkHttpClient okHttpClient = r32.f9769f;
                    SSLSocketFactory sSLSocketFactory2 = okHttpClient.C;
                    if (sSLSocketFactory2 == null) {
                        throw new IllegalStateException("CLEARTEXT-only client");
                    }
                    HostnameVerifier hostnameVerifier2 = okHttpClient.G;
                    certificatePinner = okHttpClient.H;
                    sSLSocketFactory = sSLSocketFactory2;
                    hostnameVerifier = hostnameVerifier2;
                } else {
                    sSLSocketFactory = null;
                    hostnameVerifier = null;
                    certificatePinner = null;
                }
                String str = httpUrl.f10091f;
                int i4 = httpUrl.f10092g;
                OkHttpClient okHttpClient2 = r32.f9769f;
                list = list2;
                i2 = i3;
                Address address = address;
                Address address2 = new Address(str, i4, okHttpClient2.f9573y, okHttpClient2.B, sSLSocketFactory, hostnameVerifier, certificatePinner, okHttpClient2.A, null, okHttpClient2.F, okHttpClient2.E, okHttpClient2.f9574z);
                r32.f9777t = new ExchangeFinder(realConnectionPool, address, r32, r32.f9773p);
                r1 = address;
            } else {
                list = list2;
                i2 = i3;
                r1 = r14;
            }
            try {
                if (r32.A) {
                    throw new IOException("Canceled");
                }
                try {
                    try {
                        Response responseA = realInterceptorChain3.a(requestA);
                        if (response != null) {
                            try {
                                o.f(responseA, "response");
                                Request request2 = responseA.f9641f;
                                Protocol protocol = responseA.f9642m;
                                int i5 = responseA.f9644o;
                                String str2 = responseA.f9643n;
                                Handshake handshake = responseA.f9645p;
                                Headers.a aVarD = responseA.f9646q.d();
                                ResponseBody responseBody = responseA.f9647r;
                                Response response2 = responseA.f9648s;
                                Response response3 = responseA.f9649t;
                                long j2 = responseA.f9651v;
                                RealInterceptorChain realInterceptorChain4 = realInterceptorChain3;
                                r16 = r32;
                                try {
                                    long j3 = responseA.f9652w;
                                    Exchange exchange = responseA.f9653x;
                                    realInterceptorChain2 = realInterceptorChain4;
                                    o.f(response, "response");
                                    Request request3 = response.f9641f;
                                    Protocol protocol2 = response.f9642m;
                                    int i6 = response.f9644o;
                                    String str3 = response.f9643n;
                                    Handshake handshake2 = response.f9645p;
                                    Headers.a aVarD2 = response.f9646q.d();
                                    Response response4 = response.f9648s;
                                    Response response5 = response.f9649t;
                                    Response response6 = response.f9650u;
                                    long j4 = response.f9651v;
                                    long j5 = response.f9652w;
                                    Exchange exchange2 = response.f9653x;
                                    if (!(i6 >= 0)) {
                                        throw new IllegalStateException(o.l("code < 0: ", Integer.valueOf(i6)).toString());
                                    }
                                    if (request3 == null) {
                                        throw new IllegalStateException("request == null".toString());
                                    }
                                    if (protocol2 == null) {
                                        throw new IllegalStateException("protocol == null".toString());
                                    }
                                    if (str3 == null) {
                                        throw new IllegalStateException("message == null".toString());
                                    }
                                    Response response7 = new Response(request3, protocol2, str3, i6, handshake2, aVarD2.b(), null, response4, response5, response6, j4, j5, exchange2);
                                    if (!(response7.f9647r == null)) {
                                        throw new IllegalArgumentException("priorResponse.body != null".toString());
                                    }
                                    if (!(i5 >= 0)) {
                                        throw new IllegalStateException(o.l("code < 0: ", Integer.valueOf(i5)).toString());
                                    }
                                    if (request2 == null) {
                                        throw new IllegalStateException("request == null".toString());
                                    }
                                    if (protocol == null) {
                                        throw new IllegalStateException("protocol == null".toString());
                                    }
                                    if (str2 == null) {
                                        throw new IllegalStateException("message == null".toString());
                                    }
                                    responseA = new Response(request2, protocol, str2, i5, handshake, aVarD.b(), responseBody, response2, response3, response7, j2, j3, exchange);
                                    r162 = r16;
                                } catch (Throwable th3) {
                                    th = th3;
                                    r1 = r16;
                                    r1.k(true);
                                    throw th;
                                }
                            } catch (Throwable th4) {
                                th = th4;
                                r16 = r32;
                            }
                        } else {
                            realInterceptorChain2 = realInterceptorChain3;
                            r162 = r32;
                        }
                        response = responseA;
                        r1 = r162;
                    } catch (RouteException e2) {
                        realInterceptorChain = realInterceptorChain3;
                        ?? r15 = r32;
                        List list3 = list;
                        ?? r22 = this;
                        z2 = false;
                        if (!r22.b(e2.f9814m, r15, requestA, false)) {
                            IOException iOException = e2.f9813f;
                            c.C(iOException, list3);
                            throw iOException;
                        }
                        List listQ = j.Q(list3, e2.f9813f);
                        r15.k(true);
                        list2 = listQ;
                        r12 = r15;
                        r2 = r22;
                        r3 = r12;
                        r13 = r2;
                        z4 = z2;
                        realInterceptorChain3 = realInterceptorChain;
                        i3 = i2;
                        z3 = true;
                        r14 = r13;
                        r32 = r3;
                    }
                } catch (IOException e3) {
                    realInterceptorChain = realInterceptorChain3;
                    ?? r17 = r32;
                    ?? r23 = this;
                    List list4 = list;
                    if (!r23.b(e3, r17, requestA, !(e3 instanceof ConnectionShutdownException))) {
                        c.C(e3, list4);
                        throw e3;
                    }
                    List listQ2 = j.Q(list4, e3);
                    r17.k(true);
                    list2 = listQ2;
                    z2 = false;
                    r12 = r17;
                    r2 = r23;
                    r3 = r12;
                    r13 = r2;
                    z4 = z2;
                    realInterceptorChain3 = realInterceptorChain;
                    i3 = i2;
                    z3 = true;
                    r14 = r13;
                    r32 = r3;
                }
                try {
                    Exchange exchange3 = r1.f9780w;
                    try {
                        requestA = a(response, exchange3);
                        if (requestA == null) {
                            if (exchange3 != null && exchange3.f9746e) {
                                if (!(!r1.f9779v)) {
                                    throw new IllegalStateException("Check failed.".toString());
                                }
                                r1.f9779v = true;
                                r1.f9774q.i();
                            }
                            r1.k(false);
                            return response;
                        }
                        RequestBody requestBody = requestA.f9623d;
                        if (requestBody != null && requestBody.isOneShot()) {
                            r1.k(false);
                            return response;
                        }
                        ResponseBody responseBody2 = response.f9647r;
                        if (responseBody2 != null) {
                            c.e(responseBody2);
                        }
                        i3 = i2 + 1;
                        if (i3 > 20) {
                            throw new ProtocolException(o.l("Too many follow-up requests: ", Integer.valueOf(i3)));
                        }
                        r1.k(true);
                        r3 = r1;
                        r13 = this;
                        realInterceptorChain3 = realInterceptorChain2;
                        list2 = list;
                        z4 = true;
                        z3 = true;
                        r14 = r13;
                        r32 = r3;
                    } catch (Throwable th5) {
                        th = th5;
                        r1.k(true);
                        throw th;
                    }
                } catch (Throwable th6) {
                    th = th6;
                    r1.k(true);
                    throw th;
                }
            } catch (Throwable th7) {
                th = th7;
                r1 = r32;
            }
        }
    }
}
