package okhttp3.internal.http1;

import A0.a;
import S4.p;
import S4.w;
import a.AbstractC0872a;
import java.io.EOFException;
import java.io.IOException;
import java.net.ProtocolException;
import java.net.Proxy;
import java.net.Socket;
import java.util.concurrent.TimeUnit;
import kotlin.jvm.internal.o;
import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Protocol;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.internal.Util;
import okhttp3.internal.connection.RealConnection;
import okhttp3.internal.http.ExchangeCodec;
import okhttp3.internal.http.HttpHeaders;
import okhttp3.internal.http.RequestLine;
import okhttp3.internal.http.StatusLine;
import p5.C2740j;
import p5.I;
import p5.J;
import p5.N;
import p5.P;
import p5.S;
import p5.x;

/* JADX INFO: loaded from: classes3.dex */
public final class Http1ExchangeCodec implements ExchangeCodec {

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public static final /* synthetic */ int f22404h = 0;

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

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

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

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final I f22408d;

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

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

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

    public abstract class AbstractSource implements P {

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

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

        public AbstractSource() {
            this.f22412a = new x(Http1ExchangeCodec.this.f22407c.f23058a.timeout());
        }

        public final void a() {
            Http1ExchangeCodec http1ExchangeCodec = Http1ExchangeCodec.this;
            int i6 = http1ExchangeCodec.f22409e;
            if (i6 == 6) {
                return;
            }
            if (i6 != 5) {
                throw new IllegalStateException("state: " + http1ExchangeCodec.f22409e);
            }
            x xVar = this.f22412a;
            S s6 = xVar.f23123e;
            xVar.f23123e = S.f23074d;
            s6.a();
            s6.b();
            http1ExchangeCodec.f22409e = 6;
        }

        @Override // p5.P
        public long read(C2740j sink, long j4) throws IOException {
            Http1ExchangeCodec http1ExchangeCodec = Http1ExchangeCodec.this;
            o.h(sink, "sink");
            try {
                return http1ExchangeCodec.f22407c.read(sink, j4);
            } catch (IOException e6) {
                http1ExchangeCodec.f22406b.k();
                a();
                throw e6;
            }
        }

        @Override // p5.P
        public final S timeout() {
            return this.f22412a;
        }
    }

    public final class ChunkedSink implements N {

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

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

        public ChunkedSink() {
            this.f22415a = new x(Http1ExchangeCodec.this.f22408d.f23055a.timeout());
        }

        @Override // p5.N, java.io.Closeable, java.lang.AutoCloseable
        public final synchronized void close() {
            if (this.f22416b) {
                return;
            }
            this.f22416b = true;
            Http1ExchangeCodec.this.f22408d.n("0\r\n\r\n");
            Http1ExchangeCodec http1ExchangeCodec = Http1ExchangeCodec.this;
            x xVar = this.f22415a;
            http1ExchangeCodec.getClass();
            S s6 = xVar.f23123e;
            xVar.f23123e = S.f23074d;
            s6.a();
            s6.b();
            Http1ExchangeCodec.this.f22409e = 3;
        }

        @Override // p5.N, java.io.Flushable
        public final synchronized void flush() {
            if (this.f22416b) {
                return;
            }
            Http1ExchangeCodec.this.f22408d.flush();
        }

        @Override // p5.N
        public final S timeout() {
            return this.f22415a;
        }

        @Override // p5.N
        public final void write(C2740j source, long j4) {
            o.h(source, "source");
            if (this.f22416b) {
                throw new IllegalStateException("closed");
            }
            if (j4 == 0) {
                return;
            }
            Http1ExchangeCodec http1ExchangeCodec = Http1ExchangeCodec.this;
            I i6 = http1ExchangeCodec.f22408d;
            if (i6.f23057c) {
                throw new IllegalStateException("closed");
            }
            i6.f23056b.S(j4);
            i6.a();
            I i7 = http1ExchangeCodec.f22408d;
            i7.n("\r\n");
            i7.write(source, j4);
            i7.n("\r\n");
        }
    }

    public final class ChunkedSource extends AbstractSource {

        /* JADX INFO: renamed from: d, reason: collision with root package name */
        public final HttpUrl f22418d;

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

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

        /* JADX INFO: renamed from: j, reason: collision with root package name */
        public final /* synthetic */ Http1ExchangeCodec f22421j;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public ChunkedSource(Http1ExchangeCodec http1ExchangeCodec, HttpUrl url) {
            super();
            o.h(url, "url");
            this.f22421j = http1ExchangeCodec;
            this.f22418d = url;
            this.f22419e = -1L;
            this.f22420f = true;
        }

        @Override // java.io.Closeable, java.lang.AutoCloseable
        public final void close() {
            if (this.f22413b) {
                return;
            }
            if (this.f22420f) {
                TimeUnit timeUnit = TimeUnit.MILLISECONDS;
                if (!Util.g(this)) {
                    this.f22421j.f22406b.k();
                    a();
                }
            }
            this.f22413b = true;
        }

        /* JADX WARN: Code restructure failed: missing block: B:55:0x0107, code lost:
        
            if (r18.f22420f == false) goto L56;
         */
        @Override // okhttp3.internal.http1.Http1ExchangeCodec.AbstractSource, p5.P
        /*
            Code decompiled incorrectly, please refer to instructions dump.
        */
        public final long read(C2740j sink, long j4) throws IOException {
            C2740j c2740j;
            long j6;
            byte bF;
            o.h(sink, "sink");
            long j7 = 0;
            if (j4 < 0) {
                throw new IllegalArgumentException(a.m("byteCount < 0: ", j4).toString());
            }
            if (this.f22413b) {
                throw new IllegalStateException("closed");
            }
            if (this.f22420f) {
                long j8 = this.f22419e;
                Http1ExchangeCodec http1ExchangeCodec = this.f22421j;
                if (j8 == 0 || j8 == -1) {
                    if (j8 != -1) {
                        http1ExchangeCodec.f22407c.s();
                    }
                    try {
                        J j9 = http1ExchangeCodec.f22407c;
                        j9.A(1L);
                        int i6 = 0;
                        while (true) {
                            int i7 = i6 + 1;
                            boolean zP = j9.p(i7);
                            c2740j = j9.f23059b;
                            if (!zP) {
                                j6 = j7;
                                break;
                            }
                            j6 = j7;
                            bF = c2740j.f(i6);
                            if ((bF < 48 || bF > 57) && ((bF < 97 || bF > 102) && (bF < 65 || bF > 70))) {
                                break;
                            }
                            i6 = i7;
                            j7 = j6;
                        }
                        if (i6 == 0) {
                            AbstractC0872a.w(16);
                            String string = Integer.toString(bF, 16);
                            o.g(string, "toString(...)");
                            throw new NumberFormatException("Expected leading [0-9a-fA-F] character but was 0x".concat(string));
                        }
                        this.f22419e = c2740j.C();
                        String string2 = p.o1(http1ExchangeCodec.f22407c.z(Long.MAX_VALUE)).toString();
                        if (this.f22419e < j6 || (string2.length() > 0 && !w.A0(string2, ";", false))) {
                            throw new ProtocolException("expected chunk size and optional extensions but was \"" + this.f22419e + string2 + '\"');
                        }
                        if (this.f22419e == j6) {
                            this.f22420f = false;
                            HeadersReader headersReader = http1ExchangeCodec.f22410f;
                            headersReader.getClass();
                            Headers.Builder builder = new Headers.Builder();
                            while (true) {
                                String strZ = headersReader.f22402a.z(headersReader.f22403b);
                                headersReader.f22403b -= (long) strZ.length();
                                if (strZ.length() == 0) {
                                    break;
                                }
                                builder.b(strZ);
                            }
                            http1ExchangeCodec.f22411g = builder.e();
                            OkHttpClient okHttpClient = http1ExchangeCodec.f22405a;
                            o.e(okHttpClient);
                            Headers headers = http1ExchangeCodec.f22411g;
                            o.e(headers);
                            HttpHeaders.d(okHttpClient.f22080m, this.f22418d, headers);
                            a();
                        }
                    } catch (NumberFormatException e6) {
                        throw new ProtocolException(e6.getMessage());
                    }
                }
                long j10 = super.read(sink, Math.min(j4, this.f22419e));
                if (j10 != -1) {
                    this.f22419e -= j10;
                    return j10;
                }
                http1ExchangeCodec.f22406b.k();
                ProtocolException protocolException = new ProtocolException("unexpected end of stream");
                a();
                throw protocolException;
            }
            return -1L;
        }
    }

    public static final class Companion {
        public /* synthetic */ Companion(int i6) {
            this();
        }

        private Companion() {
        }
    }

    public final class FixedLengthSource extends AbstractSource {

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

        public FixedLengthSource(long j4) {
            super();
            this.f22422d = j4;
            if (j4 == 0) {
                a();
            }
        }

        @Override // java.io.Closeable, java.lang.AutoCloseable
        public final void close() {
            if (this.f22413b) {
                return;
            }
            if (this.f22422d != 0) {
                TimeUnit timeUnit = TimeUnit.MILLISECONDS;
                if (!Util.g(this)) {
                    Http1ExchangeCodec.this.f22406b.k();
                    a();
                }
            }
            this.f22413b = true;
        }

        @Override // okhttp3.internal.http1.Http1ExchangeCodec.AbstractSource, p5.P
        public final long read(C2740j sink, long j4) throws IOException {
            o.h(sink, "sink");
            if (j4 < 0) {
                throw new IllegalArgumentException(a.m("byteCount < 0: ", j4).toString());
            }
            if (this.f22413b) {
                throw new IllegalStateException("closed");
            }
            long j6 = this.f22422d;
            if (j6 == 0) {
                return -1L;
            }
            long j7 = super.read(sink, Math.min(j6, j4));
            if (j7 == -1) {
                Http1ExchangeCodec.this.f22406b.k();
                ProtocolException protocolException = new ProtocolException("unexpected end of stream");
                a();
                throw protocolException;
            }
            long j8 = this.f22422d - j7;
            this.f22422d = j8;
            if (j8 == 0) {
                a();
            }
            return j7;
        }
    }

    public final class KnownLengthSink implements N {

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

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

        public KnownLengthSink() {
            this.f22424a = new x(Http1ExchangeCodec.this.f22408d.f23055a.timeout());
        }

        @Override // p5.N, java.io.Closeable, java.lang.AutoCloseable
        public final void close() {
            if (this.f22425b) {
                return;
            }
            this.f22425b = true;
            int i6 = Http1ExchangeCodec.f22404h;
            Http1ExchangeCodec http1ExchangeCodec = Http1ExchangeCodec.this;
            http1ExchangeCodec.getClass();
            x xVar = this.f22424a;
            S s6 = xVar.f23123e;
            xVar.f23123e = S.f23074d;
            s6.a();
            s6.b();
            http1ExchangeCodec.f22409e = 3;
        }

        @Override // p5.N, java.io.Flushable
        public final void flush() {
            if (this.f22425b) {
                return;
            }
            Http1ExchangeCodec.this.f22408d.flush();
        }

        @Override // p5.N
        public final S timeout() {
            return this.f22424a;
        }

        @Override // p5.N
        public final void write(C2740j source, long j4) {
            o.h(source, "source");
            if (this.f22425b) {
                throw new IllegalStateException("closed");
            }
            long j6 = source.f23100b;
            byte[] bArr = Util.f22179a;
            if (j4 < 0 || 0 > j6 || j6 < j4) {
                throw new ArrayIndexOutOfBoundsException();
            }
            Http1ExchangeCodec.this.f22408d.write(source, j4);
        }
    }

    public final class UnknownLengthSource extends AbstractSource {

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

        @Override // java.io.Closeable, java.lang.AutoCloseable
        public final void close() {
            if (this.f22413b) {
                return;
            }
            if (!this.f22427d) {
                a();
            }
            this.f22413b = true;
        }

        @Override // okhttp3.internal.http1.Http1ExchangeCodec.AbstractSource, p5.P
        public final long read(C2740j sink, long j4) throws IOException {
            o.h(sink, "sink");
            if (j4 < 0) {
                throw new IllegalArgumentException(a.m("byteCount < 0: ", j4).toString());
            }
            if (this.f22413b) {
                throw new IllegalStateException("closed");
            }
            if (this.f22427d) {
                return -1L;
            }
            long j6 = super.read(sink, j4);
            if (j6 != -1) {
                return j6;
            }
            this.f22427d = true;
            a();
            return -1L;
        }
    }

    static {
        new Companion(0);
    }

    public Http1ExchangeCodec(OkHttpClient okHttpClient, RealConnection connection, J source, I sink) {
        o.h(connection, "connection");
        o.h(source, "source");
        o.h(sink, "sink");
        this.f22405a = okHttpClient;
        this.f22406b = connection;
        this.f22407c = source;
        this.f22408d = sink;
        this.f22410f = new HeadersReader(source);
    }

    @Override // okhttp3.internal.http.ExchangeCodec
    public final void a() {
        this.f22408d.flush();
    }

    @Override // okhttp3.internal.http.ExchangeCodec
    public final void b(Request request) {
        o.h(request, "request");
        RequestLine requestLine = RequestLine.f22396a;
        Proxy.Type type = this.f22406b.f22337b.f22169b.type();
        o.g(type, "connection.route().proxy.type()");
        requestLine.getClass();
        StringBuilder sb = new StringBuilder();
        sb.append(request.f22126b);
        sb.append(' ');
        HttpUrl httpUrl = request.f22125a;
        if (httpUrl.f22033j || type != Proxy.Type.HTTP) {
            sb.append(RequestLine.a(httpUrl));
        } else {
            sb.append(httpUrl);
        }
        sb.append(" HTTP/1.1");
        String string = sb.toString();
        o.g(string, "StringBuilder().apply(builderAction).toString()");
        k(request.f22127c, string);
    }

    @Override // okhttp3.internal.http.ExchangeCodec
    public final P c(Response response) {
        if (!HttpHeaders.a(response)) {
            return i(0L);
        }
        if ("chunked".equalsIgnoreCase(Response.b("Transfer-Encoding", response))) {
            HttpUrl httpUrl = response.f22140a.f22125a;
            if (this.f22409e == 4) {
                this.f22409e = 5;
                return new ChunkedSource(this, httpUrl);
            }
            throw new IllegalStateException(("state: " + this.f22409e).toString());
        }
        long j4 = Util.j(response);
        if (j4 != -1) {
            return i(j4);
        }
        if (this.f22409e == 4) {
            this.f22409e = 5;
            this.f22406b.k();
            return new UnknownLengthSource();
        }
        throw new IllegalStateException(("state: " + this.f22409e).toString());
    }

    @Override // okhttp3.internal.http.ExchangeCodec
    public final void cancel() {
        Socket socket = this.f22406b.f22338c;
        if (socket != null) {
            Util.d(socket);
        }
    }

    @Override // okhttp3.internal.http.ExchangeCodec
    public final Response.Builder d(boolean z6) {
        HeadersReader headersReader = this.f22410f;
        int i6 = this.f22409e;
        if (i6 != 1 && i6 != 2 && i6 != 3) {
            throw new IllegalStateException(("state: " + this.f22409e).toString());
        }
        try {
            StatusLine.Companion companion = StatusLine.f22398d;
            String strZ = headersReader.f22402a.z(headersReader.f22403b);
            headersReader.f22403b -= (long) strZ.length();
            companion.getClass();
            StatusLine statusLineA = StatusLine.Companion.a(strZ);
            int i7 = statusLineA.f22400b;
            Response.Builder builder = new Response.Builder();
            Protocol protocol = statusLineA.f22399a;
            o.h(protocol, "protocol");
            builder.f22154b = protocol;
            builder.f22155c = i7;
            String message = statusLineA.f22401c;
            o.h(message, "message");
            builder.f22156d = message;
            Headers.Builder builder2 = new Headers.Builder();
            while (true) {
                String strZ2 = headersReader.f22402a.z(headersReader.f22403b);
                headersReader.f22403b -= (long) strZ2.length();
                if (strZ2.length() == 0) {
                    break;
                }
                builder2.b(strZ2);
            }
            builder.c(builder2.e());
            if (z6 && i7 == 100) {
                return null;
            }
            if (i7 == 100) {
                this.f22409e = 3;
                return builder;
            }
            if (102 > i7 || i7 >= 200) {
                this.f22409e = 4;
                return builder;
            }
            this.f22409e = 3;
            return builder;
        } catch (EOFException e6) {
            throw new IOException("unexpected end of stream on ".concat(this.f22406b.f22337b.f22168a.f21896h.f()), e6);
        }
    }

    @Override // okhttp3.internal.http.ExchangeCodec
    public final RealConnection e() {
        return this.f22406b;
    }

    @Override // okhttp3.internal.http.ExchangeCodec
    public final void f() {
        this.f22408d.flush();
    }

    @Override // okhttp3.internal.http.ExchangeCodec
    public final long g(Response response) {
        if (!HttpHeaders.a(response)) {
            return 0L;
        }
        if ("chunked".equalsIgnoreCase(Response.b("Transfer-Encoding", response))) {
            return -1L;
        }
        return Util.j(response);
    }

    @Override // okhttp3.internal.http.ExchangeCodec
    public final N h(Request request, long j4) {
        o.h(request, "request");
        if ("chunked".equalsIgnoreCase(request.f22127c.g("Transfer-Encoding"))) {
            if (this.f22409e == 1) {
                this.f22409e = 2;
                return new ChunkedSink();
            }
            throw new IllegalStateException(("state: " + this.f22409e).toString());
        }
        if (j4 == -1) {
            throw new IllegalStateException("Cannot stream a request body without chunked encoding or a known content length!");
        }
        if (this.f22409e == 1) {
            this.f22409e = 2;
            return new KnownLengthSink();
        }
        throw new IllegalStateException(("state: " + this.f22409e).toString());
    }

    public final P i(long j4) {
        if (this.f22409e == 4) {
            this.f22409e = 5;
            return new FixedLengthSource(j4);
        }
        throw new IllegalStateException(("state: " + this.f22409e).toString());
    }

    public final void j(Response response) {
        long j4 = Util.j(response);
        if (j4 == -1) {
            return;
        }
        P pI = i(j4);
        TimeUnit timeUnit = TimeUnit.MILLISECONDS;
        Util.v(pI, Integer.MAX_VALUE);
        ((FixedLengthSource) pI).close();
    }

    public final void k(Headers headers, String requestLine) {
        o.h(requestLine, "requestLine");
        if (this.f22409e != 0) {
            throw new IllegalStateException(("state: " + this.f22409e).toString());
        }
        I i6 = this.f22408d;
        i6.n(requestLine);
        i6.n("\r\n");
        int size = headers.size();
        for (int i7 = 0; i7 < size; i7++) {
            i6.n(headers.h(i7));
            i6.n(": ");
            i6.n(headers.l(i7));
            i6.n("\r\n");
        }
        i6.n("\r\n");
        this.f22409e = 1;
    }
}
