package g.i0.h;

import g.d0;
import g.f0;
import g.i0.g.i;
import g.r;
import g.s;
import g.w;
import g.z;
import h.h;
import h.l;
import h.p;
import h.x;
import h.y;
import java.io.EOFException;
import java.io.IOException;
import java.net.ProtocolException;
import java.net.Proxy;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

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

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final w f6218a;

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

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

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

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public int f6222e = 0;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public long f6223f = 262144;

    public abstract class b implements x {

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        public final l f6224b;

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

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

        public b(C0090a c0090a) {
            this.f6224b = new l(a.this.f6220c.e());
        }

        public final void a(boolean z, IOException iOException) {
            a aVar = a.this;
            int i2 = aVar.f6222e;
            if (i2 == 6) {
                return;
            }
            if (i2 != 5) {
                StringBuilder sbC = a.b.c.a.a.C("state: ");
                sbC.append(a.this.f6222e);
                throw new IllegalStateException(sbC.toString());
            }
            aVar.h(this.f6224b);
            a aVar2 = a.this;
            aVar2.f6222e = 6;
            g.i0.f.g gVar = aVar2.f6219b;
            if (gVar != null) {
                gVar.i(!z, aVar2, this.f6226d, iOException);
            }
        }

        @Override // h.x
        public y e() {
            return this.f6224b;
        }

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

    public final class c implements h.w {

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        public final l f6228b;

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

        public c() {
            this.f6228b = new l(a.this.f6221d.e());
        }

        @Override // h.w, java.io.Closeable, java.lang.AutoCloseable
        public synchronized void close() {
            if (this.f6229c) {
                return;
            }
            this.f6229c = true;
            a.this.f6221d.C("0\r\n\r\n");
            a.this.h(this.f6228b);
            a.this.f6222e = 3;
        }

        @Override // h.w
        public y e() {
            return this.f6228b;
        }

        @Override // h.w, java.io.Flushable
        public synchronized void flush() {
            if (this.f6229c) {
                return;
            }
            a.this.f6221d.flush();
        }

        @Override // h.w
        public void h(h.f fVar, long j2) {
            if (this.f6229c) {
                throw new IllegalStateException("closed");
            }
            if (j2 == 0) {
                return;
            }
            a.this.f6221d.j(j2);
            a.this.f6221d.C("\r\n");
            a.this.f6221d.h(fVar, j2);
            a.this.f6221d.C("\r\n");
        }
    }

    public class d extends b {

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        public final s f6231f;

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

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

        public d(s sVar) {
            super(null);
            this.f6232g = -1L;
            this.f6233h = true;
            this.f6231f = sVar;
        }

        @Override // h.x, java.io.Closeable, java.lang.AutoCloseable
        public void close() {
            if (this.f6225c) {
                return;
            }
            if (this.f6233h && !g.i0.c.j(this, 100, TimeUnit.MILLISECONDS)) {
                a(false, null);
            }
            this.f6225c = true;
        }

        @Override // g.i0.h.a.b, h.x
        public long v(h.f fVar, long j2) throws IOException {
            if (j2 < 0) {
                throw new IllegalArgumentException("byteCount < 0: " + j2);
            }
            if (this.f6225c) {
                throw new IllegalStateException("closed");
            }
            if (!this.f6233h) {
                return -1L;
            }
            long j3 = this.f6232g;
            if (j3 == 0 || j3 == -1) {
                if (j3 != -1) {
                    a.this.f6220c.n();
                }
                try {
                    this.f6232g = a.this.f6220c.H();
                    String strTrim = a.this.f6220c.n().trim();
                    if (this.f6232g < 0 || !(strTrim.isEmpty() || strTrim.startsWith(";"))) {
                        throw new ProtocolException("expected chunk size and optional extensions but was \"" + this.f6232g + strTrim + "\"");
                    }
                    if (this.f6232g == 0) {
                        this.f6233h = false;
                        a aVar = a.this;
                        g.i0.g.e.d(aVar.f6218a.k, this.f6231f, aVar.k());
                        a(true, null);
                    }
                    if (!this.f6233h) {
                        return -1L;
                    }
                } catch (NumberFormatException e2) {
                    throw new ProtocolException(e2.getMessage());
                }
            }
            long jV = super.v(fVar, Math.min(j2, this.f6232g));
            if (jV != -1) {
                this.f6232g -= jV;
                return jV;
            }
            ProtocolException protocolException = new ProtocolException("unexpected end of stream");
            a(false, protocolException);
            throw protocolException;
        }
    }

    public final class e implements h.w {

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        public final l f6235b;

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

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

        public e(long j2) {
            this.f6235b = new l(a.this.f6221d.e());
            this.f6237d = j2;
        }

        @Override // h.w, java.io.Closeable, java.lang.AutoCloseable
        public void close() throws ProtocolException {
            if (this.f6236c) {
                return;
            }
            this.f6236c = true;
            if (this.f6237d > 0) {
                throw new ProtocolException("unexpected end of stream");
            }
            a.this.h(this.f6235b);
            a.this.f6222e = 3;
        }

        @Override // h.w
        public y e() {
            return this.f6235b;
        }

        @Override // h.w, java.io.Flushable
        public void flush() {
            if (this.f6236c) {
                return;
            }
            a.this.f6221d.flush();
        }

        @Override // h.w
        public void h(h.f fVar, long j2) throws ProtocolException {
            if (this.f6236c) {
                throw new IllegalStateException("closed");
            }
            g.i0.c.c(fVar.f6529d, 0L, j2);
            if (j2 <= this.f6237d) {
                a.this.f6221d.h(fVar, j2);
                this.f6237d -= j2;
            } else {
                StringBuilder sbC = a.b.c.a.a.C("expected ");
                sbC.append(this.f6237d);
                sbC.append(" bytes but received ");
                sbC.append(j2);
                throw new ProtocolException(sbC.toString());
            }
        }
    }

    public class f extends b {

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

        public f(a aVar, long j2) {
            super(null);
            this.f6239f = j2;
            if (j2 == 0) {
                a(true, null);
            }
        }

        @Override // h.x, java.io.Closeable, java.lang.AutoCloseable
        public void close() {
            if (this.f6225c) {
                return;
            }
            if (this.f6239f != 0 && !g.i0.c.j(this, 100, TimeUnit.MILLISECONDS)) {
                a(false, null);
            }
            this.f6225c = true;
        }

        @Override // g.i0.h.a.b, h.x
        public long v(h.f fVar, long j2) throws IOException {
            if (j2 < 0) {
                throw new IllegalArgumentException("byteCount < 0: " + j2);
            }
            if (this.f6225c) {
                throw new IllegalStateException("closed");
            }
            long j3 = this.f6239f;
            if (j3 == 0) {
                return -1L;
            }
            long jV = super.v(fVar, Math.min(j3, j2));
            if (jV == -1) {
                ProtocolException protocolException = new ProtocolException("unexpected end of stream");
                a(false, protocolException);
                throw protocolException;
            }
            long j4 = this.f6239f - jV;
            this.f6239f = j4;
            if (j4 == 0) {
                a(true, null);
            }
            return jV;
        }
    }

    public class g extends b {

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

        public g(a aVar) {
            super(null);
        }

        @Override // h.x, java.io.Closeable, java.lang.AutoCloseable
        public void close() {
            if (this.f6225c) {
                return;
            }
            if (!this.f6240f) {
                a(false, null);
            }
            this.f6225c = true;
        }

        @Override // g.i0.h.a.b, h.x
        public long v(h.f fVar, long j2) throws IOException {
            if (j2 < 0) {
                throw new IllegalArgumentException("byteCount < 0: " + j2);
            }
            if (this.f6225c) {
                throw new IllegalStateException("closed");
            }
            if (this.f6240f) {
                return -1L;
            }
            long jV = super.v(fVar, j2);
            if (jV != -1) {
                return jV;
            }
            this.f6240f = true;
            a(true, null);
            return -1L;
        }
    }

    public a(w wVar, g.i0.f.g gVar, h hVar, h.g gVar2) {
        this.f6218a = wVar;
        this.f6219b = gVar;
        this.f6220c = hVar;
        this.f6221d = gVar2;
    }

    @Override // g.i0.g.c
    public void a() {
        g.i0.f.c cVarB = this.f6219b.b();
        if (cVarB != null) {
            g.i0.c.e(cVarB.f6161d);
        }
    }

    @Override // g.i0.g.c
    public void b() {
        this.f6221d.flush();
    }

    @Override // g.i0.g.c
    public void c(z zVar) {
        Proxy.Type type = this.f6219b.b().f6160c.f6116b.type();
        StringBuilder sb = new StringBuilder();
        sb.append(zVar.f6505b);
        sb.append(' ');
        if (!zVar.f6504a.f6440b.equals("https") && type == Proxy.Type.HTTP) {
            sb.append(zVar.f6504a);
        } else {
            sb.append(d.a.u.a.x(zVar.f6504a));
        }
        sb.append(" HTTP/1.1");
        l(zVar.f6506c, sb.toString());
    }

    @Override // g.i0.g.c
    public f0 d(d0 d0Var) {
        Objects.requireNonNull(this.f6219b.f6186f);
        String strC = d0Var.f6080g.c("Content-Type");
        if (strC == null) {
            strC = null;
        }
        if (!g.i0.g.e.b(d0Var)) {
            x xVarI = i(0L);
            Logger logger = p.f6550a;
            return new g.i0.g.g(strC, 0L, new h.s(xVarI));
        }
        String strC2 = d0Var.f6080g.c("Transfer-Encoding");
        if ("chunked".equalsIgnoreCase(strC2 != null ? strC2 : null)) {
            s sVar = d0Var.f6075b.f6504a;
            if (this.f6222e != 4) {
                StringBuilder sbC = a.b.c.a.a.C("state: ");
                sbC.append(this.f6222e);
                throw new IllegalStateException(sbC.toString());
            }
            this.f6222e = 5;
            d dVar = new d(sVar);
            Logger logger2 = p.f6550a;
            return new g.i0.g.g(strC, -1L, new h.s(dVar));
        }
        long jA = g.i0.g.e.a(d0Var);
        if (jA != -1) {
            x xVarI2 = i(jA);
            Logger logger3 = p.f6550a;
            return new g.i0.g.g(strC, jA, new h.s(xVarI2));
        }
        if (this.f6222e != 4) {
            StringBuilder sbC2 = a.b.c.a.a.C("state: ");
            sbC2.append(this.f6222e);
            throw new IllegalStateException(sbC2.toString());
        }
        g.i0.f.g gVar = this.f6219b;
        if (gVar == null) {
            throw new IllegalStateException("streamAllocation == null");
        }
        this.f6222e = 5;
        gVar.f();
        g gVar2 = new g(this);
        Logger logger4 = p.f6550a;
        return new g.i0.g.g(strC, -1L, new h.s(gVar2));
    }

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

    @Override // g.i0.g.c
    public h.w f(z zVar, long j2) {
        if ("chunked".equalsIgnoreCase(zVar.f6506c.c("Transfer-Encoding"))) {
            if (this.f6222e == 1) {
                this.f6222e = 2;
                return new c();
            }
            StringBuilder sbC = a.b.c.a.a.C("state: ");
            sbC.append(this.f6222e);
            throw new IllegalStateException(sbC.toString());
        }
        if (j2 == -1) {
            throw new IllegalStateException("Cannot stream a request body without chunked encoding or a known content length!");
        }
        if (this.f6222e == 1) {
            this.f6222e = 2;
            return new e(j2);
        }
        StringBuilder sbC2 = a.b.c.a.a.C("state: ");
        sbC2.append(this.f6222e);
        throw new IllegalStateException(sbC2.toString());
    }

    @Override // g.i0.g.c
    public d0.a g(boolean z) {
        int i2 = this.f6222e;
        if (i2 != 1 && i2 != 3) {
            StringBuilder sbC = a.b.c.a.a.C("state: ");
            sbC.append(this.f6222e);
            throw new IllegalStateException(sbC.toString());
        }
        try {
            i iVarA = i.a(j());
            d0.a aVar = new d0.a();
            aVar.f6085b = iVarA.f6215a;
            aVar.f6086c = iVarA.f6216b;
            aVar.f6087d = iVarA.f6217c;
            aVar.d(k());
            if (z && iVarA.f6216b == 100) {
                return null;
            }
            if (iVarA.f6216b == 100) {
                this.f6222e = 3;
                return aVar;
            }
            this.f6222e = 4;
            return aVar;
        } catch (EOFException e2) {
            StringBuilder sbC2 = a.b.c.a.a.C("unexpected end of stream on ");
            sbC2.append(this.f6219b);
            IOException iOException = new IOException(sbC2.toString());
            iOException.initCause(e2);
            throw iOException;
        }
    }

    public void h(l lVar) {
        y yVar = lVar.f6538e;
        lVar.f6538e = y.f6571a;
        yVar.a();
        yVar.b();
    }

    public x i(long j2) {
        if (this.f6222e == 4) {
            this.f6222e = 5;
            return new f(this, j2);
        }
        StringBuilder sbC = a.b.c.a.a.C("state: ");
        sbC.append(this.f6222e);
        throw new IllegalStateException(sbC.toString());
    }

    public final String j() {
        String strW = this.f6220c.w(this.f6223f);
        this.f6223f -= (long) strW.length();
        return strW;
    }

    public r k() {
        r.a aVar = new r.a();
        while (true) {
            String strJ = j();
            if (strJ.length() == 0) {
                return new r(aVar);
            }
            Objects.requireNonNull((w.a) g.i0.a.f6138a);
            int iIndexOf = strJ.indexOf(":", 1);
            if (iIndexOf != -1) {
                aVar.b(strJ.substring(0, iIndexOf), strJ.substring(iIndexOf + 1));
            } else {
                if (strJ.startsWith(":")) {
                    strJ = strJ.substring(1);
                }
                aVar.f6438a.add("");
                aVar.f6438a.add(strJ.trim());
            }
        }
    }

    public void l(r rVar, String str) {
        if (this.f6222e != 0) {
            StringBuilder sbC = a.b.c.a.a.C("state: ");
            sbC.append(this.f6222e);
            throw new IllegalStateException(sbC.toString());
        }
        this.f6221d.C(str).C("\r\n");
        int iG = rVar.g();
        for (int i2 = 0; i2 < iG; i2++) {
            this.f6221d.C(rVar.d(i2)).C(": ").C(rVar.h(i2)).C("\r\n");
        }
        this.f6221d.C("\r\n");
        this.f6222e = 1;
    }
}
