package g.i0.i;

import g.d0;
import g.f0;
import g.i0.i.p;
import g.r;
import g.t;
import g.w;
import g.x;
import g.z;
import java.io.IOException;
import java.net.ProtocolException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

/* JADX INFO: loaded from: classes.dex */
public final class f implements g.i0.g.c {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public static final List<String> f6272a = g.i0.c.o("connection", "host", "keep-alive", "proxy-connection", "te", "transfer-encoding", "encoding", "upgrade", ":method", ":path", ":scheme", ":authority");

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static final List<String> f6273b = g.i0.c.o("connection", "host", "keep-alive", "proxy-connection", "te", "transfer-encoding", "encoding", "upgrade");

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final t.a f6274c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final g.i0.f.g f6275d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final g f6276e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public p f6277f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final x f6278g;

    public class a extends h.k {

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        public boolean f6279c;

        /* JADX INFO: renamed from: d, reason: collision with root package name */
        public long f6280d;

        public a(h.x xVar) {
            super(xVar);
            this.f6279c = false;
            this.f6280d = 0L;
        }

        public final void a(IOException iOException) {
            if (this.f6279c) {
                return;
            }
            this.f6279c = true;
            f fVar = f.this;
            fVar.f6275d.i(false, fVar, this.f6280d, iOException);
        }

        @Override // h.k, h.x, java.io.Closeable, java.lang.AutoCloseable
        public void close() {
            this.f6537b.close();
            a(null);
        }

        @Override // h.x
        public long v(h.f fVar, long j2) throws IOException {
            try {
                long jV = this.f6537b.v(fVar, j2);
                if (jV > 0) {
                    this.f6280d += jV;
                }
                return jV;
            } catch (IOException e2) {
                a(e2);
                throw e2;
            }
        }
    }

    public f(w wVar, t.a aVar, g.i0.f.g gVar, g gVar2) {
        this.f6274c = aVar;
        this.f6275d = gVar;
        this.f6276e = gVar2;
        List<x> list = wVar.f6479e;
        x xVar = x.H2_PRIOR_KNOWLEDGE;
        this.f6278g = list.contains(xVar) ? xVar : x.HTTP_2;
    }

    @Override // g.i0.g.c
    public void a() {
        p pVar = this.f6277f;
        if (pVar != null) {
            pVar.e(b.CANCEL);
        }
    }

    @Override // g.i0.g.c
    public void b() throws IOException {
        ((p.a) this.f6277f.f()).close();
    }

    @Override // g.i0.g.c
    public void c(z zVar) {
        int i2;
        p pVar;
        boolean z;
        if (this.f6277f != null) {
            return;
        }
        boolean z2 = zVar.f6507d != null;
        g.r rVar = zVar.f6506c;
        ArrayList arrayList = new ArrayList(rVar.g() + 4);
        arrayList.add(new c(c.f6243c, zVar.f6505b));
        arrayList.add(new c(c.f6244d, d.a.u.a.x(zVar.f6504a)));
        String strC = zVar.f6506c.c("Host");
        if (strC != null) {
            arrayList.add(new c(c.f6246f, strC));
        }
        arrayList.add(new c(c.f6245e, zVar.f6504a.f6440b));
        int iG = rVar.g();
        for (int i3 = 0; i3 < iG; i3++) {
            h.i iVarE = h.i.e(rVar.d(i3).toLowerCase(Locale.US));
            if (!f6272a.contains(iVarE.o())) {
                arrayList.add(new c(iVarE, rVar.h(i3)));
            }
        }
        g gVar = this.f6276e;
        boolean z3 = !z2;
        synchronized (gVar.t) {
            synchronized (gVar) {
                if (gVar.f6288h > 1073741823) {
                    gVar.L(b.REFUSED_STREAM);
                }
                if (gVar.f6289i) {
                    throw new g.i0.i.a();
                }
                i2 = gVar.f6288h;
                gVar.f6288h = i2 + 2;
                pVar = new p(i2, gVar, z3, false, null);
                z = !z2 || gVar.o == 0 || pVar.f6342b == 0;
                if (pVar.h()) {
                    gVar.f6285e.put(Integer.valueOf(i2), pVar);
                }
            }
            q qVar = gVar.t;
            synchronized (qVar) {
                if (qVar.f6366g) {
                    throw new IOException("closed");
                }
                qVar.B(z3, i2, arrayList);
            }
        }
        if (z) {
            gVar.t.flush();
        }
        this.f6277f = pVar;
        p.c cVar = pVar.f6349i;
        long j2 = ((g.i0.g.f) this.f6274c).f6207j;
        TimeUnit timeUnit = TimeUnit.MILLISECONDS;
        cVar.g(j2, timeUnit);
        this.f6277f.f6350j.g(((g.i0.g.f) this.f6274c).k, timeUnit);
    }

    @Override // g.i0.g.c
    public f0 d(d0 d0Var) {
        Objects.requireNonNull(this.f6275d.f6186f);
        String strC = d0Var.f6080g.c("Content-Type");
        if (strC == null) {
            strC = null;
        }
        long jA = g.i0.g.e.a(d0Var);
        a aVar = new a(this.f6277f.f6347g);
        Logger logger = h.p.f6550a;
        return new g.i0.g.g(strC, jA, new h.s(aVar));
    }

    @Override // g.i0.g.c
    public void e() {
        this.f6276e.t.flush();
    }

    @Override // g.i0.g.c
    public h.w f(z zVar, long j2) {
        return this.f6277f.f();
    }

    @Override // g.i0.g.c
    public d0.a g(boolean z) throws ProtocolException {
        g.r rVarRemoveFirst;
        p pVar = this.f6277f;
        synchronized (pVar) {
            pVar.f6349i.i();
            while (pVar.f6345e.isEmpty() && pVar.k == null) {
                try {
                    pVar.j();
                } catch (Throwable th) {
                    pVar.f6349i.n();
                    throw th;
                }
            }
            pVar.f6349i.n();
            if (pVar.f6345e.isEmpty()) {
                throw new u(pVar.k);
            }
            rVarRemoveFirst = pVar.f6345e.removeFirst();
        }
        x xVar = this.f6278g;
        ArrayList arrayList = new ArrayList(20);
        int iG = rVarRemoveFirst.g();
        g.i0.g.i iVarA = null;
        for (int i2 = 0; i2 < iG; i2++) {
            String strD = rVarRemoveFirst.d(i2);
            String strH = rVarRemoveFirst.h(i2);
            if (strD.equals(":status")) {
                iVarA = g.i0.g.i.a("HTTP/1.1 " + strH);
            } else if (!f6273b.contains(strD)) {
                Objects.requireNonNull((w.a) g.i0.a.f6138a);
                arrayList.add(strD);
                arrayList.add(strH.trim());
            }
        }
        if (iVarA == null) {
            throw new ProtocolException("Expected ':status' header not present");
        }
        d0.a aVar = new d0.a();
        aVar.f6085b = xVar;
        aVar.f6086c = iVarA.f6216b;
        aVar.f6087d = iVarA.f6217c;
        String[] strArr = (String[]) arrayList.toArray(new String[arrayList.size()]);
        r.a aVar2 = new r.a();
        Collections.addAll(aVar2.f6438a, strArr);
        aVar.f6089f = aVar2;
        if (z) {
            Objects.requireNonNull((w.a) g.i0.a.f6138a);
            if (aVar.f6086c == 100) {
                return null;
            }
        }
        return aVar;
    }
}
