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> f6286a = 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> f6287b = 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 f6288c;

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

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

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

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

    public class a extends h.k {

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

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

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

        public final void a(IOException iOException) {
            if (this.f6293c) {
                return;
            }
            this.f6293c = true;
            f fVar = f.this;
            fVar.f6289d.i(false, fVar, this.f6294d, iOException);
        }

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

        @Override // h.x
        public long v(h.f fVar, long j2) throws IOException {
            try {
                long jV = this.f6551b.v(fVar, j2);
                if (jV > 0) {
                    this.f6294d += 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.f6288c = aVar;
        this.f6289d = gVar;
        this.f6290e = gVar2;
        List<x> list = wVar.f6493e;
        x xVar = x.H2_PRIOR_KNOWLEDGE;
        this.f6292g = list.contains(xVar) ? xVar : x.HTTP_2;
    }

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

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

    @Override // g.i0.g.c
    public void c(z zVar) {
        int i2;
        p pVar;
        boolean z;
        if (this.f6291f != null) {
            return;
        }
        boolean z2 = zVar.f6521d != null;
        g.r rVar = zVar.f6520c;
        ArrayList arrayList = new ArrayList(rVar.g() + 4);
        arrayList.add(new c(c.f6257c, zVar.f6519b));
        arrayList.add(new c(c.f6258d, d.a.u.a.y(zVar.f6518a)));
        String strC = zVar.f6520c.c("Host");
        if (strC != null) {
            arrayList.add(new c(c.f6260f, strC));
        }
        arrayList.add(new c(c.f6259e, zVar.f6518a.f6454b));
        int iG = rVar.g();
        for (int i3 = 0; i3 < iG; i3++) {
            h.i iVarE = h.i.e(rVar.d(i3).toLowerCase(Locale.US));
            if (!f6286a.contains(iVarE.o())) {
                arrayList.add(new c(iVarE, rVar.h(i3)));
            }
        }
        g gVar = this.f6290e;
        boolean z3 = !z2;
        synchronized (gVar.t) {
            synchronized (gVar) {
                if (gVar.f6302h > 1073741823) {
                    gVar.L(b.REFUSED_STREAM);
                }
                if (gVar.f6303i) {
                    throw new g.i0.i.a();
                }
                i2 = gVar.f6302h;
                gVar.f6302h = i2 + 2;
                pVar = new p(i2, gVar, z3, false, null);
                z = !z2 || gVar.o == 0 || pVar.f6356b == 0;
                if (pVar.h()) {
                    gVar.f6299e.put(Integer.valueOf(i2), pVar);
                }
            }
            q qVar = gVar.t;
            synchronized (qVar) {
                if (qVar.f6380g) {
                    throw new IOException("closed");
                }
                qVar.B(z3, i2, arrayList);
            }
        }
        if (z) {
            gVar.t.flush();
        }
        this.f6291f = pVar;
        p.c cVar = pVar.f6363i;
        long j2 = ((g.i0.g.f) this.f6288c).f6221j;
        TimeUnit timeUnit = TimeUnit.MILLISECONDS;
        cVar.g(j2, timeUnit);
        this.f6291f.f6364j.g(((g.i0.g.f) this.f6288c).k, timeUnit);
    }

    @Override // g.i0.g.c
    public f0 d(d0 d0Var) {
        Objects.requireNonNull(this.f6289d.f6200f);
        String strC = d0Var.f6094g.c("Content-Type");
        if (strC == null) {
            strC = null;
        }
        long jA = g.i0.g.e.a(d0Var);
        a aVar = new a(this.f6291f.f6361g);
        Logger logger = h.p.f6564a;
        return new g.i0.g.g(strC, jA, new h.s(aVar));
    }

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

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

    @Override // g.i0.g.c
    public d0.a g(boolean z) throws ProtocolException {
        g.r rVarRemoveFirst;
        p pVar = this.f6291f;
        synchronized (pVar) {
            pVar.f6363i.i();
            while (pVar.f6359e.isEmpty() && pVar.k == null) {
                try {
                    pVar.j();
                } catch (Throwable th) {
                    pVar.f6363i.n();
                    throw th;
                }
            }
            pVar.f6363i.n();
            if (pVar.f6359e.isEmpty()) {
                throw new u(pVar.k);
            }
            rVarRemoveFirst = pVar.f6359e.removeFirst();
        }
        x xVar = this.f6292g;
        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 (!f6287b.contains(strD)) {
                Objects.requireNonNull((w.a) g.i0.a.f6152a);
                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.f6099b = xVar;
        aVar.f6100c = iVarA.f6230b;
        aVar.f6101d = iVarA.f6231c;
        String[] strArr = (String[]) arrayList.toArray(new String[arrayList.size()]);
        r.a aVar2 = new r.a();
        Collections.addAll(aVar2.f6452a, strArr);
        aVar.f6103f = aVar2;
        if (z) {
            Objects.requireNonNull((w.a) g.i0.a.f6152a);
            if (aVar.f6100c == 100) {
                return null;
            }
        }
        return aVar;
    }
}
