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 f6232a;

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

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

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

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

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

    public abstract class b implements x {

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

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

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

        public b(C0086a c0086a) {
            this.f6238b = new l(a.this.f6234c.e());
        }

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

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

        @Override // h.x
        public long v(h.f fVar, long j2) throws IOException {
            try {
                long jV = a.this.f6234c.v(fVar, j2);
                if (jV > 0) {
                    this.f6240d += 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 f6242b;

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

        public c() {
            this.f6242b = new l(a.this.f6235d.e());
        }

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

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

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

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

    public class d extends b {

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

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

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

        public d(s sVar) {
            super(null);
            this.f6246g = -1L;
            this.f6247h = true;
            this.f6245f = sVar;
        }

        @Override // h.x, java.io.Closeable, java.lang.AutoCloseable
        public void close() {
            if (this.f6239c) {
                return;
            }
            if (this.f6247h && !g.i0.c.j(this, 100, TimeUnit.MILLISECONDS)) {
                a(false, null);
            }
            this.f6239c = 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.f6239c) {
                throw new IllegalStateException("closed");
            }
            if (!this.f6247h) {
                return -1L;
            }
            long j3 = this.f6246g;
            if (j3 == 0 || j3 == -1) {
                if (j3 != -1) {
                    a.this.f6234c.n();
                }
                try {
                    this.f6246g = a.this.f6234c.H();
                    String strTrim = a.this.f6234c.n().trim();
                    if (this.f6246g < 0 || !(strTrim.isEmpty() || strTrim.startsWith(";"))) {
                        throw new ProtocolException("expected chunk size and optional extensions but was \"" + this.f6246g + strTrim + "\"");
                    }
                    if (this.f6246g == 0) {
                        this.f6247h = false;
                        a aVar = a.this;
                        g.i0.g.e.d(aVar.f6232a.k, this.f6245f, aVar.k());
                        a(true, null);
                    }
                    if (!this.f6247h) {
                        return -1L;
                    }
                } catch (NumberFormatException e2) {
                    throw new ProtocolException(e2.getMessage());
                }
            }
            long jV = super.v(fVar, Math.min(j2, this.f6246g));
            if (jV != -1) {
                this.f6246g -= 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 f6249b;

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

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

        public e(long j2) {
            this.f6249b = new l(a.this.f6235d.e());
            this.f6251d = j2;
        }

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

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

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

        @Override // h.w
        public void h(h.f fVar, long j2) throws ProtocolException {
            if (this.f6250c) {
                throw new IllegalStateException("closed");
            }
            g.i0.c.c(fVar.f6543d, 0L, j2);
            if (j2 <= this.f6251d) {
                a.this.f6235d.h(fVar, j2);
                this.f6251d -= j2;
            } else {
                StringBuilder sbC = a.b.c.a.a.C("expected ");
                sbC.append(this.f6251d);
                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 f6253f;

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

        @Override // h.x, java.io.Closeable, java.lang.AutoCloseable
        public void close() {
            if (this.f6239c) {
                return;
            }
            if (this.f6253f != 0 && !g.i0.c.j(this, 100, TimeUnit.MILLISECONDS)) {
                a(false, null);
            }
            this.f6239c = 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.f6239c) {
                throw new IllegalStateException("closed");
            }
            long j3 = this.f6253f;
            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.f6253f - jV;
            this.f6253f = 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 f6254f;

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

        @Override // h.x, java.io.Closeable, java.lang.AutoCloseable
        public void close() {
            if (this.f6239c) {
                return;
            }
            if (!this.f6254f) {
                a(false, null);
            }
            this.f6239c = 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.f6239c) {
                throw new IllegalStateException("closed");
            }
            if (this.f6254f) {
                return -1L;
            }
            long jV = super.v(fVar, j2);
            if (jV != -1) {
                return jV;
            }
            this.f6254f = true;
            a(true, null);
            return -1L;
        }
    }

    public a(w wVar, g.i0.f.g gVar, h hVar, h.g gVar2) {
        this.f6232a = wVar;
        this.f6233b = gVar;
        this.f6234c = hVar;
        this.f6235d = gVar2;
    }

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

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

    @Override // g.i0.g.c
    public void c(z zVar) {
        Proxy.Type type = this.f6233b.b().f6174c.f6130b.type();
        StringBuilder sb = new StringBuilder();
        sb.append(zVar.f6519b);
        sb.append(' ');
        if (!zVar.f6518a.f6454b.equals("https") && type == Proxy.Type.HTTP) {
            sb.append(zVar.f6518a);
        } else {
            sb.append(d.a.u.a.y(zVar.f6518a));
        }
        sb.append(" HTTP/1.1");
        l(zVar.f6520c, sb.toString());
    }

    @Override // g.i0.g.c
    public f0 d(d0 d0Var) {
        Objects.requireNonNull(this.f6233b.f6200f);
        String strC = d0Var.f6094g.c("Content-Type");
        if (strC == null) {
            strC = null;
        }
        if (!g.i0.g.e.b(d0Var)) {
            x xVarI = i(0L);
            Logger logger = p.f6564a;
            return new g.i0.g.g(strC, 0L, new h.s(xVarI));
        }
        String strC2 = d0Var.f6094g.c("Transfer-Encoding");
        if ("chunked".equalsIgnoreCase(strC2 != null ? strC2 : null)) {
            s sVar = d0Var.f6089b.f6518a;
            if (this.f6236e != 4) {
                StringBuilder sbC = a.b.c.a.a.C("state: ");
                sbC.append(this.f6236e);
                throw new IllegalStateException(sbC.toString());
            }
            this.f6236e = 5;
            d dVar = new d(sVar);
            Logger logger2 = p.f6564a;
            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.f6564a;
            return new g.i0.g.g(strC, jA, new h.s(xVarI2));
        }
        if (this.f6236e != 4) {
            StringBuilder sbC2 = a.b.c.a.a.C("state: ");
            sbC2.append(this.f6236e);
            throw new IllegalStateException(sbC2.toString());
        }
        g.i0.f.g gVar = this.f6233b;
        if (gVar == null) {
            throw new IllegalStateException("streamAllocation == null");
        }
        this.f6236e = 5;
        gVar.f();
        g gVar2 = new g(this);
        Logger logger4 = p.f6564a;
        return new g.i0.g.g(strC, -1L, new h.s(gVar2));
    }

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

    @Override // g.i0.g.c
    public h.w f(z zVar, long j2) {
        if ("chunked".equalsIgnoreCase(zVar.f6520c.c("Transfer-Encoding"))) {
            if (this.f6236e == 1) {
                this.f6236e = 2;
                return new c();
            }
            StringBuilder sbC = a.b.c.a.a.C("state: ");
            sbC.append(this.f6236e);
            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.f6236e == 1) {
            this.f6236e = 2;
            return new e(j2);
        }
        StringBuilder sbC2 = a.b.c.a.a.C("state: ");
        sbC2.append(this.f6236e);
        throw new IllegalStateException(sbC2.toString());
    }

    @Override // g.i0.g.c
    public d0.a g(boolean z) {
        int i2 = this.f6236e;
        if (i2 != 1 && i2 != 3) {
            StringBuilder sbC = a.b.c.a.a.C("state: ");
            sbC.append(this.f6236e);
            throw new IllegalStateException(sbC.toString());
        }
        try {
            i iVarA = i.a(j());
            d0.a aVar = new d0.a();
            aVar.f6099b = iVarA.f6229a;
            aVar.f6100c = iVarA.f6230b;
            aVar.f6101d = iVarA.f6231c;
            aVar.d(k());
            if (z && iVarA.f6230b == 100) {
                return null;
            }
            if (iVarA.f6230b == 100) {
                this.f6236e = 3;
                return aVar;
            }
            this.f6236e = 4;
            return aVar;
        } catch (EOFException e2) {
            StringBuilder sbC2 = a.b.c.a.a.C("unexpected end of stream on ");
            sbC2.append(this.f6233b);
            IOException iOException = new IOException(sbC2.toString());
            iOException.initCause(e2);
            throw iOException;
        }
    }

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

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

    public final String j() {
        String strW = this.f6234c.w(this.f6237f);
        this.f6237f -= (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.f6152a);
            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.f6452a.add("");
                aVar.f6452a.add(strJ.trim());
            }
        }
    }

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