package l5; import com.google.android.gms.common.api.Api; import h5.b0; import h5.d0; import h5.f0; import h5.h0; import h5.i0; import h5.m0; import h5.t; 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.HostnameVerifier; import javax.net.ssl.SSLHandshakeException; import javax.net.ssl.SSLPeerUnverifiedException; import javax.net.ssl.SSLSocketFactory; /* loaded from: classes2.dex */ public final class j implements d0 { /* renamed from: a, reason: collision with root package name */ private final f0 f11620a; /* renamed from: b, reason: collision with root package name */ private final boolean f11621b; /* renamed from: c, reason: collision with root package name */ private volatile k5.g f11622c; /* renamed from: d, reason: collision with root package name */ private Object f11623d; /* renamed from: e, reason: collision with root package name */ private volatile boolean f11624e; public j(f0 f0Var, boolean z10) { this.f11620a = f0Var; this.f11621b = z10; } private int b(i0 i0Var, int i10) { String O = i0Var.O("Retry-After"); return O == null ? i10 : O.matches("\\d+") ? Integer.valueOf(O).intValue() : Api.BaseClientBuilder.API_PRIORITY_OTHER; } private h5.a c(b0 b0Var) { SSLSocketFactory sSLSocketFactory; HostnameVerifier hostnameVerifier; h5.k kVar; if (b0Var.u()) { sSLSocketFactory = this.f11620a.C(); hostnameVerifier = this.f11620a.p(); kVar = this.f11620a.f(); } else { sSLSocketFactory = null; hostnameVerifier = null; kVar = null; } return new h5.a(b0Var.t(), b0Var.y(), this.f11620a.l(), this.f11620a.B(), sSLSocketFactory, hostnameVerifier, kVar, this.f11620a.w(), this.f11620a.v(), this.f11620a.u(), this.f11620a.i(), this.f11620a.x()); } private h0 d(i0 i0Var, m0 m0Var) { String O; b0 B; if (i0Var == null) { throw new IllegalStateException(); } int G = i0Var.G(); String f10 = i0Var.j0().f(); if (G == 307 || G == 308) { if (!f10.equals("GET") && !f10.equals("HEAD")) { return null; } } else { if (G == 401) { return this.f11620a.d().a(m0Var, i0Var); } if (G == 503) { if ((i0Var.d0() == null || i0Var.d0().G() != 503) && b(i0Var, Api.BaseClientBuilder.API_PRIORITY_OTHER) == 0) { return i0Var.j0(); } return null; } if (G == 407) { if ((m0Var != null ? m0Var.b() : this.f11620a.v()).type() == Proxy.Type.HTTP) { return this.f11620a.w().a(m0Var, i0Var); } throw new ProtocolException("Received HTTP_PROXY_AUTH (407) code while not using proxy"); } if (G == 408) { if (!this.f11620a.A()) { return null; } i0Var.j0().a(); if ((i0Var.d0() == null || i0Var.d0().G() != 408) && b(i0Var, 0) <= 0) { return i0Var.j0(); } return null; } switch (G) { case 300: case 301: case 302: case 303: break; default: return null; } } if (!this.f11620a.n() || (O = i0Var.O("Location")) == null || (B = i0Var.j0().h().B(O)) == null) { return null; } if (!B.C().equals(i0Var.j0().h().C()) && !this.f11620a.o()) { return null; } h0.a g10 = i0Var.j0().g(); if (f.a(f10)) { boolean c10 = f.c(f10); if (f.b(f10)) { g10.e("GET", null); } else { g10.e(f10, c10 ? i0Var.j0().a() : null); } if (!c10) { g10.g("Transfer-Encoding"); g10.g("Content-Length"); g10.g("Content-Type"); } } if (!e(i0Var, B)) { g10.g("Authorization"); } return g10.h(B).b(); } private boolean e(i0 i0Var, b0 b0Var) { b0 h10 = i0Var.j0().h(); return h10.t().equals(b0Var.t()) && h10.y() == b0Var.y() && h10.C().equals(b0Var.C()); } private boolean f(IOException iOException, k5.g gVar, boolean z10, h0 h0Var) { gVar.p(iOException); if (!this.f11620a.A()) { return false; } if (z10) { h0Var.a(); } if (g(iOException, z10)) { return gVar.j(); } return false; } private boolean g(IOException iOException, boolean z10) { if (iOException instanceof ProtocolException) { return false; } if (iOException instanceof InterruptedIOException) { return (iOException instanceof SocketTimeoutException) && !z10; } if ((iOException instanceof SSLHandshakeException) && (iOException.getCause() instanceof CertificateException)) { return false; } return !(iOException instanceof SSLPeerUnverifiedException); } @Override // h5.d0 public i0 a(d0.a aVar) { i0 j10; h0 d10 = aVar.d(); g gVar = (g) aVar; h5.i f10 = gVar.f(); t h10 = gVar.h(); k5.g gVar2 = new k5.g(this.f11620a.h(), c(d10.h()), f10, h10, this.f11623d); this.f11622c = gVar2; i0 i0Var = null; int i10 = 0; while (!this.f11624e) { try { try { j10 = gVar.j(d10, gVar2, null, null); if (i0Var != null) { j10 = j10.a0().l(i0Var.a0().d(null).e()).e(); } } catch (IOException e10) { if (!f(e10, gVar2, !(e10 instanceof n5.a), d10)) { throw e10; } } catch (k5.e e11) { if (!f(e11.c(), gVar2, false, d10)) { throw e11.c(); } } try { h0 d11 = d(j10, gVar2.o()); if (d11 == null) { if (!this.f11621b) { gVar2.m(); } return j10; } i5.c.j(j10.c()); int i11 = i10 + 1; if (i11 > 20) { gVar2.m(); throw new ProtocolException("Too many follow-up requests: " + i11); } d11.a(); if (!e(j10, d11.h())) { gVar2.m(); gVar2 = new k5.g(this.f11620a.h(), c(d11.h()), f10, h10, this.f11623d); this.f11622c = gVar2; } else if (gVar2.h() != null) { throw new IllegalStateException("Closing the body of " + j10 + " didn't close its backing stream. Bad interceptor?"); } i0Var = j10; d10 = d11; i10 = i11; } catch (IOException e12) { gVar2.m(); throw e12; } } catch (Throwable th) { gVar2.p(null); gVar2.m(); throw th; } } gVar2.m(); throw new IOException("Canceled"); } public boolean h() { return this.f11624e; } public void i(Object obj) { this.f11623d = obj; } }