package okhttp3.internal.http1; import android.support.v4.media.session.PlaybackStateCompat; import csdk.gluads.Consts; import java.io.EOFException; import java.io.IOException; import java.net.ProtocolException; import java.util.concurrent.TimeUnit; import okhttp3.Headers; import okhttp3.HttpUrl; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; import okhttp3.internal.Internal; 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; import org.apache.http.protocol.HTTP; /* loaded from: classes5.dex */ public final class Http1ExchangeCodec implements ExchangeCodec { public final OkHttpClient client; public final RealConnection realConnection; public final BufferedSink sink; public final BufferedSource source; public Headers trailers; public int state = 0; public long headerLimit = PlaybackStateCompat.ACTION_SET_REPEAT_MODE; @Override // okhttp3.internal.http.ExchangeCodec public RealConnection connection() { return this.realConnection; } public Http1ExchangeCodec(OkHttpClient okHttpClient, RealConnection realConnection, BufferedSource bufferedSource, BufferedSink bufferedSink) { this.client = okHttpClient; this.realConnection = realConnection; this.source = bufferedSource; this.sink = bufferedSink; } @Override // okhttp3.internal.http.ExchangeCodec public Sink createRequestBody(Request request, long j) { if (request.body() != null && request.body().isDuplex()) { throw new ProtocolException("Duplex connections are not supported for HTTP/1"); } if (HTTP.CHUNK_CODING.equalsIgnoreCase(request.header(HTTP.TRANSFER_ENCODING))) { return newChunkedSink(); } if (j != -1) { return newKnownLengthSink(); } throw new IllegalStateException("Cannot stream a request body without chunked encoding or a known content length!"); } @Override // okhttp3.internal.http.ExchangeCodec public void cancel() { RealConnection realConnection = this.realConnection; if (realConnection != null) { realConnection.cancel(); } } @Override // okhttp3.internal.http.ExchangeCodec public void writeRequestHeaders(Request request) { writeRequest(request.headers(), RequestLine.get(request, this.realConnection.route().proxy().type())); } @Override // okhttp3.internal.http.ExchangeCodec public long reportedContentLength(Response response) { if (!HttpHeaders.hasBody(response)) { return 0L; } if (HTTP.CHUNK_CODING.equalsIgnoreCase(response.header(HTTP.TRANSFER_ENCODING))) { return -1L; } return HttpHeaders.contentLength(response); } @Override // okhttp3.internal.http.ExchangeCodec public Source openResponseBodySource(Response response) { if (!HttpHeaders.hasBody(response)) { return newFixedLengthSource(0L); } if (HTTP.CHUNK_CODING.equalsIgnoreCase(response.header(HTTP.TRANSFER_ENCODING))) { return newChunkedSource(response.request().url()); } long contentLength = HttpHeaders.contentLength(response); if (contentLength != -1) { return newFixedLengthSource(contentLength); } return newUnknownLengthSource(); } @Override // okhttp3.internal.http.ExchangeCodec public void flushRequest() { this.sink.flush(); } @Override // okhttp3.internal.http.ExchangeCodec public void finishRequest() { this.sink.flush(); } public void writeRequest(Headers headers, String str) { if (this.state != 0) { throw new IllegalStateException("state: " + this.state); } this.sink.writeUtf8(str).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; } @Override // okhttp3.internal.http.ExchangeCodec public Response.Builder readResponseHeaders(boolean z) { int i = this.state; if (i != 1 && i != 3) { throw new IllegalStateException("state: " + this.state); } try { StatusLine parse = StatusLine.parse(readHeaderLine()); Response.Builder headers = new Response.Builder().protocol(parse.protocol).code(parse.code).message(parse.message).headers(readHeaders()); if (z && parse.code == 100) { return null; } if (parse.code == 100) { this.state = 3; return headers; } this.state = 4; return headers; } catch (EOFException e) { RealConnection realConnection = this.realConnection; throw new IOException("unexpected end of stream on " + (realConnection != null ? realConnection.route().address().url().redact() : "unknown"), e); } } public final String readHeaderLine() { String readUtf8LineStrict = this.source.readUtf8LineStrict(this.headerLimit); this.headerLimit -= readUtf8LineStrict.length(); return readUtf8LineStrict; } public final Headers readHeaders() { Headers.Builder builder = new Headers.Builder(); while (true) { String readHeaderLine = readHeaderLine(); if (readHeaderLine.length() != 0) { Internal.instance.addLenient(builder, readHeaderLine); } else { return builder.build(); } } } public final Sink newChunkedSink() { if (this.state != 1) { throw new IllegalStateException("state: " + this.state); } this.state = 2; return new ChunkedSink(); } public final Sink newKnownLengthSink() { if (this.state != 1) { throw new IllegalStateException("state: " + this.state); } this.state = 2; return new KnownLengthSink(); } public final Source newFixedLengthSource(long j) { if (this.state != 4) { throw new IllegalStateException("state: " + this.state); } this.state = 5; return new FixedLengthSource(j); } public final Source newChunkedSource(HttpUrl httpUrl) { if (this.state != 4) { throw new IllegalStateException("state: " + this.state); } this.state = 5; return new ChunkedSource(httpUrl); } public final Source newUnknownLengthSource() { if (this.state != 4) { throw new IllegalStateException("state: " + this.state); } this.state = 5; this.realConnection.noNewExchanges(); return new UnknownLengthSource(); } public final void detachTimeout(ForwardingTimeout forwardingTimeout) { Timeout delegate = forwardingTimeout.delegate(); forwardingTimeout.setDelegate(Timeout.NONE); delegate.clearDeadline(); delegate.clearTimeout(); } public void skipConnectBody(Response response) { long contentLength = HttpHeaders.contentLength(response); if (contentLength == -1) { return; } Source newFixedLengthSource = newFixedLengthSource(contentLength); Util.skipAll(newFixedLengthSource, Integer.MAX_VALUE, TimeUnit.MILLISECONDS); newFixedLengthSource.close(); } public final class KnownLengthSink implements Sink { public boolean closed; public final ForwardingTimeout timeout; @Override // okio.Sink public Timeout timeout() { return this.timeout; } public KnownLengthSink() { this.timeout = new ForwardingTimeout(Http1ExchangeCodec.this.sink.timeout()); } @Override // okio.Sink public void write(Buffer buffer, long j) { if (this.closed) { throw new IllegalStateException(Consts.PLACEMENT_STATUS_CLOSED); } Util.checkOffsetAndCount(buffer.size(), 0L, j); Http1ExchangeCodec.this.sink.write(buffer, j); } @Override // okio.Sink, java.io.Flushable public void flush() { if (this.closed) { return; } Http1ExchangeCodec.this.sink.flush(); } @Override // okio.Sink, java.io.Closeable, java.lang.AutoCloseable public void close() { if (this.closed) { return; } this.closed = true; Http1ExchangeCodec.this.detachTimeout(this.timeout); Http1ExchangeCodec.this.state = 3; } } public final class ChunkedSink implements Sink { public boolean closed; public final ForwardingTimeout timeout; @Override // okio.Sink public Timeout timeout() { return this.timeout; } public ChunkedSink() { this.timeout = new ForwardingTimeout(Http1ExchangeCodec.this.sink.timeout()); } @Override // okio.Sink public void write(Buffer buffer, long j) { if (this.closed) { throw new IllegalStateException(Consts.PLACEMENT_STATUS_CLOSED); } if (j == 0) { return; } Http1ExchangeCodec.this.sink.writeHexadecimalUnsignedLong(j); Http1ExchangeCodec.this.sink.writeUtf8("\r\n"); Http1ExchangeCodec.this.sink.write(buffer, j); Http1ExchangeCodec.this.sink.writeUtf8("\r\n"); } @Override // okio.Sink, java.io.Flushable public synchronized void flush() { if (this.closed) { return; } Http1ExchangeCodec.this.sink.flush(); } @Override // okio.Sink, java.io.Closeable, java.lang.AutoCloseable public synchronized void close() { if (this.closed) { return; } this.closed = true; Http1ExchangeCodec.this.sink.writeUtf8("0\r\n\r\n"); Http1ExchangeCodec.this.detachTimeout(this.timeout); Http1ExchangeCodec.this.state = 3; } } public abstract class AbstractSource implements Source { public boolean closed; public final ForwardingTimeout timeout; @Override // okio.Source public Timeout timeout() { return this.timeout; } public AbstractSource() { this.timeout = new ForwardingTimeout(Http1ExchangeCodec.this.source.timeout()); } @Override // okio.Source public long read(Buffer buffer, long j) { try { return Http1ExchangeCodec.this.source.read(buffer, j); } catch (IOException e) { Http1ExchangeCodec.this.realConnection.noNewExchanges(); responseBodyComplete(); throw e; } } public final void responseBodyComplete() { if (Http1ExchangeCodec.this.state == 6) { return; } if (Http1ExchangeCodec.this.state == 5) { Http1ExchangeCodec.this.detachTimeout(this.timeout); Http1ExchangeCodec.this.state = 6; } else { throw new IllegalStateException("state: " + Http1ExchangeCodec.this.state); } } } public class FixedLengthSource extends AbstractSource { public long bytesRemaining; public FixedLengthSource(long j) { super(); this.bytesRemaining = j; if (j == 0) { responseBodyComplete(); } } @Override // okhttp3.internal.http1.Http1ExchangeCodec.AbstractSource, okio.Source public long read(Buffer buffer, long j) { if (j < 0) { throw new IllegalArgumentException("byteCount < 0: " + j); } if (this.closed) { throw new IllegalStateException(Consts.PLACEMENT_STATUS_CLOSED); } long j2 = this.bytesRemaining; if (j2 == 0) { return -1L; } long read = super.read(buffer, Math.min(j2, j)); if (read == -1) { Http1ExchangeCodec.this.realConnection.noNewExchanges(); ProtocolException protocolException = new ProtocolException("unexpected end of stream"); responseBodyComplete(); throw protocolException; } long j3 = this.bytesRemaining - read; this.bytesRemaining = j3; if (j3 == 0) { responseBodyComplete(); } return read; } @Override // okio.Source, java.io.Closeable, java.lang.AutoCloseable public void close() { if (this.closed) { return; } if (this.bytesRemaining != 0 && !Util.discard(this, 100, TimeUnit.MILLISECONDS)) { Http1ExchangeCodec.this.realConnection.noNewExchanges(); responseBodyComplete(); } this.closed = true; } } public class ChunkedSource extends AbstractSource { public long bytesRemainingInChunk; public boolean hasMoreChunks; public final HttpUrl url; public ChunkedSource(HttpUrl httpUrl) { super(); this.bytesRemainingInChunk = -1L; this.hasMoreChunks = true; this.url = httpUrl; } @Override // okhttp3.internal.http1.Http1ExchangeCodec.AbstractSource, okio.Source public long read(Buffer buffer, long j) { if (j < 0) { throw new IllegalArgumentException("byteCount < 0: " + j); } if (this.closed) { throw new IllegalStateException(Consts.PLACEMENT_STATUS_CLOSED); } if (!this.hasMoreChunks) { return -1L; } long j2 = this.bytesRemainingInChunk; if (j2 == 0 || j2 == -1) { readChunkSize(); if (!this.hasMoreChunks) { return -1L; } } long read = super.read(buffer, Math.min(j, this.bytesRemainingInChunk)); if (read != -1) { this.bytesRemainingInChunk -= read; return read; } Http1ExchangeCodec.this.realConnection.noNewExchanges(); ProtocolException protocolException = new ProtocolException("unexpected end of stream"); responseBodyComplete(); throw protocolException; } public final void readChunkSize() { if (this.bytesRemainingInChunk != -1) { Http1ExchangeCodec.this.source.readUtf8LineStrict(); } try { this.bytesRemainingInChunk = Http1ExchangeCodec.this.source.readHexadecimalUnsignedLong(); String trim = Http1ExchangeCodec.this.source.readUtf8LineStrict().trim(); if (this.bytesRemainingInChunk < 0 || !(trim.isEmpty() || trim.startsWith(";"))) { throw new ProtocolException("expected chunk size and optional extensions but was \"" + this.bytesRemainingInChunk + trim + "\""); } if (this.bytesRemainingInChunk == 0) { this.hasMoreChunks = false; Http1ExchangeCodec http1ExchangeCodec = Http1ExchangeCodec.this; http1ExchangeCodec.trailers = http1ExchangeCodec.readHeaders(); HttpHeaders.receiveHeaders(Http1ExchangeCodec.this.client.cookieJar(), this.url, Http1ExchangeCodec.this.trailers); responseBodyComplete(); } } catch (NumberFormatException e) { throw new ProtocolException(e.getMessage()); } } @Override // okio.Source, java.io.Closeable, java.lang.AutoCloseable public void close() { if (this.closed) { return; } if (this.hasMoreChunks && !Util.discard(this, 100, TimeUnit.MILLISECONDS)) { Http1ExchangeCodec.this.realConnection.noNewExchanges(); responseBodyComplete(); } this.closed = true; } } public class UnknownLengthSource extends AbstractSource { public boolean inputExhausted; public UnknownLengthSource() { super(); } @Override // okhttp3.internal.http1.Http1ExchangeCodec.AbstractSource, okio.Source public long read(Buffer buffer, long j) { if (j < 0) { throw new IllegalArgumentException("byteCount < 0: " + j); } if (this.closed) { throw new IllegalStateException(Consts.PLACEMENT_STATUS_CLOSED); } if (this.inputExhausted) { return -1L; } long read = super.read(buffer, j); if (read != -1) { return read; } this.inputExhausted = true; responseBodyComplete(); return -1L; } @Override // okio.Source, java.io.Closeable, java.lang.AutoCloseable public void close() { if (this.closed) { return; } if (!this.inputExhausted) { responseBodyComplete(); } this.closed = true; } } }