what-the-bank/sources/okhttp3/internal/http1/Http1ExchangeCodec.java

632 lines
22 KiB
Java
Raw Permalink Normal View History

2024-07-27 18:17:47 +07:00
package okhttp3.internal.http1;
import com.google.common.net.HttpHeaders;
import java.io.EOFException;
import java.io.IOException;
import java.net.ProtocolException;
import java.net.Proxy;
import java.util.concurrent.TimeUnit;
import o.C14953gcr;
import o.C14957gcv;
import o.C15111ghZ;
import o.C15139giB;
import o.C15159gin;
import o.InterfaceC15148gic;
import o.InterfaceC15149gid;
import o.InterfaceC15168giw;
import o.gdZ;
import o.giD;
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.RequestLine;
import okhttp3.internal.http.StatusLine;
/* loaded from: classes.dex */
public final class Http1ExchangeCodec implements ExchangeCodec {
public static final Companion Companion = new Companion(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;
private final OkHttpClient client;
private final RealConnection connection;
private final HeadersReader headersReader;
private final InterfaceC15148gic sink;
private final InterfaceC15149gid source;
private int state;
private Headers trailers;
public Http1ExchangeCodec(OkHttpClient okHttpClient, RealConnection realConnection, InterfaceC15149gid interfaceC15149gid, InterfaceC15148gic interfaceC15148gic) {
C14957gcv.e(realConnection, "");
C14957gcv.e(interfaceC15149gid, "");
C14957gcv.e(interfaceC15148gic, "");
this.client = okHttpClient;
this.connection = realConnection;
this.source = interfaceC15149gid;
this.sink = interfaceC15148gic;
this.headersReader = new HeadersReader(interfaceC15149gid);
}
private final boolean isChunked(Response response) {
return gdZ.a("chunked", Response.header$default(response, HttpHeaders.TRANSFER_ENCODING, null, 2, null), true);
}
private final boolean isChunked(Request request) {
return gdZ.a("chunked", request.header(HttpHeaders.TRANSFER_ENCODING), true);
}
@Override // okhttp3.internal.http.ExchangeCodec
public final InterfaceC15168giw createRequestBody(Request request, long j) {
C14957gcv.e(request, "");
if (request.body() != null && request.body().isDuplex()) {
throw new ProtocolException("Duplex connections are not supported for HTTP/1");
}
if (isChunked(request)) {
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 final void cancel() {
getConnection().cancel();
}
@Override // okhttp3.internal.http.ExchangeCodec
public final void writeRequestHeaders(Request request) {
C14957gcv.e(request, "");
RequestLine requestLine = RequestLine.INSTANCE;
Proxy.Type type = getConnection().route().proxy().type();
C14957gcv.c(type, "");
writeRequest(request.headers(), requestLine.get(request, type));
}
@Override // okhttp3.internal.http.ExchangeCodec
public final long reportedContentLength(Response response) {
C14957gcv.e(response, "");
if (!okhttp3.internal.http.HttpHeaders.promisesBody(response)) {
return 0L;
}
if (isChunked(response)) {
return -1L;
}
return Util.headersContentLength(response);
}
@Override // okhttp3.internal.http.ExchangeCodec
public final giD openResponseBodySource(Response response) {
C14957gcv.e(response, "");
if (!okhttp3.internal.http.HttpHeaders.promisesBody(response)) {
return newFixedLengthSource(0L);
}
if (isChunked(response)) {
return newChunkedSource(response.request().url());
}
long headersContentLength = Util.headersContentLength(response);
if (headersContentLength != -1) {
return newFixedLengthSource(headersContentLength);
}
return newUnknownLengthSource();
}
@Override // okhttp3.internal.http.ExchangeCodec
public final Headers trailers() {
if (this.state != 6) {
throw new IllegalStateException("too early; can't read the trailers yet".toString());
}
Headers headers = this.trailers;
return headers == null ? Util.EMPTY_HEADERS : headers;
}
@Override // okhttp3.internal.http.ExchangeCodec
public final void flushRequest() {
this.sink.flush();
}
@Override // okhttp3.internal.http.ExchangeCodec
public final void finishRequest() {
this.sink.flush();
}
public final void writeRequest(Headers headers, String str) {
C14957gcv.e(headers, "");
C14957gcv.e(str, "");
int i = this.state;
if (i != 0) {
throw new IllegalStateException(C14957gcv.c("state: ", Integer.valueOf(i)).toString());
}
this.sink.d(str).d("\r\n");
int size = headers.size();
for (int i2 = 0; i2 < size; i2++) {
this.sink.d(headers.name(i2)).d(": ").d(headers.value(i2)).d("\r\n");
}
this.sink.d("\r\n");
this.state = 1;
}
@Override // okhttp3.internal.http.ExchangeCodec
public final Response.Builder readResponseHeaders(boolean z) {
int i = this.state;
if (i != 1 && i != 2 && i != 3) {
throw new IllegalStateException(C14957gcv.c("state: ", Integer.valueOf(i)).toString());
}
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 (z && parse.code == 100) {
return null;
}
if (parse.code == 100) {
this.state = 3;
return headers;
}
int i2 = parse.code;
if (102 > i2 || i2 >= 200) {
this.state = 4;
return headers;
}
this.state = 3;
return headers;
} catch (EOFException e) {
throw new IOException(C14957gcv.c("unexpected end of stream on ", (Object) getConnection().route().address().url().redact()), e);
}
}
private final InterfaceC15168giw newChunkedSink() {
int i = this.state;
if (i != 1) {
throw new IllegalStateException(C14957gcv.c("state: ", Integer.valueOf(i)).toString());
}
this.state = 2;
return new ChunkedSink(this);
}
private final InterfaceC15168giw newKnownLengthSink() {
int i = this.state;
if (i != 1) {
throw new IllegalStateException(C14957gcv.c("state: ", Integer.valueOf(i)).toString());
}
this.state = 2;
return new KnownLengthSink(this);
}
private final giD newFixedLengthSource(long j) {
int i = this.state;
if (i != 4) {
throw new IllegalStateException(C14957gcv.c("state: ", Integer.valueOf(i)).toString());
}
this.state = 5;
return new FixedLengthSource(this, j);
}
private final giD newChunkedSource(HttpUrl httpUrl) {
int i = this.state;
if (i != 4) {
throw new IllegalStateException(C14957gcv.c("state: ", Integer.valueOf(i)).toString());
}
this.state = 5;
return new ChunkedSource(this, httpUrl);
}
private final giD newUnknownLengthSource() {
int i = this.state;
if (i != 4) {
throw new IllegalStateException(C14957gcv.c("state: ", Integer.valueOf(i)).toString());
}
this.state = 5;
getConnection().noNewExchanges$okhttp();
return new UnknownLengthSource(this);
}
public final void skipConnectBody(Response response) {
C14957gcv.e(response, "");
long headersContentLength = Util.headersContentLength(response);
if (headersContentLength == -1) {
return;
}
giD newFixedLengthSource = newFixedLengthSource(headersContentLength);
Util.skipAll(newFixedLengthSource, Integer.MAX_VALUE, TimeUnit.MILLISECONDS);
newFixedLengthSource.close();
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes.dex */
public final class KnownLengthSink implements InterfaceC15168giw {
private boolean closed;
final Http1ExchangeCodec this$0;
private final C15159gin timeout;
public KnownLengthSink(Http1ExchangeCodec http1ExchangeCodec) {
C14957gcv.e(http1ExchangeCodec, "");
this.this$0 = http1ExchangeCodec;
this.timeout = new C15159gin(http1ExchangeCodec.sink.timeout());
}
@Override // o.InterfaceC15168giw
public final C15139giB timeout() {
return this.timeout;
}
@Override // o.InterfaceC15168giw, java.io.Flushable
public final void flush() {
if (this.closed) {
return;
}
this.this$0.sink.flush();
}
@Override // o.InterfaceC15168giw, java.io.Closeable, java.lang.AutoCloseable
public final void close() {
if (this.closed) {
return;
}
this.closed = true;
this.this$0.detachTimeout(this.timeout);
this.this$0.state = 3;
}
@Override // o.InterfaceC15168giw
public final void write(C15111ghZ c15111ghZ, long j) {
C14957gcv.e(c15111ghZ, "");
if (!(!this.closed)) {
throw new IllegalStateException("closed".toString());
}
Util.checkOffsetAndCount(c15111ghZ.c, 0L, j);
this.this$0.sink.write(c15111ghZ, j);
}
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes.dex */
public final class ChunkedSink implements InterfaceC15168giw {
private boolean closed;
final Http1ExchangeCodec this$0;
private final C15159gin timeout;
public ChunkedSink(Http1ExchangeCodec http1ExchangeCodec) {
C14957gcv.e(http1ExchangeCodec, "");
this.this$0 = http1ExchangeCodec;
this.timeout = new C15159gin(http1ExchangeCodec.sink.timeout());
}
@Override // o.InterfaceC15168giw
public final C15139giB timeout() {
return this.timeout;
}
@Override // o.InterfaceC15168giw
public final void write(C15111ghZ c15111ghZ, long j) {
C14957gcv.e(c15111ghZ, "");
if (!(!this.closed)) {
throw new IllegalStateException("closed".toString());
}
if (j == 0) {
return;
}
this.this$0.sink.l(j);
this.this$0.sink.d("\r\n");
this.this$0.sink.write(c15111ghZ, j);
this.this$0.sink.d("\r\n");
}
@Override // o.InterfaceC15168giw, java.io.Flushable
public final void flush() {
synchronized (this) {
if (this.closed) {
return;
}
this.this$0.sink.flush();
}
}
@Override // o.InterfaceC15168giw, java.io.Closeable, java.lang.AutoCloseable
public final void close() {
synchronized (this) {
if (this.closed) {
return;
}
this.closed = true;
this.this$0.sink.d("0\r\n\r\n");
this.this$0.detachTimeout(this.timeout);
this.this$0.state = 3;
}
}
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes.dex */
public abstract class AbstractSource implements giD {
private boolean closed;
final Http1ExchangeCodec this$0;
private final C15159gin timeout;
public AbstractSource(Http1ExchangeCodec http1ExchangeCodec) {
C14957gcv.e(http1ExchangeCodec, "");
this.this$0 = http1ExchangeCodec;
this.timeout = new C15159gin(http1ExchangeCodec.source.timeout());
}
@Override // o.giD
public C15139giB timeout() {
return this.timeout;
}
@Override // o.giD
public long read(C15111ghZ c15111ghZ, long j) {
C14957gcv.e(c15111ghZ, "");
try {
return this.this$0.source.read(c15111ghZ, j);
} catch (IOException e) {
this.this$0.getConnection().noNewExchanges$okhttp();
responseBodyComplete();
throw e;
}
}
public final void responseBodyComplete() {
if (this.this$0.state == 6) {
return;
}
if (this.this$0.state == 5) {
this.this$0.detachTimeout(this.timeout);
this.this$0.state = 6;
return;
}
throw new IllegalStateException(C14957gcv.c("state: ", Integer.valueOf(this.this$0.state)));
}
protected final void setClosed(boolean z) {
this.closed = z;
}
protected final C15159gin getTimeout() {
return this.timeout;
}
protected final boolean getClosed() {
return this.closed;
}
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes.dex */
public final class FixedLengthSource extends AbstractSource {
private long bytesRemaining;
final Http1ExchangeCodec this$0;
/* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
public FixedLengthSource(Http1ExchangeCodec http1ExchangeCodec, long j) {
super(http1ExchangeCodec);
C14957gcv.e(http1ExchangeCodec, "");
this.this$0 = http1ExchangeCodec;
this.bytesRemaining = j;
if (j == 0) {
responseBodyComplete();
}
}
@Override // okhttp3.internal.http1.Http1ExchangeCodec.AbstractSource, o.giD
public final long read(C15111ghZ c15111ghZ, long j) {
C14957gcv.e(c15111ghZ, "");
if (j < 0) {
throw new IllegalArgumentException(C14957gcv.c("byteCount < 0: ", Long.valueOf(j)).toString());
}
if (!(!getClosed())) {
throw new IllegalStateException("closed".toString());
}
long j2 = this.bytesRemaining;
if (j2 == 0) {
return -1L;
}
long read = super.read(c15111ghZ, Math.min(j2, j));
if (read == -1) {
this.this$0.getConnection().noNewExchanges$okhttp();
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 // o.giD, java.io.Closeable, java.lang.AutoCloseable
public final void close() {
if (getClosed()) {
return;
}
if (this.bytesRemaining != 0 && !Util.discard(this, 100, TimeUnit.MILLISECONDS)) {
this.this$0.getConnection().noNewExchanges$okhttp();
responseBodyComplete();
}
setClosed(true);
}
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes.dex */
public final class ChunkedSource extends AbstractSource {
private long bytesRemainingInChunk;
private boolean hasMoreChunks;
final Http1ExchangeCodec this$0;
private final HttpUrl url;
/* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
public ChunkedSource(Http1ExchangeCodec http1ExchangeCodec, HttpUrl httpUrl) {
super(http1ExchangeCodec);
C14957gcv.e(http1ExchangeCodec, "");
C14957gcv.e(httpUrl, "");
this.this$0 = http1ExchangeCodec;
this.url = httpUrl;
this.bytesRemainingInChunk = -1L;
this.hasMoreChunks = true;
}
@Override // okhttp3.internal.http1.Http1ExchangeCodec.AbstractSource, o.giD
public final long read(C15111ghZ c15111ghZ, long j) {
C14957gcv.e(c15111ghZ, "");
if (j < 0) {
throw new IllegalArgumentException(C14957gcv.c("byteCount < 0: ", Long.valueOf(j)).toString());
}
if (!(!getClosed())) {
throw new IllegalStateException("closed".toString());
}
if (!this.hasMoreChunks) {
return -1L;
}
long j2 = this.bytesRemainingInChunk;
if (j2 == 0 || j2 == -1) {
readChunkSize();
if (!this.hasMoreChunks) {
return -1L;
}
}
long read = super.read(c15111ghZ, Math.min(j, 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;
}
private final void readChunkSize() {
if (this.bytesRemainingInChunk != -1) {
this.this$0.source.t();
}
try {
this.bytesRemainingInChunk = this.this$0.source.m();
String obj = gdZ.b((CharSequence) this.this$0.source.t()).toString();
if (this.bytesRemainingInChunk < 0 || (obj.length() > 0 && !gdZ.e(obj, ";", false))) {
StringBuilder sb = new StringBuilder("expected chunk size and optional extensions but was \"");
sb.append(this.bytesRemainingInChunk);
sb.append(obj);
sb.append('\"');
throw new ProtocolException(sb.toString());
}
if (this.bytesRemainingInChunk == 0) {
this.hasMoreChunks = false;
Http1ExchangeCodec http1ExchangeCodec = this.this$0;
http1ExchangeCodec.trailers = http1ExchangeCodec.headersReader.readHeaders();
OkHttpClient okHttpClient = this.this$0.client;
C14957gcv.e(okHttpClient);
CookieJar cookieJar = okHttpClient.cookieJar();
HttpUrl httpUrl = this.url;
Headers headers = this.this$0.trailers;
C14957gcv.e(headers);
okhttp3.internal.http.HttpHeaders.receiveHeaders(cookieJar, httpUrl, headers);
responseBodyComplete();
}
} catch (NumberFormatException e) {
throw new ProtocolException(e.getMessage());
}
}
@Override // o.giD, java.io.Closeable, java.lang.AutoCloseable
public final void close() {
if (getClosed()) {
return;
}
if (this.hasMoreChunks && !Util.discard(this, 100, TimeUnit.MILLISECONDS)) {
this.this$0.getConnection().noNewExchanges$okhttp();
responseBodyComplete();
}
setClosed(true);
}
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes.dex */
public final class UnknownLengthSource extends AbstractSource {
private boolean inputExhausted;
final Http1ExchangeCodec this$0;
/* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
public UnknownLengthSource(Http1ExchangeCodec http1ExchangeCodec) {
super(http1ExchangeCodec);
C14957gcv.e(http1ExchangeCodec, "");
this.this$0 = http1ExchangeCodec;
}
@Override // okhttp3.internal.http1.Http1ExchangeCodec.AbstractSource, o.giD
public final long read(C15111ghZ c15111ghZ, long j) {
C14957gcv.e(c15111ghZ, "");
if (j < 0) {
throw new IllegalArgumentException(C14957gcv.c("byteCount < 0: ", Long.valueOf(j)).toString());
}
if (!(!getClosed())) {
throw new IllegalStateException("closed".toString());
}
if (this.inputExhausted) {
return -1L;
}
long read = super.read(c15111ghZ, j);
if (read != -1) {
return read;
}
this.inputExhausted = true;
responseBodyComplete();
return -1L;
}
@Override // o.giD, java.io.Closeable, java.lang.AutoCloseable
public final void close() {
if (getClosed()) {
return;
}
if (!this.inputExhausted) {
responseBodyComplete();
}
setClosed(true);
}
}
/* loaded from: classes.dex */
public static final class Companion {
private Companion() {
}
public /* synthetic */ Companion(C14953gcr c14953gcr) {
this();
}
}
/* JADX INFO: Access modifiers changed from: private */
public final void detachTimeout(C15159gin c15159gin) {
C15139giB c15139giB = c15159gin.c;
C15139giB c15139giB2 = C15139giB.NONE;
C14957gcv.e(c15139giB2, "");
c15159gin.c = c15139giB2;
c15139giB.clearDeadline();
c15139giB.clearTimeout();
}
public final boolean isClosed() {
return this.state == 6;
}
@Override // okhttp3.internal.http.ExchangeCodec
public final RealConnection getConnection() {
return this.connection;
}
}