package o;

import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.ProtocolException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.TimeUnit;

/* compiled from: r8-map-id-3b7cc9102578c509342fa2f0fabed2bfc72239cd258d579fc679bfcbc198b048 */
/* loaded from: classes.dex */
public final class LF implements InterfaceC0593Ww {
    public static final List g = AbstractC2264qi0.k("connection", "host", "keep-alive", "proxy-connection", "te", "transfer-encoding", "encoding", "upgrade", ":method", ":path", ":scheme", ":authority");
    public static final List h = AbstractC2264qi0.k("connection", "host", "keep-alive", "proxy-connection", "te", "transfer-encoding", "encoding", "upgrade");
    public final C2486t00 a;
    public final A00 b;
    public final KF c;
    public volatile RF d;
    public final EnumC2910xZ e;
    public volatile boolean f;

    public LF(C2429sT c2429sT, C2486t00 c2486t00, A00 a00, KF kf) {
        this.a = c2486t00;
        this.b = a00;
        this.c = kf;
        List list = c2429sT.C;
        EnumC2910xZ enumC2910xZ = EnumC2910xZ.q;
        this.e = list.contains(enumC2910xZ) ? enumC2910xZ : EnumC2910xZ.p;
    }

    @Override // o.InterfaceC0593Ww
    public final R80 a(U7 u7, long j) {
        return this.d.f();
    }

    @Override // o.InterfaceC0593Ww
    public final void b() {
        this.d.f().close();
    }

    @Override // o.InterfaceC0593Ww
    public final void c() {
        this.c.flush();
    }

    @Override // o.InterfaceC0593Ww
    public final void cancel() {
        this.f = true;
        RF rf = this.d;
        if (rf != null) {
            rf.e(9);
        }
    }

    @Override // o.InterfaceC0593Ww
    public final void d(U7 u7) throws IOException {
        int i;
        RF rf;
        boolean z;
        if (this.d != null) {
            return;
        }
        boolean z2 = ((AbstractC0752ap) u7.p) != null;
        LE le = (LE) u7.f352o;
        ArrayList arrayList = new ArrayList(le.size() + 4);
        arrayList.add(new JE(JE.f, (String) u7.n));
        C0135Ff c0135Ff = JE.g;
        ZF zf = (ZF) u7.m;
        String strB = zf.b();
        String strD = zf.d();
        if (strD != null) {
            strB = strB + '?' + strD;
        }
        arrayList.add(new JE(c0135Ff, strB));
        String strA = le.a("Host");
        if (strA != null) {
            arrayList.add(new JE(JE.i, strA));
        }
        arrayList.add(new JE(JE.h, zf.a));
        int size = le.size();
        for (int i2 = 0; i2 < size; i2++) {
            String lowerCase = le.b(i2).toLowerCase(Locale.US);
            if (!g.contains(lowerCase) || (lowerCase.equals("te") && F3.c(le.d(i2), "trailers"))) {
                arrayList.add(new JE(lowerCase, le.d(i2)));
            }
        }
        KF kf = this.c;
        boolean z3 = !z2;
        synchronized (kf.H) {
            synchronized (kf) {
                try {
                    if (kf.p > 1073741823) {
                        kf.f(8);
                    }
                    if (kf.q) {
                        throw new C1699kl();
                    }
                    i = kf.p;
                    kf.p = i + 2;
                    rf = new RF(i, kf, z3, false, null);
                    z = !z2 || kf.E >= kf.F || rf.e >= rf.f;
                    if (rf.h()) {
                        kf.m.put(Integer.valueOf(i), rf);
                    }
                } catch (Throwable th) {
                    throw th;
                }
            }
            kf.H.g(z3, i, arrayList);
        }
        if (z) {
            kf.H.flush();
        }
        this.d = rf;
        if (this.f) {
            this.d.e(9);
            throw new IOException("Canceled");
        }
        QF qf = this.d.k;
        long j = this.b.g;
        TimeUnit timeUnit = TimeUnit.MILLISECONDS;
        qf.g(j);
        this.d.l.g(this.b.h);
    }

    @Override // o.InterfaceC0593Ww
    public final T90 e(C20 c20) {
        return this.d.i;
    }

    @Override // o.InterfaceC0593Ww
    public final long f(C20 c20) {
        if (TF.a(c20)) {
            return AbstractC2264qi0.j(c20);
        }
        return 0L;
    }

    @Override // o.InterfaceC0593Ww
    public final B20 g(boolean z) throws NumberFormatException, IOException {
        LE le;
        RF rf = this.d;
        if (rf == null) {
            throw new IOException("stream wasn't created");
        }
        synchronized (rf) {
            rf.k.h();
            while (rf.g.isEmpty() && rf.m == 0) {
                try {
                    try {
                        rf.wait();
                    } catch (InterruptedException unused) {
                        Thread.currentThread().interrupt();
                        throw new InterruptedIOException();
                    }
                } catch (Throwable th) {
                    rf.k.k();
                    throw th;
                }
            }
            rf.k.k();
            if (rf.g.isEmpty()) {
                IOException iOException = rf.n;
                if (iOException != null) {
                    throw iOException;
                }
                throw new Ta0(rf.m);
            }
            le = (LE) rf.g.removeFirst();
        }
        EnumC2910xZ enumC2910xZ = this.e;
        ArrayList arrayList = new ArrayList(20);
        int size = le.size();
        C0692a8 c0692a8K = null;
        for (int i = 0; i < size; i++) {
            String strB = le.b(i);
            String strD = le.d(i);
            if (F3.c(strB, ":status")) {
                c0692a8K = AbstractC1692kh0.K("HTTP/1.1 " + strD);
            } else if (!h.contains(strB)) {
                arrayList.add(strB);
                arrayList.add(Za0.o0(strD).toString());
            }
        }
        if (c0692a8K == null) {
            throw new ProtocolException("Expected ':status' header not present");
        }
        B20 b20 = new B20();
        b20.b = enumC2910xZ;
        b20.c = c0692a8K.b;
        b20.d = (String) c0692a8K.d;
        String[] strArr = (String[]) arrayList.toArray(new String[0]);
        KE ke = new KE(0);
        ke.a.addAll(Arrays.asList(strArr));
        b20.f = ke;
        if (z && b20.c == 100) {
            return null;
        }
        return b20;
    }

    @Override // o.InterfaceC0593Ww
    public final C2486t00 h() {
        return this.a;
    }
}
