package r7;

import a.AbstractC0386a;
import java.io.IOException;
import java.net.ProtocolException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
import k7.C;
import x7.C1734j;
import x7.F;
import x7.H;

/* JADX INFO: loaded from: classes.dex */
public final class p implements p7.b {

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public static final List f16656g = l7.b.l("connection", "host", "keep-alive", "proxy-connection", "te", "transfer-encoding", "encoding", "upgrade", ":method", ":path", ":scheme", ":authority");

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public static final List f16657h = l7.b.l("connection", "host", "keep-alive", "proxy-connection", "te", "transfer-encoding", "encoding", "upgrade");

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final o7.l f16658a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final p7.d f16659b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final o f16660c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public volatile w f16661d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final k7.x f16662e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public volatile boolean f16663f;

    public p(k7.w wVar, o7.l lVar, p7.d dVar, o oVar) {
        P6.g.e(wVar, "client");
        P6.g.e(lVar, "connection");
        P6.g.e(oVar, "http2Connection");
        this.f16658a = lVar;
        this.f16659b = dVar;
        this.f16660c = oVar;
        k7.x xVar = k7.x.f14198v;
        this.f16662e = wVar.H.contains(xVar) ? xVar : k7.x.f14197u;
    }

    @Override // p7.b
    public final H a(C c3) {
        w wVar = this.f16661d;
        P6.g.b(wVar);
        return wVar.i;
    }

    @Override // p7.b
    public final void b(k7.y yVar) throws IOException {
        int i;
        w wVar;
        P6.g.e(yVar, "request");
        if (this.f16661d != null) {
            return;
        }
        boolean z8 = true;
        boolean z9 = yVar.f14205d != null;
        k7.o oVar = yVar.f14204c;
        ArrayList arrayList = new ArrayList(oVar.size() + 4);
        arrayList.add(new C1512b(C1512b.f16585f, yVar.f14203b));
        C1734j c1734j = C1512b.f16586g;
        k7.q qVar = yVar.f14202a;
        P6.g.e(qVar, "url");
        String strB = qVar.b();
        String strD = qVar.d();
        if (strD != null) {
            strB = strB + '?' + strD;
        }
        arrayList.add(new C1512b(c1734j, strB));
        String strA = yVar.f14204c.a("Host");
        if (strA != null) {
            arrayList.add(new C1512b(C1512b.i, strA));
        }
        arrayList.add(new C1512b(C1512b.f16587h, qVar.f14121a));
        int size = oVar.size();
        for (int i5 = 0; i5 < size; i5++) {
            String strC = oVar.c(i5);
            Locale locale = Locale.US;
            P6.g.d(locale, "US");
            String lowerCase = strC.toLowerCase(locale);
            P6.g.d(lowerCase, "this as java.lang.String).toLowerCase(locale)");
            if (!f16656g.contains(lowerCase) || (lowerCase.equals("te") && P6.g.a(oVar.g(i5), "trailers"))) {
                arrayList.add(new C1512b(lowerCase, oVar.g(i5)));
            }
        }
        o oVar2 = this.f16660c;
        oVar2.getClass();
        boolean z10 = !z9;
        synchronized (oVar2.f16643M) {
            synchronized (oVar2) {
                try {
                    if (oVar2.f16650u > 1073741823) {
                        oVar2.m(8);
                    }
                    if (oVar2.f16651v) {
                        throw new C1511a();
                    }
                    i = oVar2.f16650u;
                    oVar2.f16650u = i + 2;
                    wVar = new w(i, oVar2, z10, false, null);
                    if (z9 && oVar2.f16640J < oVar2.f16641K && wVar.f16689e < wVar.f16690f) {
                        z8 = false;
                    }
                    if (wVar.i()) {
                        oVar2.f16647r.put(Integer.valueOf(i), wVar);
                    }
                } catch (Throwable th) {
                    throw th;
                }
            }
            oVar2.f16643M.m(z10, i, arrayList);
        }
        if (z8) {
            oVar2.f16643M.flush();
        }
        this.f16661d = wVar;
        if (this.f16663f) {
            w wVar2 = this.f16661d;
            P6.g.b(wVar2);
            wVar2.e(9);
            throw new IOException("Canceled");
        }
        w wVar3 = this.f16661d;
        P6.g.b(wVar3);
        v vVar = wVar3.f16694k;
        long j8 = this.f16659b.f16163g;
        TimeUnit timeUnit = TimeUnit.MILLISECONDS;
        vVar.g(j8);
        w wVar4 = this.f16661d;
        P6.g.b(wVar4);
        wVar4.f16695l.g(this.f16659b.f16164h);
    }

    @Override // p7.b
    public final F c(k7.y yVar, long j8) {
        P6.g.e(yVar, "request");
        w wVar = this.f16661d;
        P6.g.b(wVar);
        return wVar.g();
    }

    @Override // p7.b
    public final void cancel() {
        this.f16663f = true;
        w wVar = this.f16661d;
        if (wVar != null) {
            wVar.e(9);
        }
    }

    @Override // p7.b
    public final long d(C c3) {
        if (p7.c.a(c3)) {
            return l7.b.k(c3);
        }
        return 0L;
    }

    @Override // p7.b
    public final void e() {
        w wVar = this.f16661d;
        P6.g.b(wVar);
        wVar.g().close();
    }

    @Override // p7.b
    public final void f() {
        this.f16660c.flush();
    }

    @Override // p7.b
    public final k7.B g(boolean z8) throws IOException {
        k7.o oVar;
        w wVar = this.f16661d;
        if (wVar == null) {
            throw new IOException("stream wasn't created");
        }
        synchronized (wVar) {
            wVar.f16694k.h();
            while (wVar.f16691g.isEmpty() && wVar.f16696m == 0) {
                try {
                    wVar.l();
                } catch (Throwable th) {
                    wVar.f16694k.k();
                    throw th;
                }
            }
            wVar.f16694k.k();
            if (wVar.f16691g.isEmpty()) {
                IOException iOException = wVar.f16697n;
                if (iOException != null) {
                    throw iOException;
                }
                int i = wVar.f16696m;
                I1.a.z(i);
                throw new B(i);
            }
            Object objRemoveFirst = wVar.f16691g.removeFirst();
            P6.g.d(objRemoveFirst, "headersQueue.removeFirst()");
            oVar = (k7.o) objRemoveFirst;
        }
        k7.x xVar = this.f16662e;
        P6.g.e(xVar, "protocol");
        ArrayList arrayList = new ArrayList(20);
        int size = oVar.size();
        A1.H hX = null;
        for (int i5 = 0; i5 < size; i5++) {
            String strC = oVar.c(i5);
            String strG = oVar.g(i5);
            if (P6.g.a(strC, ":status")) {
                hX = AbstractC0386a.x("HTTP/1.1 " + strG);
            } else if (!f16657h.contains(strC)) {
                P6.g.e(strC, "name");
                P6.g.e(strG, "value");
                arrayList.add(strC);
                arrayList.add(X6.e.j0(strG).toString());
            }
        }
        if (hX == null) {
            throw new ProtocolException("Expected ':status' header not present");
        }
        k7.B b8 = new k7.B();
        b8.f13996b = xVar;
        b8.f13997c = hX.f65r;
        b8.f13998d = (String) hX.f67t;
        String[] strArr = (String[]) arrayList.toArray(new String[0]);
        N0.c cVar = new N0.c(1);
        ArrayList arrayList2 = cVar.f4375a;
        P6.g.e(arrayList2, "<this>");
        P6.g.e(strArr, "elements");
        arrayList2.addAll(B6.j.D(strArr));
        b8.f14000f = cVar;
        if (z8 && b8.f13997c == 100) {
            return null;
        }
        return b8;
    }

    @Override // p7.b
    public final o7.l h() {
        return this.f16658a;
    }
}
