package okhttp3.n0.http1;

import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.rtsp.RtspHeaders;
import java.io.EOFException;
import java.io.IOException;
import java.net.ProtocolException;
import java.net.Proxy;
import java.net.Socket;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import kotlin.Metadata;
import kotlin.jvm.internal.o;
import kotlin.text.j;
import okhttp3.CookieJar;
import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.n0.connection.RealConnection;
import okhttp3.n0.http.ExchangeCodec;
import okhttp3.n0.http.StatusLine;
import okio.Buffer;
import okio.BufferedSink;
import okio.BufferedSource;
import okio.ForwardingTimeout;
import okio.Sink;
import okio.Source;
import okio.Timeout;

/* JADX INFO: renamed from: y.n0.i.b, reason: from Kotlin metadata */
/* JADX INFO: loaded from: classes2.dex */
@Metadata(d1 = {"\u0000\u0088\u0001\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0004\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u000b\n\u0002\b\u0002\n\u0002\u0010\b\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\t\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0004\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0007\n\u0002\u0018\u0002\n\u0002\b\u0006\n\u0002\u0010\u000e\n\u0002\b\t\u0018\u0000 ?2\u00020\u0001:\u0007<=>?@ABB'\u0012\b\u0010\u0002\u001a\u0004\u0018\u00010\u0003\u0012\u0006\u0010\u0004\u001a\u00020\u0005\u0012\u0006\u0010\u0006\u001a\u00020\u0007\u0012\u0006\u0010\b\u001a\u00020\t¢\u0006\u0002\u0010\nJ\b\u0010\u001b\u001a\u00020\u001cH\u0016J\u0018\u0010\u001d\u001a\u00020\u001e2\u0006\u0010\u001f\u001a\u00020\u00172\u0006\u0010 \u001a\u00020!H\u0016J\u0010\u0010\"\u001a\u00020\u001c2\u0006\u0010#\u001a\u00020$H\u0002J\b\u0010%\u001a\u00020\u001cH\u0016J\b\u0010&\u001a\u00020\u001cH\u0016J\b\u0010'\u001a\u00020\u001eH\u0002J\u0010\u0010(\u001a\u00020)2\u0006\u0010*\u001a\u00020+H\u0002J\u0010\u0010,\u001a\u00020)2\u0006\u0010-\u001a\u00020!H\u0002J\b\u0010.\u001a\u00020\u001eH\u0002J\b\u0010/\u001a\u00020)H\u0002J\u0010\u00100\u001a\u00020)2\u0006\u00101\u001a\u00020\u0019H\u0016J\u0012\u00102\u001a\u0004\u0018\u0001032\u0006\u00104\u001a\u00020\u0010H\u0016J\u0010\u00105\u001a\u00020!2\u0006\u00101\u001a\u00020\u0019H\u0016J\u000e\u00106\u001a\u00020\u001c2\u0006\u00101\u001a\u00020\u0019J\b\u0010\u0014\u001a\u00020\u0015H\u0016J\u0016\u00107\u001a\u00020\u001c2\u0006\u00108\u001a\u00020\u00152\u0006\u00109\u001a\u00020:J\u0010\u0010;\u001a\u00020\u001c2\u0006\u0010\u001f\u001a\u00020\u0017H\u0016R\u0010\u0010\u0002\u001a\u0004\u0018\u00010\u0003X\u0082\u0004¢\u0006\u0002\n\u0000R\u0014\u0010\u0004\u001a\u00020\u0005X\u0096\u0004¢\u0006\b\n\u0000\u001a\u0004\b\u000b\u0010\fR\u000e\u0010\r\u001a\u00020\u000eX\u0082\u0004¢\u0006\u0002\n\u0000R\u0011\u0010\u000f\u001a\u00020\u00108F¢\u0006\u0006\u001a\u0004\b\u000f\u0010\u0011R\u000e\u0010\b\u001a\u00020\tX\u0082\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\u0006\u001a\u00020\u0007X\u0082\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\u0012\u001a\u00020\u0013X\u0082\u000e¢\u0006\u0002\n\u0000R\u0010\u0010\u0014\u001a\u0004\u0018\u00010\u0015X\u0082\u000e¢\u0006\u0002\n\u0000R\u0018\u0010\u0016\u001a\u00020\u0010*\u00020\u00178BX\u0082\u0004¢\u0006\u0006\u001a\u0004\b\u0016\u0010\u0018R\u0018\u0010\u0016\u001a\u00020\u0010*\u00020\u00198BX\u0082\u0004¢\u0006\u0006\u001a\u0004\b\u0016\u0010\u001a¨\u0006C"}, d2 = {"Lokhttp3/internal/http1/Http1ExchangeCodec;", "Lokhttp3/internal/http/ExchangeCodec;", "client", "Lokhttp3/OkHttpClient;", "connection", "Lokhttp3/internal/connection/RealConnection;", "source", "Lokio/BufferedSource;", "sink", "Lokio/BufferedSink;", "(Lokhttp3/OkHttpClient;Lokhttp3/internal/connection/RealConnection;Lokio/BufferedSource;Lokio/BufferedSink;)V", "getConnection", "()Lokhttp3/internal/connection/RealConnection;", "headersReader", "Lokhttp3/internal/http1/HeadersReader;", "isClosed", "", "()Z", "state", "", HttpHeaders.Values.TRAILERS, "Lokhttp3/Headers;", "isChunked", "Lokhttp3/Request;", "(Lokhttp3/Request;)Z", "Lokhttp3/Response;", "(Lokhttp3/Response;)Z", "cancel", "", "createRequestBody", "Lokio/Sink;", "request", "contentLength", "", "detachTimeout", RtspHeaders.Values.TIMEOUT, "Lokio/ForwardingTimeout;", "finishRequest", "flushRequest", "newChunkedSink", "newChunkedSource", "Lokio/Source;", RtspHeaders.Values.URL, "Lokhttp3/HttpUrl;", "newFixedLengthSource", "length", "newKnownLengthSink", "newUnknownLengthSource", "openResponseBodySource", "response", "readResponseHeaders", "Lokhttp3/Response$Builder;", "expectContinue", "reportedContentLength", "skipConnectBody", "writeRequest", "headers", "requestLine", "", "writeRequestHeaders", "AbstractSource", "ChunkedSink", "ChunkedSource", "Companion", "FixedLengthSource", "KnownLengthSink", "UnknownLengthSource", "okhttp"}, k = 1, mv = {1, 6, 0}, xi = 48)
public final class Http1ExchangeCodec implements ExchangeCodec {
    public final OkHttpClient a;

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

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

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

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

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

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

    /* JADX INFO: renamed from: y.n0.i.b$a */
    @Metadata(d1 = {"\u00004\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\u000b\n\u0002\b\u0005\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0010\t\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\u0002\n\u0002\u0018\u0002\n\u0000\b¢\u0004\u0018\u00002\u00020\u0001B\u0005¢\u0006\u0002\u0010\u0002J\u0018\u0010\r\u001a\u00020\u000e2\u0006\u0010\u000f\u001a\u00020\u00102\u0006\u0010\u0011\u001a\u00020\u000eH\u0016J\u0006\u0010\u0012\u001a\u00020\u0013J\b\u0010\t\u001a\u00020\u0014H\u0016R\u001a\u0010\u0003\u001a\u00020\u0004X\u0084\u000e¢\u0006\u000e\n\u0000\u001a\u0004\b\u0005\u0010\u0006\"\u0004\b\u0007\u0010\bR\u0014\u0010\t\u001a\u00020\nX\u0084\u0004¢\u0006\b\n\u0000\u001a\u0004\b\u000b\u0010\f¨\u0006\u0015"}, d2 = {"Lokhttp3/internal/http1/Http1ExchangeCodec$AbstractSource;", "Lokio/Source;", "(Lokhttp3/internal/http1/Http1ExchangeCodec;)V", "closed", "", "getClosed", "()Z", "setClosed", "(Z)V", RtspHeaders.Values.TIMEOUT, "Lokio/ForwardingTimeout;", "getTimeout", "()Lokio/ForwardingTimeout;", "read", "", "sink", "Lokio/Buffer;", "byteCount", "responseBodyComplete", "", "Lokio/Timeout;", "okhttp"}, k = 1, mv = {1, 6, 0}, xi = 48)
    public abstract class a implements Source {

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

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

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

        public a(Http1ExchangeCodec http1ExchangeCodec) {
            o.f(http1ExchangeCodec, "this$0");
            this.f9848n = http1ExchangeCodec;
            this.f9846f = new ForwardingTimeout(http1ExchangeCodec.f9841c.getF10138m());
        }

        public final void a() {
            Http1ExchangeCodec http1ExchangeCodec = this.f9848n;
            int i2 = http1ExchangeCodec.f9843e;
            if (i2 == 6) {
                return;
            }
            if (i2 != 5) {
                throw new IllegalStateException(o.l("state: ", Integer.valueOf(this.f9848n.f9843e)));
            }
            Http1ExchangeCodec.i(http1ExchangeCodec, this.f9846f);
            this.f9848n.f9843e = 6;
        }

        @Override // okio.Source
        public long r(Buffer buffer, long j2) throws IOException {
            o.f(buffer, "sink");
            try {
                return this.f9848n.f9841c.r(buffer, j2);
            } catch (IOException e2) {
                this.f9848n.f9840b.l();
                a();
                throw e2;
            }
        }

        @Override // okio.Source
        /* JADX INFO: renamed from: timeout */
        public Timeout getF10138m() {
            return this.f9846f;
        }
    }

    /* JADX INFO: renamed from: y.n0.i.b$b */
    @Metadata(d1 = {"\u00002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\u000b\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\t\n\u0000\b\u0082\u0004\u0018\u00002\u00020\u0001B\u0005¢\u0006\u0002\u0010\u0002J\b\u0010\u0007\u001a\u00020\bH\u0016J\b\u0010\t\u001a\u00020\bH\u0016J\b\u0010\u0005\u001a\u00020\nH\u0016J\u0018\u0010\u000b\u001a\u00020\b2\u0006\u0010\f\u001a\u00020\r2\u0006\u0010\u000e\u001a\u00020\u000fH\u0016R\u000e\u0010\u0003\u001a\u00020\u0004X\u0082\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\u0005\u001a\u00020\u0006X\u0082\u0004¢\u0006\u0002\n\u0000¨\u0006\u0010"}, d2 = {"Lokhttp3/internal/http1/Http1ExchangeCodec$ChunkedSink;", "Lokio/Sink;", "(Lokhttp3/internal/http1/Http1ExchangeCodec;)V", "closed", "", RtspHeaders.Values.TIMEOUT, "Lokio/ForwardingTimeout;", "close", "", "flush", "Lokio/Timeout;", "write", "source", "Lokio/Buffer;", "byteCount", "", "okhttp"}, k = 1, mv = {1, 6, 0}, xi = 48)
    public final class b implements Sink {

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

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

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

        public b(Http1ExchangeCodec http1ExchangeCodec) {
            o.f(http1ExchangeCodec, "this$0");
            this.f9851n = http1ExchangeCodec;
            this.f9849f = new ForwardingTimeout(http1ExchangeCodec.f9842d.getF10143m());
        }

        @Override // okio.Sink, java.io.Closeable, java.lang.AutoCloseable, java.nio.channels.Channel
        public synchronized void close() {
            if (this.f9850m) {
                return;
            }
            this.f9850m = true;
            this.f9851n.f9842d.writeUtf8("0\r\n\r\n");
            Http1ExchangeCodec.i(this.f9851n, this.f9849f);
            this.f9851n.f9843e = 3;
        }

        @Override // okio.Sink, java.io.Flushable
        public synchronized void flush() {
            if (this.f9850m) {
                return;
            }
            this.f9851n.f9842d.flush();
        }

        @Override // okio.Sink
        /* JADX INFO: renamed from: timeout */
        public Timeout getF10143m() {
            return this.f9849f;
        }

        @Override // okio.Sink
        public void write(Buffer buffer, long j2) {
            o.f(buffer, "source");
            if (!(!this.f9850m)) {
                throw new IllegalStateException("closed".toString());
            }
            if (j2 == 0) {
                return;
            }
            this.f9851n.f9842d.writeHexadecimalUnsignedLong(j2);
            this.f9851n.f9842d.writeUtf8("\r\n");
            this.f9851n.f9842d.write(buffer, j2);
            this.f9851n.f9842d.writeUtf8("\r\n");
        }
    }

    /* JADX INFO: renamed from: y.n0.i.b$c */
    @Metadata(d1 = {"\u00002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\t\n\u0000\n\u0002\u0010\u000b\n\u0000\n\u0002\u0010\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0003\b\u0082\u0004\u0018\u00002\u00060\u0001R\u00020\u0002B\r\u0012\u0006\u0010\u0003\u001a\u00020\u0004¢\u0006\u0002\u0010\u0005J\b\u0010\n\u001a\u00020\u000bH\u0016J\u0018\u0010\f\u001a\u00020\u00072\u0006\u0010\r\u001a\u00020\u000e2\u0006\u0010\u000f\u001a\u00020\u0007H\u0016J\b\u0010\u0010\u001a\u00020\u000bH\u0002R\u000e\u0010\u0006\u001a\u00020\u0007X\u0082\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\b\u001a\u00020\tX\u0082\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\u0003\u001a\u00020\u0004X\u0082\u0004¢\u0006\u0002\n\u0000¨\u0006\u0011"}, d2 = {"Lokhttp3/internal/http1/Http1ExchangeCodec$ChunkedSource;", "Lokhttp3/internal/http1/Http1ExchangeCodec$AbstractSource;", "Lokhttp3/internal/http1/Http1ExchangeCodec;", RtspHeaders.Values.URL, "Lokhttp3/HttpUrl;", "(Lokhttp3/internal/http1/Http1ExchangeCodec;Lokhttp3/HttpUrl;)V", "bytesRemainingInChunk", "", "hasMoreChunks", "", "close", "", "read", "sink", "Lokio/Buffer;", "byteCount", "readChunkSize", "okhttp"}, k = 1, mv = {1, 6, 0}, xi = 48)
    public final class c extends a {

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

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

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

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

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public c(Http1ExchangeCodec http1ExchangeCodec, HttpUrl httpUrl) {
            super(http1ExchangeCodec);
            o.f(http1ExchangeCodec, "this$0");
            o.f(httpUrl, RtspHeaders.Values.URL);
            this.f9855r = http1ExchangeCodec;
            this.f9852o = httpUrl;
            this.f9853p = -1L;
            this.f9854q = true;
        }

        @Override // okio.Source, java.io.Closeable, java.lang.AutoCloseable
        public void close() {
            if (this.f9847m) {
                return;
            }
            if (this.f9854q && !okhttp3.n0.c.i(this, 100, TimeUnit.MILLISECONDS)) {
                this.f9855r.f9840b.l();
                a();
            }
            this.f9847m = true;
        }

        @Override // okhttp3.n0.http1.Http1ExchangeCodec.a, okio.Source
        public long r(Buffer buffer, long j2) throws IOException {
            o.f(buffer, "sink");
            boolean z2 = true;
            if (!(j2 >= 0)) {
                throw new IllegalArgumentException(o.l("byteCount < 0: ", Long.valueOf(j2)).toString());
            }
            if (!(!this.f9847m)) {
                throw new IllegalStateException("closed".toString());
            }
            if (!this.f9854q) {
                return -1L;
            }
            long j3 = this.f9853p;
            if (j3 == 0 || j3 == -1) {
                if (j3 != -1) {
                    this.f9855r.f9841c.n();
                }
                try {
                    this.f9853p = this.f9855r.f9841c.v();
                    String string = j.Y(this.f9855r.f9841c.n()).toString();
                    if (this.f9853p >= 0) {
                        if (string.length() <= 0) {
                            z2 = false;
                        }
                        if (!z2 || j.M(string, ";", false, 2)) {
                            if (this.f9853p == 0) {
                                this.f9854q = false;
                                Http1ExchangeCodec http1ExchangeCodec = this.f9855r;
                                http1ExchangeCodec.f9845g = http1ExchangeCodec.f9844f.a();
                                OkHttpClient okHttpClient = this.f9855r.a;
                                o.c(okHttpClient);
                                CookieJar cookieJar = okHttpClient.f9572x;
                                HttpUrl httpUrl = this.f9852o;
                                Headers headers = this.f9855r.f9845g;
                                o.c(headers);
                                okhttp3.n0.http.e.d(cookieJar, httpUrl, headers);
                                a();
                            }
                            if (!this.f9854q) {
                                return -1L;
                            }
                        }
                    }
                    throw new ProtocolException("expected chunk size and optional extensions but was \"" + this.f9853p + string + '\"');
                } catch (NumberFormatException e2) {
                    throw new ProtocolException(e2.getMessage());
                }
            }
            long jR = super.r(buffer, Math.min(j2, this.f9853p));
            if (jR != -1) {
                this.f9853p -= jR;
                return jR;
            }
            this.f9855r.f9840b.l();
            ProtocolException protocolException = new ProtocolException("unexpected end of stream");
            a();
            throw protocolException;
        }
    }

    /* JADX INFO: renamed from: y.n0.i.b$d */
    @Metadata(d1 = {"\u0000&\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\t\n\u0002\b\u0002\n\u0002\u0010\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\b\u0082\u0004\u0018\u00002\u00060\u0001R\u00020\u0002B\r\u0012\u0006\u0010\u0003\u001a\u00020\u0004¢\u0006\u0002\u0010\u0005J\b\u0010\u0006\u001a\u00020\u0007H\u0016J\u0018\u0010\b\u001a\u00020\u00042\u0006\u0010\t\u001a\u00020\n2\u0006\u0010\u000b\u001a\u00020\u0004H\u0016R\u000e\u0010\u0003\u001a\u00020\u0004X\u0082\u000e¢\u0006\u0002\n\u0000¨\u0006\f"}, d2 = {"Lokhttp3/internal/http1/Http1ExchangeCodec$FixedLengthSource;", "Lokhttp3/internal/http1/Http1ExchangeCodec$AbstractSource;", "Lokhttp3/internal/http1/Http1ExchangeCodec;", "bytesRemaining", "", "(Lokhttp3/internal/http1/Http1ExchangeCodec;J)V", "close", "", "read", "sink", "Lokio/Buffer;", "byteCount", "okhttp"}, k = 1, mv = {1, 6, 0}, xi = 48)
    public final class d extends a {

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

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

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public d(Http1ExchangeCodec http1ExchangeCodec, long j2) {
            super(http1ExchangeCodec);
            o.f(http1ExchangeCodec, "this$0");
            this.f9857p = http1ExchangeCodec;
            this.f9856o = j2;
            if (j2 == 0) {
                a();
            }
        }

        @Override // okio.Source, java.io.Closeable, java.lang.AutoCloseable
        public void close() {
            if (this.f9847m) {
                return;
            }
            if (this.f9856o != 0 && !okhttp3.n0.c.i(this, 100, TimeUnit.MILLISECONDS)) {
                this.f9857p.f9840b.l();
                a();
            }
            this.f9847m = true;
        }

        @Override // okhttp3.n0.http1.Http1ExchangeCodec.a, okio.Source
        public long r(Buffer buffer, long j2) throws IOException {
            o.f(buffer, "sink");
            if (!(j2 >= 0)) {
                throw new IllegalArgumentException(o.l("byteCount < 0: ", Long.valueOf(j2)).toString());
            }
            if (!(!this.f9847m)) {
                throw new IllegalStateException("closed".toString());
            }
            long j3 = this.f9856o;
            if (j3 == 0) {
                return -1L;
            }
            long jR = super.r(buffer, Math.min(j3, j2));
            if (jR == -1) {
                this.f9857p.f9840b.l();
                ProtocolException protocolException = new ProtocolException("unexpected end of stream");
                a();
                throw protocolException;
            }
            long j4 = this.f9856o - jR;
            this.f9856o = j4;
            if (j4 == 0) {
                a();
            }
            return jR;
        }
    }

    /* JADX INFO: renamed from: y.n0.i.b$e */
    @Metadata(d1 = {"\u00002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\u000b\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\t\n\u0000\b\u0082\u0004\u0018\u00002\u00020\u0001B\u0005¢\u0006\u0002\u0010\u0002J\b\u0010\u0007\u001a\u00020\bH\u0016J\b\u0010\t\u001a\u00020\bH\u0016J\b\u0010\u0005\u001a\u00020\nH\u0016J\u0018\u0010\u000b\u001a\u00020\b2\u0006\u0010\f\u001a\u00020\r2\u0006\u0010\u000e\u001a\u00020\u000fH\u0016R\u000e\u0010\u0003\u001a\u00020\u0004X\u0082\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\u0005\u001a\u00020\u0006X\u0082\u0004¢\u0006\u0002\n\u0000¨\u0006\u0010"}, d2 = {"Lokhttp3/internal/http1/Http1ExchangeCodec$KnownLengthSink;", "Lokio/Sink;", "(Lokhttp3/internal/http1/Http1ExchangeCodec;)V", "closed", "", RtspHeaders.Values.TIMEOUT, "Lokio/ForwardingTimeout;", "close", "", "flush", "Lokio/Timeout;", "write", "source", "Lokio/Buffer;", "byteCount", "", "okhttp"}, k = 1, mv = {1, 6, 0}, xi = 48)
    public final class e implements Sink {

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

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

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

        public e(Http1ExchangeCodec http1ExchangeCodec) {
            o.f(http1ExchangeCodec, "this$0");
            this.f9860n = http1ExchangeCodec;
            this.f9858f = new ForwardingTimeout(http1ExchangeCodec.f9842d.getF10143m());
        }

        @Override // okio.Sink, java.io.Closeable, java.lang.AutoCloseable, java.nio.channels.Channel
        public void close() {
            if (this.f9859m) {
                return;
            }
            this.f9859m = true;
            Http1ExchangeCodec.i(this.f9860n, this.f9858f);
            this.f9860n.f9843e = 3;
        }

        @Override // okio.Sink, java.io.Flushable
        public void flush() {
            if (this.f9859m) {
                return;
            }
            this.f9860n.f9842d.flush();
        }

        @Override // okio.Sink
        /* JADX INFO: renamed from: timeout */
        public Timeout getF10143m() {
            return this.f9858f;
        }

        @Override // okio.Sink
        public void write(Buffer buffer, long j2) {
            o.f(buffer, "source");
            if (!(!this.f9859m)) {
                throw new IllegalStateException("closed".toString());
            }
            okhttp3.n0.c.d(buffer.f10118m, 0L, j2);
            this.f9860n.f9842d.write(buffer, j2);
        }
    }

    /* JADX INFO: renamed from: y.n0.i.b$f */
    @Metadata(d1 = {"\u0000*\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\u000b\n\u0000\n\u0002\u0010\u0002\n\u0000\n\u0002\u0010\t\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\b\u0082\u0004\u0018\u00002\u00060\u0001R\u00020\u0002B\u0005¢\u0006\u0002\u0010\u0003J\b\u0010\u0006\u001a\u00020\u0007H\u0016J\u0018\u0010\b\u001a\u00020\t2\u0006\u0010\n\u001a\u00020\u000b2\u0006\u0010\f\u001a\u00020\tH\u0016R\u000e\u0010\u0004\u001a\u00020\u0005X\u0082\u000e¢\u0006\u0002\n\u0000¨\u0006\r"}, d2 = {"Lokhttp3/internal/http1/Http1ExchangeCodec$UnknownLengthSource;", "Lokhttp3/internal/http1/Http1ExchangeCodec$AbstractSource;", "Lokhttp3/internal/http1/Http1ExchangeCodec;", "(Lokhttp3/internal/http1/Http1ExchangeCodec;)V", "inputExhausted", "", "close", "", "read", "", "sink", "Lokio/Buffer;", "byteCount", "okhttp"}, k = 1, mv = {1, 6, 0}, xi = 48)
    public final class f extends a {

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

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public f(Http1ExchangeCodec http1ExchangeCodec) {
            super(http1ExchangeCodec);
            o.f(http1ExchangeCodec, "this$0");
        }

        @Override // okio.Source, java.io.Closeable, java.lang.AutoCloseable
        public void close() {
            if (this.f9847m) {
                return;
            }
            if (!this.f9861o) {
                a();
            }
            this.f9847m = true;
        }

        @Override // okhttp3.n0.http1.Http1ExchangeCodec.a, okio.Source
        public long r(Buffer buffer, long j2) throws IOException {
            o.f(buffer, "sink");
            if (!(j2 >= 0)) {
                throw new IllegalArgumentException(o.l("byteCount < 0: ", Long.valueOf(j2)).toString());
            }
            if (!(!this.f9847m)) {
                throw new IllegalStateException("closed".toString());
            }
            if (this.f9861o) {
                return -1L;
            }
            long jR = super.r(buffer, j2);
            if (jR != -1) {
                return jR;
            }
            this.f9861o = true;
            a();
            return -1L;
        }
    }

    public Http1ExchangeCodec(OkHttpClient okHttpClient, RealConnection realConnection, BufferedSource bufferedSource, BufferedSink bufferedSink) {
        o.f(realConnection, "connection");
        o.f(bufferedSource, "source");
        o.f(bufferedSink, "sink");
        this.a = okHttpClient;
        this.f9840b = realConnection;
        this.f9841c = bufferedSource;
        this.f9842d = bufferedSink;
        this.f9844f = new HeadersReader(bufferedSource);
    }

    public static final void i(Http1ExchangeCodec http1ExchangeCodec, ForwardingTimeout forwardingTimeout) {
        Objects.requireNonNull(http1ExchangeCodec);
        Timeout timeout = forwardingTimeout.f10127e;
        Timeout timeout2 = Timeout.a;
        o.f(timeout2, "delegate");
        forwardingTimeout.f10127e = timeout2;
        timeout.a();
        timeout.b();
    }

    @Override // okhttp3.n0.http.ExchangeCodec
    public void a() {
        this.f9842d.flush();
    }

    @Override // okhttp3.n0.http.ExchangeCodec
    public void b(Request request) {
        o.f(request, "request");
        Proxy.Type type = this.f9840b.f9788b.f9690b.type();
        o.e(type, "connection.route().proxy.type()");
        o.f(request, "request");
        o.f(type, "proxyType");
        StringBuilder sb = new StringBuilder();
        sb.append(request.f9621b);
        sb.append(' ');
        HttpUrl httpUrl = request.a;
        if (!httpUrl.f10097l && type == Proxy.Type.HTTP) {
            sb.append(httpUrl);
        } else {
            o.f(httpUrl, RtspHeaders.Values.URL);
            String strB = httpUrl.b();
            String strD = httpUrl.d();
            if (strD != null) {
                strB = strB + '?' + ((Object) strD);
            }
            sb.append(strB);
        }
        sb.append(" HTTP/1.1");
        String string = sb.toString();
        o.e(string, "StringBuilder().apply(builderAction).toString()");
        k(request.f9622c, string);
    }

    @Override // okhttp3.n0.http.ExchangeCodec
    public Source c(Response response) {
        o.f(response, "response");
        if (!okhttp3.n0.http.e.a(response)) {
            return j(0L);
        }
        if (j.g(HttpHeaders.Values.CHUNKED, Response.a(response, HttpHeaders.Names.TRANSFER_ENCODING, null, 2), true)) {
            HttpUrl httpUrl = response.f9641f.a;
            int i2 = this.f9843e;
            if (!(i2 == 4)) {
                throw new IllegalStateException(o.l("state: ", Integer.valueOf(i2)).toString());
            }
            this.f9843e = 5;
            return new c(this, httpUrl);
        }
        long jL = okhttp3.n0.c.l(response);
        if (jL != -1) {
            return j(jL);
        }
        int i3 = this.f9843e;
        if (!(i3 == 4)) {
            throw new IllegalStateException(o.l("state: ", Integer.valueOf(i3)).toString());
        }
        this.f9843e = 5;
        this.f9840b.l();
        return new f(this);
    }

    @Override // okhttp3.n0.http.ExchangeCodec
    public void cancel() {
        Socket socket = this.f9840b.f9789c;
        if (socket == null) {
            return;
        }
        okhttp3.n0.c.f(socket);
    }

    @Override // okhttp3.n0.http.ExchangeCodec
    public Response.a d(boolean z2) {
        int i2 = this.f9843e;
        boolean z3 = false;
        if (!(i2 == 1 || i2 == 2 || i2 == 3)) {
            throw new IllegalStateException(o.l("state: ", Integer.valueOf(i2)).toString());
        }
        try {
            StatusLine statusLineA = StatusLine.a(this.f9844f.b());
            Response.a aVar = new Response.a();
            aVar.g(statusLineA.a);
            aVar.f9655c = statusLineA.f9837b;
            aVar.f(statusLineA.f9838c);
            aVar.e(this.f9844f.a());
            if (z2 && statusLineA.f9837b == 100) {
                return null;
            }
            int i3 = statusLineA.f9837b;
            if (i3 != 100) {
                if (102 <= i3 && i3 < 200) {
                    z3 = true;
                }
                if (!z3) {
                    this.f9843e = 4;
                    return aVar;
                }
            }
            this.f9843e = 3;
            return aVar;
        } catch (EOFException e2) {
            throw new IOException(o.l("unexpected end of stream on ", this.f9840b.f9788b.a.f9541i.f()), e2);
        }
    }

    @Override // okhttp3.n0.http.ExchangeCodec
    /* JADX INFO: renamed from: e, reason: from getter */
    public RealConnection getF9967c() {
        return this.f9840b;
    }

    @Override // okhttp3.n0.http.ExchangeCodec
    public void f() {
        this.f9842d.flush();
    }

    @Override // okhttp3.n0.http.ExchangeCodec
    public long g(Response response) {
        o.f(response, "response");
        if (!okhttp3.n0.http.e.a(response)) {
            return 0L;
        }
        if (j.g(HttpHeaders.Values.CHUNKED, Response.a(response, HttpHeaders.Names.TRANSFER_ENCODING, null, 2), true)) {
            return -1L;
        }
        return okhttp3.n0.c.l(response);
    }

    @Override // okhttp3.n0.http.ExchangeCodec
    public Sink h(Request request, long j2) throws ProtocolException {
        o.f(request, "request");
        RequestBody requestBody = request.f9623d;
        if (requestBody != null && requestBody.isDuplex()) {
            throw new ProtocolException("Duplex connections are not supported for HTTP/1");
        }
        if (j.g(HttpHeaders.Values.CHUNKED, request.b(HttpHeaders.Names.TRANSFER_ENCODING), true)) {
            int i2 = this.f9843e;
            if (!(i2 == 1)) {
                throw new IllegalStateException(o.l("state: ", Integer.valueOf(i2)).toString());
            }
            this.f9843e = 2;
            return new b(this);
        }
        if (j2 == -1) {
            throw new IllegalStateException("Cannot stream a request body without chunked encoding or a known content length!");
        }
        int i3 = this.f9843e;
        if (!(i3 == 1)) {
            throw new IllegalStateException(o.l("state: ", Integer.valueOf(i3)).toString());
        }
        this.f9843e = 2;
        return new e(this);
    }

    public final Source j(long j2) {
        int i2 = this.f9843e;
        if (!(i2 == 4)) {
            throw new IllegalStateException(o.l("state: ", Integer.valueOf(i2)).toString());
        }
        this.f9843e = 5;
        return new d(this, j2);
    }

    public final void k(Headers headers, String str) {
        o.f(headers, "headers");
        o.f(str, "requestLine");
        int i2 = this.f9843e;
        if (!(i2 == 0)) {
            throw new IllegalStateException(o.l("state: ", Integer.valueOf(i2)).toString());
        }
        this.f9842d.writeUtf8(str).writeUtf8("\r\n");
        int size = headers.size();
        for (int i3 = 0; i3 < size; i3++) {
            this.f9842d.writeUtf8(headers.c(i3)).writeUtf8(": ").writeUtf8(headers.e(i3)).writeUtf8("\r\n");
        }
        this.f9842d.writeUtf8("\r\n");
        this.f9843e = 1;
    }
}
