content: package okhttp3.internal.http1;
  
  import java.io.EOFException;
  import java.io.IOException;
  import java.net.ProtocolException;
  import java.net.Proxy;
  import java.util.concurrent.TimeUnit;
  import kotlin.Metadata;
  import kotlin.jvm.internal.DefaultConstructorMarker;
  import kotlin.jvm.internal.Intrinsics;
  import kotlin.text.StringsKt;
  import kotlin.text.Typography;
  import mt.Log1F380D;
  import okhttp3.CookieJar;
  import okhttp3.Headers;
  import okhttp3.HttpUrl;
  import okhttp3.OkHttpClient;
  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 okio.Buffer;
  import okio.BufferedSink;
  import okio.BufferedSource;
  import okio.ForwardingTimeout;
  import okio.Sink;
  import okio.Source;
  import okio.Timeout;
  
  @Metadata(bv = {1, 0, 3}, d1 = {"\u0000\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\u0004¢\u0006\u0002\n\u0000R\u0014\u0010\u0004\u001a\u00020\u0005X\u0004¢\u0006\b\n\u0000\u001a\u0004\b\u000b\u0010\fR\u000e\u0010\r\u001a\u00020\u000eX\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\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\u0006\u001a\u00020\u0007X\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\u0012\u001a\u00020\u0013X\u000e¢\u0006\u0002\n\u0000R\u0010\u0010\u0014\u001a\u0004\u0018\u00010\u0015X\u000e¢\u0006\u0002\n\u0000R\u0018\u0010\u0016\u001a\u00020\u0010*\u00020\u00178BX\u0004¢\u0006\u0006\u001a\u0004\b\u0016\u0010\u0018R\u0018\u0010\u0016\u001a\u00020\u0010*\u00020\u00198BX\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", "", "trailers", "Lokhttp3/Headers;", "isChunked", "Lokhttp3/Request;", "(Lokhttp3/Request;)Z", "Lokhttp3/Response;", "(Lokhttp3/Response;)Z", "cancel", "", "createRequestBody", "Lokio/Sink;", "request", "contentLength", "", "detachTimeout", "timeout", "Lokio/ForwardingTimeout;", "finishRequest", "flushRequest", "newChunkedSink", "newChunkedSource", "Lokio/Source;", "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, 4, 0})
  /* compiled from: 01D2 */
  public final class Http1ExchangeCodec implements ExchangeCodec {
      public static final Companion Companion = new Companion((DefaultConstructorMarker) null);
      private static final long NO_CHUNK_YET = -1;
      private static final int STATE_CLOSED = 6;
      private static final int STATE_IDLE = 0;
      private static final int STATE_OPEN_REQUEST_BODY = 1;
      private static final int STATE_OPEN_RESPONSE_BODY = 4;
      private static final int STATE_READING_RESPONSE_BODY = 5;
      private static final int STATE_READ_RESPONSE_HEADERS = 3;
      private static final int STATE_WRITING_REQUEST_BODY = 2;
      /* access modifiers changed from: private */
      public final OkHttpClient client;
      private final RealConnection connection;
      /* access modifiers changed from: private */
      public final HeadersReader headersReader;
      /* access modifiers changed from: private */
      public final BufferedSink sink;
      /* access modifiers changed from: private */
      public final BufferedSource source;
      /* access modifiers changed from: private */
      public int state;
      /* access modifiers changed from: private */
      public Headers trailers;
  
      @Metadata(bv = {1, 0, 3}, 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\u000e¢\u0006\u000e\n\u0000\u001a\u0004\b\u0005\u0010\u0006\"\u0004\b\u0007\u0010\bR\u0014\u0010\t\u001a\u00020\nX\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", "timeout", "Lokio/ForwardingTimeout;", "getTimeout", "()Lokio/ForwardingTimeout;", "read", "", "sink", "Lokio/Buffer;", "byteCount", "responseBodyComplete", "", "Lokio/Timeout;", "okhttp"}, k = 1, mv = {1, 4, 0})
      /* compiled from: Http1ExchangeCodec.kt */
      private abstract class AbstractSource implements Source {
          private boolean closed;
          private final ForwardingTimeout timeout;
  
          public AbstractSource() {
              this.timeout = new ForwardingTimeout(Http1ExchangeCodec.this.source.timeout());
          }
  
          /* access modifiers changed from: protected */
          public final boolean getClosed() {
              return this.closed;
          }
  
          /* access modifiers changed from: protected */
          public final ForwardingTimeout getTimeout() {
              return this.timeout;
          }
  
          public long read(Buffer sink, long byteCount) {
              Intrinsics.checkNotNullParameter(sink, "sink");
              try {
                  return Http1ExchangeCodec.this.source.read(sink, byteCount);
              } catch (IOException e) {
                  Http1ExchangeCodec.this.getConnection().noNewExchanges$okhttp();
                  responseBodyComplete();
                  throw e;
              }
          }
  
          public final void responseBodyComplete() {
              if (Http1ExchangeCodec.this.state != 6) {
                  if (Http1ExchangeCodec.this.state == 5) {
                      Http1ExchangeCodec.this.detachTimeout(this.timeout);
                      Http1ExchangeCodec.this.state = 6;
                      return;
                  }
                  throw new IllegalStateException("state: " + Http1ExchangeCodec.this.state);
              }
          }
  
          /* access modifiers changed from: protected */
          public final void setClosed(boolean z) {
              this.closed = z;
          }
  
          public Timeout timeout() {
              return this.timeout;
          }
      }
  
      @Metadata(bv = {1, 0, 3}, 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\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\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\u0005\u001a\u00020\u0006X\u0004¢\u0006\u0002\n\u0000¨\u0006\u0010"}, d2 = {"Lokhttp3/internal/http1/Http1ExchangeCodec$ChunkedSink;", "Lokio/Sink;", "(Lokhttp3/internal/http1/Http1ExchangeCodec;)V", "closed", "", "timeout", "Lokio/ForwardingTimeout;", "close", "", "flush", "Lokio/Timeout;", "write", "source", "Lokio/Buffer;", "byteCount", "", "okhttp"}, k = 1, mv = {1, 4, 0})
      /* compiled from: Http1ExchangeCodec.kt */
      private final class ChunkedSink implements Sink {
          private boolean closed;
          private final ForwardingTimeout timeout;
  
          public ChunkedSink() {
              this.timeout = new ForwardingTimeout(Http1ExchangeCodec.this.sink.timeout());
          }
  
          public synchronized void close() {
              if (!this.closed) {
                  this.closed = true;
                  Http1ExchangeCodec.this.sink.writeUtf8("0\r\n\r\n");
                  Http1ExchangeCodec.this.detachTimeout(this.timeout);
                  Http1ExchangeCodec.this.state = 3;
              }
          }
  
          public synchronized void flush() {
              if (!this.closed) {
                  Http1ExchangeCodec.this.sink.flush();
              }
          }
  
          public Timeout timeout() {
              return this.timeout;
          }
  
          public void write(Buffer source, long byteCount) {
              Intrinsics.checkNotNullParameter(source, "source");
              if (!(!this.closed)) {
                  throw new IllegalStateException("closed".toString());
              } else if (byteCount != 0) {
                  Http1ExchangeCodec.this.sink.writeHexadecimalUnsignedLong(byteCount);
                  Http1ExchangeCodec.this.sink.writeUtf8("\r\n");
                  Http1ExchangeCodec.this.sink.write(source, byteCount);
                  Http1ExchangeCodec.this.sink.writeUtf8("\r\n");
              }
          }
      }
  
      @Metadata(bv = {1, 0, 3}, 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\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\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\b\u001a\u00020\tX\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\u0003\u001a\u00020\u0004X\u0004¢\u0006\u0002\n\u0000¨\u0006\u0011"}, d2 = {"Lokhttp3/internal/http1/Http1ExchangeCodec$ChunkedSource;", "Lokhttp3/internal/http1/Http1ExchangeCodec$AbstractSource;", "Lokhttp3/internal/http1/Http1ExchangeCodec;", "url", "Lokhttp3/HttpUrl;", "(Lokhttp3/internal/http1/Http1ExchangeCodec;Lokhttp3/HttpUrl;)V", "bytesRemainingInChunk", "", "hasMoreChunks", "", "close", "", "read", "sink", "Lokio/Buffer;", "byteCount", "readChunkSize", "okhttp"}, k = 1, mv = {1, 4, 0})
      /* compiled from: Http1ExchangeCodec.kt */
      private final class ChunkedSource extends AbstractSource {
          private long bytesRemainingInChunk = -1;
          private boolean hasMoreChunks = true;
          final /* synthetic */ Http1ExchangeCodec this$0;
          private final HttpUrl url;
  
          /* JADX INFO: super call moved to the top of the method (can break code semantics) */
          public ChunkedSource(Http1ExchangeCodec this$02, HttpUrl url2) {
              super();
              Intrinsics.checkNotNullParameter(url2, "url");
              this.this$0 = this$02;
              this.url = url2;
          }
  
          private final void readChunkSize() {
              if (this.bytesRemainingInChunk != -1) {
                  this.this$0.source.readUtf8LineStrict();
              }
              try {
                  this.bytesRemainingInChunk = this.this$0.source.readHexadecimalUnsignedLong();
                  String readUtf8LineStrict = this.this$0.source.readUtf8LineStrict();
                  if (readUtf8LineStrict != null) {
                      String obj = StringsKt.trim((CharSequence) readUtf8LineStrict).toString();
                      if (this.bytesRemainingInChunk >= 0) {
                          if (!(obj.length() > 0) || StringsKt.startsWith$default(obj, ";", false, 2, (Object) null)) {
                              if (this.bytesRemainingInChunk == 0) {
                                  this.hasMoreChunks = false;
                                  Http1ExchangeCodec http1ExchangeCodec = this.this$0;
                                  http1ExchangeCodec.trailers = http1ExchangeCodec.headersReader.readHeaders();
                                  OkHttpClient access$getClient$p = this.this$0.client;
                                  Intrinsics.checkNotNull(access$getClient$p);
                                  CookieJar cookieJar = access$getClient$p.cookieJar();
                                  HttpUrl httpUrl = this.url;
                                  Headers access$getTrailers$p = this.this$0.trailers;
                                  Intrinsics.checkNotNull(access$getTrailers$p);
                                  HttpHeaders.receiveHeaders(cookieJar, httpUrl, access$getTrailers$p);
                                  responseBodyComplete();
                                  return;
                              }
                              return;
                          }
                      }
                      throw new ProtocolException("expected chunk size and optional extensions" + " but was \"" + this.bytesRemainingInChunk + obj + Typography.quote);
                  }
                  throw new NullPointerException("null cannot be cast to non-null type kotlin.CharSequence");
              } catch (NumberFormatException e) {
                  throw new ProtocolException(e.getMessage());
              }
          }
  
          public void close() {
              if (!getClosed()) {
                  if (this.hasMoreChunks && !Util.discard(this, 100, TimeUnit.MILLISECONDS)) {
                      this.this$0.getConnection().noNewExchanges$okhttp();
                      responseBodyComplete();
                  }
                  setClosed(true);
              }
          }
  
          public long read(Buffer sink, long byteCount) {
              Intrinsics.checkNotNullParameter(sink, "sink");
              if (!(byteCount >= 0)) {
                  throw new IllegalArgumentException(("byteCount < 0: " + byteCount).toString());
              } else if (!(!getClosed())) {
                  throw new IllegalStateException("closed".toString());
              } else if (!this.hasMoreChunks) {
                  return -1;
              } else {
                  long j = this.bytesRemainingInChunk;
                  if (j == 0 || j == -1) {
                      readChunkSize();
                      if (!this.hasMoreChunks) {
                          return -1;
                      }
                  }
                  long read = super.read(sink, Math.min(byteCount, this.bytesRemainingInChunk));
                  if (read != -1) {
                      this.bytesRemainingInChunk -= read;
                      return read;
                  }
                  this.this$0.getConnection().noNewExchanges$okhttp();
                  ProtocolException protocolException = new ProtocolException("unexpected end of stream");
                  responseBodyComplete();
                  throw protocolException;
              }
          }
      }
  
      @Metadata(bv = {1, 0, 3}, d1 = {"\u0000\u001a\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0002\n\u0002\u0010\t\n\u0000\n\u0002\u0010\b\n\u0002\b\u0007\b\u0003\u0018\u00002\u00020\u0001B\u0007\b\u0002¢\u0006\u0002\u0010\u0002R\u000e\u0010\u0003\u001a\u00020\u0004XT¢\u0006\u0002\n\u0000R\u000e\u0010\u0005\u001a\u00020\u0006XT¢\u0006\u0002\n\u0000R\u000e\u0010\u0007\u001a\u00020\u0006XT¢\u0006\u0002\n\u0000R\u000e\u0010\b\u001a\u00020\u0006XT¢\u0006\u0002\n\u0000R\u000e\u0010\t\u001a\u00020\u0006XT¢\u0006\u0002\n\u0000R\u000e\u0010\n\u001a\u00020\u0006XT¢\u0006\u0002\n\u0000R\u000e\u0010\u000b\u001a\u00020\u0006XT¢\u0006\u0002\n\u0000R\u000e\u0010\f\u001a\u00020\u0006XT¢\u0006\u0002\n\u0000¨\u0006\r"}, d2 = {"Lokhttp3/internal/http1/Http1ExchangeCodec$Companion;", "", "()V", "NO_CHUNK_YET", "", "STATE_CLOSED", "", "STATE_IDLE", "STATE_OPEN_REQUEST_BODY", "STATE_OPEN_RESPONSE_BODY", "STATE_READING_RESPONSE_BODY", "STATE_READ_RESPONSE_HEADERS", "STATE_WRITING_REQUEST_BODY", "okhttp"}, k = 1, mv = {1, 4, 0})
      /* compiled from: Http1ExchangeCodec.kt */
      public static final class Companion {
          private Companion() {
          }
  
          public /* synthetic */ Companion(DefaultConstructorMarker $constructor_marker) {
              this();
          }
      }
  
      @Metadata(bv = {1, 0, 3}, 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\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\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, 4, 0})
      /* compiled from: Http1ExchangeCodec.kt */
      private final class FixedLengthSource extends AbstractSource {
          private long bytesRemaining;
  
          public FixedLengthSource(long bytesRemaining2) {
              super();
              this.bytesRemaining = bytesRemaining2;
              if (bytesRemaining2 == 0) {
                  responseBodyComplete();
              }
          }
  
          public void close() {
              if (!getClosed()) {
                  if (this.bytesRemaining != 0 && !Util.discard(this, 100, TimeUnit.MILLISECONDS)) {
                      Http1ExchangeCodec.this.getConnection().noNewExchanges$okhttp();
                      responseBodyComplete();
                  }
                  setClosed(true);
              }
          }
  
          public long read(Buffer sink, long byteCount) {
              Intrinsics.checkNotNullParameter(sink, "sink");
              if (!(byteCount >= 0)) {
                  throw new IllegalArgumentException(("byteCount < 0: " + byteCount).toString());
              } else if (!getClosed()) {
                  long j = this.bytesRemaining;
                  if (j == 0) {
                      return -1;
                  }
                  long read = super.read(sink, Math.min(j, byteCount));
                  if (read != -1) {
                      long j2 = this.bytesRemaining - read;
                      this.bytesRemaining = j2;
                      if (j2 == 0) {
                          responseBodyComplete();
                      }
                      return read;
                  }
                  Http1ExchangeCodec.this.getConnection().noNewExchanges$okhttp();
                  ProtocolException protocolException = new ProtocolException("unexpected end of stream");
                  responseBodyComplete();
                  throw protocolException;
              } else {
                  throw new IllegalStateException("closed".toString());
              }
          }
      }
  
      @Metadata(bv = {1, 0, 3}, 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\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\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\u0005\u001a\u00020\u0006X\u0004¢\u0006\u0002\n\u0000¨\u0006\u0010"}, d2 = {"Lokhttp3/internal/http1/Http1ExchangeCodec$KnownLengthSink;", "Lokio/Sink;", "(Lokhttp3/internal/http1/Http1ExchangeCodec;)V", "closed", "", "timeout", "Lokio/ForwardingTimeout;", "close", "", "flush", "Lokio/Timeout;", "write", "source", "Lokio/Buffer;", "byteCount", "", "okhttp"}, k = 1, mv = {1, 4, 0})
      /* compiled from: Http1ExchangeCodec.kt */
      private final class KnownLengthSink implements Sink {
          private boolean closed;
          private final ForwardingTimeout timeout;
  
          public KnownLengthSink() {
              this.timeout = new ForwardingTimeout(Http1ExchangeCodec.this.sink.timeout());
          }
  
          public void close() {
              if (!this.closed) {
                  this.closed = true;
                  Http1ExchangeCodec.this.detachTimeout(this.timeout);
                  Http1ExchangeCodec.this.state = 3;
              }
          }
  
          public void flush() {
              if (!this.closed) {
                  Http1ExchangeCodec.this.sink.flush();
              }
          }
  
          public Timeout timeout() {
              return this.timeout;
          }
  
          public void write(Buffer source, long byteCount) {
              Intrinsics.checkNotNullParameter(source, "source");
              if (!this.closed) {
                  Util.checkOffsetAndCount(source.size(), 0, byteCount);
                  Http1ExchangeCodec.this.sink.write(source, byteCount);
                  return;
              }
              throw new IllegalStateException("closed".toString());
          }
      }
  
      @Metadata(bv = {1, 0, 3}, 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\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\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, 4, 0})
      /* compiled from: Http1ExchangeCodec.kt */
      private final class UnknownLengthSource extends AbstractSource {
          private boolean inputExhausted;
  
          public UnknownLengthSource() {
              super();
          }
  
          public void close() {
              if (!getClosed()) {
                  if (!this.inputExhausted) {
                      responseBodyComplete();
                  }
                  setClosed(true);
              }
          }
  
          public long read(Buffer sink, long byteCount) {
              Intrinsics.checkNotNullParameter(sink, "sink");
              if (!(byteCount >= 0)) {
                  throw new IllegalArgumentException(("byteCount < 0: " + byteCount).toString());
              } else if (!(!getClosed())) {
                  throw new IllegalStateException("closed".toString());
              } else if (this.inputExhausted) {
                  return -1;
              } else {
                  long read = super.read(sink, byteCount);
                  if (read != -1) {
                      return read;
                  }
                  this.inputExhausted = true;
                  responseBodyComplete();
                  return -1;
              }
          }
      }
  
      public Http1ExchangeCodec(OkHttpClient client2, RealConnection connection2, BufferedSource source2, BufferedSink sink2) {
          Intrinsics.checkNotNullParameter(connection2, "connection");
          Intrinsics.checkNotNullParameter(source2, "source");
          Intrinsics.checkNotNullParameter(sink2, "sink");
          this.client = client2;
          this.connection = connection2;
          this.source = source2;
          this.sink = sink2;
          this.headersReader = new HeadersReader(source2);
      }
  
      /* access modifiers changed from: private */
      public final void detachTimeout(ForwardingTimeout timeout) {
          Timeout delegate = timeout.delegate();
          timeout.setDelegate(Timeout.NONE);
          delegate.clearDeadline();
          delegate.clearTimeout();
      }
  
      private final boolean isChunked(Request $this$isChunked) {
          return StringsKt.equals("chunked", $this$isChunked.header("Transfer-Encoding"), true);
      }
  
      private final boolean isChunked(Response $this$isChunked) {
          String header$default = Response.header$default($this$isChunked, "Transfer-Encoding", (String) null, 2, (Object) null);
          Log1F380D.a((Object) header$default);
          return StringsKt.equals("chunked", header$default, true);
      }
  
      private final Sink newChunkedSink() {
          boolean z = true;
          if (this.state != 1) {
              z = false;
          }
          if (z) {
              this.state = 2;
              return new ChunkedSink();
          }
          throw new IllegalStateException(("state: " + this.state).toString());
      }
  
      private final Source newChunkedSource(HttpUrl url) {
          if (this.state == 4) {
              this.state = 5;
              return new ChunkedSource(this, url);
          }
          throw new IllegalStateException(("state: " + this.state).toString());
      }
  
      private final Source newFixedLengthSource(long length) {
          if (this.state == 4) {
              this.state = 5;
              return new FixedLengthSource(length);
          }
          throw new IllegalStateException(("state: " + this.state).toString());
      }
  
      private final Sink newKnownLengthSink() {
          boolean z = true;
          if (this.state != 1) {
              z = false;
          }
          if (z) {
              this.state = 2;
              return new KnownLengthSink();
          }
          throw new IllegalStateException(("state: " + this.state).toString());
      }
  
      private final Source newUnknownLengthSource() {
          if (this.state == 4) {
              this.state = 5;
              getConnection().noNewExchanges$okhttp();
              return new UnknownLengthSource();
          }
          throw new IllegalStateException(("state: " + this.state).toString());
      }
  
      public void cancel() {
          getConnection().cancel();
      }
  
      public Sink createRequestBody(Request request, long contentLength) {
          Intrinsics.checkNotNullParameter(request, "request");
          if (request.body() != null && request.body().isDuplex()) {
              throw new ProtocolException("Duplex connections are not supported for HTTP/1");
          } else if (isChunked(request)) {
              return newChunkedSink();
          } else {
              if (contentLength != -1) {
                  return newKnownLengthSink();
              }
              throw new IllegalStateException("Cannot stream a request body without chunked encoding or a known content length!");
          }
      }
  
      public void finishRequest() {
          this.sink.flush();
      }
  
      public void flushRequest() {
          this.sink.flush();
      }
  
      public RealConnection getConnection() {
          return this.connection;
      }
  
      public final boolean isClosed() {
          return this.state == 6;
      }
  
      public Source openResponseBodySource(Response response) {
          Intrinsics.checkNotNullParameter(response, "response");
          if (!HttpHeaders.promisesBody(response)) {
              return newFixedLengthSource(0);
          }
          if (isChunked(response)) {
              return newChunkedSource(response.request().url());
          }
          long headersContentLength = Util.headersContentLength(response);
          return headersContentLength != -1 ? newFixedLengthSource(headersContentLength) : newUnknownLengthSource();
      }
  
      public Response.Builder readResponseHeaders(boolean expectContinue) {
          int i = this.state;
          boolean z = true;
          if (!(i == 1 || i == 3)) {
              z = false;
          }
          if (z) {
              try {
                  StatusLine parse = StatusLine.Companion.parse(this.headersReader.readLine());
                  Response.Builder headers = new Response.Builder().protocol(parse.protocol).code(parse.code).message(parse.message).headers(this.headersReader.readHeaders());
                  if (expectContinue && parse.code == 100) {
                      return null;
                  }
                  if (parse.code == 100) {
                      this.state = 3;
                  } else {
                      this.state = 4;
                  }
                  return headers;
              } catch (EOFException e) {
                  throw new IOException("unexpected end of stream on " + getConnection().route().address().url().redact(), e);
              }
          } else {
              throw new IllegalStateException(("state: " + this.state).toString());
          }
      }
  
      public long reportedContentLength(Response response) {
          Intrinsics.checkNotNullParameter(response, "response");
          if (!HttpHeaders.promisesBody(response)) {
              return 0;
          }
          if (isChunked(response)) {
              return -1;
          }
          return Util.headersContentLength(response);
      }
  
      public final void skipConnectBody(Response response) {
          Intrinsics.checkNotNullParameter(response, "response");
          long headersContentLength = Util.headersContentLength(response);
          if (headersContentLength != -1) {
              Source newFixedLengthSource = newFixedLengthSource(headersContentLength);
              Util.skipAll(newFixedLengthSource, Integer.MAX_VALUE, TimeUnit.MILLISECONDS);
              newFixedLengthSource.close();
          }
      }
  
      public Headers trailers() {
          if (this.state == 6) {
              Headers headers = this.trailers;
              return headers != null ? headers : Util.EMPTY_HEADERS;
          }
          throw new IllegalStateException("too early; can't read the trailers yet".toString());
      }
  
      public final void writeRequest(Headers headers, String requestLine) {
          Intrinsics.checkNotNullParameter(headers, "headers");
          Intrinsics.checkNotNullParameter(requestLine, "requestLine");
          if (this.state == 0) {
              this.sink.writeUtf8(requestLine).writeUtf8("\r\n");
              int size = headers.size();
              for (int i = 0; i < size; i++) {
                  this.sink.writeUtf8(headers.name(i)).writeUtf8(": ").writeUtf8(headers.value(i)).writeUtf8("\r\n");
              }
              this.sink.writeUtf8("\r\n");
              this.state = 1;
              return;
          }
          throw new IllegalStateException(("state: " + this.state).toString());
      }
  
      public void writeRequestHeaders(Request request) {
          Intrinsics.checkNotNullParameter(request, "request");
          RequestLine requestLine = RequestLine.INSTANCE;
          Proxy.Type type = getConnection().route().proxy().type();
          Intrinsics.checkNotNullExpressionValue(type, "connection.route().proxy.type()");
          writeRequest(request.headers(), requestLine.get(request, type));
      }
  }
  