what-the-bank/sources/okhttp3/internal/http2/Http2ExchangeCodec.java

212 lines
8.8 KiB
Java
Raw Normal View History

2024-07-27 18:17:47 +07:00
package okhttp3.internal.http2;
import io.grpc.internal.GrpcUtil;
import java.io.IOException;
import java.net.ProtocolException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
import o.C14953gcr;
import o.C14957gcv;
import o.InterfaceC15168giw;
import o.giD;
import okhttp3.Headers;
import okhttp3.OkHttpClient;
import okhttp3.Protocol;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.internal.Util;
import okhttp3.internal.connection.RealConnection;
import okhttp3.internal.http.ExchangeCodec;
import okhttp3.internal.http.HttpHeaders;
import okhttp3.internal.http.RealInterceptorChain;
import okhttp3.internal.http.RequestLine;
import okhttp3.internal.http.StatusLine;
/* loaded from: classes.dex */
public final class Http2ExchangeCodec implements ExchangeCodec {
private volatile boolean canceled;
private final RealInterceptorChain chain;
private final RealConnection connection;
private final Http2Connection http2Connection;
private final Protocol protocol;
private volatile Http2Stream stream;
public static final Companion Companion = new Companion(null);
private static final String CONNECTION = "connection";
private static final String HOST = "host";
private static final String KEEP_ALIVE = "keep-alive";
private static final String PROXY_CONNECTION = "proxy-connection";
private static final String TE = "te";
private static final String TRANSFER_ENCODING = "transfer-encoding";
private static final String ENCODING = "encoding";
private static final String UPGRADE = "upgrade";
private static final List<String> HTTP_2_SKIPPED_REQUEST_HEADERS = Util.immutableListOf(CONNECTION, HOST, KEEP_ALIVE, PROXY_CONNECTION, TE, TRANSFER_ENCODING, ENCODING, UPGRADE, Header.TARGET_METHOD_UTF8, Header.TARGET_PATH_UTF8, Header.TARGET_SCHEME_UTF8, Header.TARGET_AUTHORITY_UTF8);
private static final List<String> HTTP_2_SKIPPED_RESPONSE_HEADERS = Util.immutableListOf(CONNECTION, HOST, KEEP_ALIVE, PROXY_CONNECTION, TE, TRANSFER_ENCODING, ENCODING, UPGRADE);
public Http2ExchangeCodec(OkHttpClient okHttpClient, RealConnection realConnection, RealInterceptorChain realInterceptorChain, Http2Connection http2Connection) {
Protocol protocol;
C14957gcv.e(okHttpClient, "");
C14957gcv.e(realConnection, "");
C14957gcv.e(realInterceptorChain, "");
C14957gcv.e(http2Connection, "");
this.connection = realConnection;
this.chain = realInterceptorChain;
this.http2Connection = http2Connection;
if (okHttpClient.protocols().contains(Protocol.H2_PRIOR_KNOWLEDGE)) {
protocol = Protocol.H2_PRIOR_KNOWLEDGE;
} else {
protocol = Protocol.HTTP_2;
}
this.protocol = protocol;
}
@Override // okhttp3.internal.http.ExchangeCodec
public final InterfaceC15168giw createRequestBody(Request request, long j) {
C14957gcv.e(request, "");
Http2Stream http2Stream = this.stream;
C14957gcv.e(http2Stream);
return http2Stream.getSink();
}
@Override // okhttp3.internal.http.ExchangeCodec
public final void writeRequestHeaders(Request request) {
C14957gcv.e(request, "");
if (this.stream != null) {
return;
}
this.stream = this.http2Connection.newStream(Companion.http2HeadersList(request), request.body() != null);
if (this.canceled) {
Http2Stream http2Stream = this.stream;
C14957gcv.e(http2Stream);
http2Stream.closeLater(ErrorCode.CANCEL);
throw new IOException("Canceled");
}
Http2Stream http2Stream2 = this.stream;
C14957gcv.e(http2Stream2);
http2Stream2.readTimeout().timeout(this.chain.getReadTimeoutMillis$okhttp(), TimeUnit.MILLISECONDS);
Http2Stream http2Stream3 = this.stream;
C14957gcv.e(http2Stream3);
http2Stream3.writeTimeout().timeout(this.chain.getWriteTimeoutMillis$okhttp(), TimeUnit.MILLISECONDS);
}
@Override // okhttp3.internal.http.ExchangeCodec
public final void flushRequest() {
this.http2Connection.flush();
}
@Override // okhttp3.internal.http.ExchangeCodec
public final void finishRequest() {
Http2Stream http2Stream = this.stream;
C14957gcv.e(http2Stream);
http2Stream.getSink().close();
}
@Override // okhttp3.internal.http.ExchangeCodec
public final Response.Builder readResponseHeaders(boolean z) {
Http2Stream http2Stream = this.stream;
if (http2Stream == null) {
throw new IOException("stream wasn't created");
}
Response.Builder readHttp2HeadersList = Companion.readHttp2HeadersList(http2Stream.takeHeaders(), this.protocol);
if (z && readHttp2HeadersList.getCode$okhttp() == 100) {
return null;
}
return readHttp2HeadersList;
}
@Override // okhttp3.internal.http.ExchangeCodec
public final long reportedContentLength(Response response) {
C14957gcv.e(response, "");
if (HttpHeaders.promisesBody(response)) {
return Util.headersContentLength(response);
}
return 0L;
}
@Override // okhttp3.internal.http.ExchangeCodec
public final giD openResponseBodySource(Response response) {
C14957gcv.e(response, "");
Http2Stream http2Stream = this.stream;
C14957gcv.e(http2Stream);
return http2Stream.getSource$okhttp();
}
@Override // okhttp3.internal.http.ExchangeCodec
public final Headers trailers() {
Http2Stream http2Stream = this.stream;
C14957gcv.e(http2Stream);
return http2Stream.trailers();
}
@Override // okhttp3.internal.http.ExchangeCodec
public final void cancel() {
this.canceled = true;
Http2Stream http2Stream = this.stream;
if (http2Stream != null) {
http2Stream.closeLater(ErrorCode.CANCEL);
}
}
/* loaded from: classes.dex */
public static final class Companion {
private Companion() {
}
public final List<Header> http2HeadersList(Request request) {
C14957gcv.e(request, "");
Headers headers = request.headers();
ArrayList arrayList = new ArrayList(headers.size() + 4);
arrayList.add(new Header(Header.TARGET_METHOD, request.method()));
arrayList.add(new Header(Header.TARGET_PATH, RequestLine.INSTANCE.requestPath(request.url())));
String header = request.header(com.google.common.net.HttpHeaders.HOST);
if (header != null) {
arrayList.add(new Header(Header.TARGET_AUTHORITY, header));
}
arrayList.add(new Header(Header.TARGET_SCHEME, request.url().scheme()));
int size = headers.size();
for (int i = 0; i < size; i++) {
String name = headers.name(i);
Locale locale = Locale.US;
C14957gcv.c(locale, "");
String lowerCase = name.toLowerCase(locale);
C14957gcv.c((Object) lowerCase, "");
if (!Http2ExchangeCodec.HTTP_2_SKIPPED_REQUEST_HEADERS.contains(lowerCase) || (C14957gcv.b((Object) lowerCase, (Object) Http2ExchangeCodec.TE) && C14957gcv.b((Object) headers.value(i), (Object) GrpcUtil.TE_TRAILERS))) {
arrayList.add(new Header(lowerCase, headers.value(i)));
}
}
return arrayList;
}
public final Response.Builder readHttp2HeadersList(Headers headers, Protocol protocol) {
C14957gcv.e(headers, "");
C14957gcv.e(protocol, "");
Headers.Builder builder = new Headers.Builder();
int size = headers.size();
StatusLine statusLine = null;
for (int i = 0; i < size; i++) {
String name = headers.name(i);
String value = headers.value(i);
if (C14957gcv.b((Object) name, (Object) Header.RESPONSE_STATUS_UTF8)) {
statusLine = StatusLine.Companion.parse(C14957gcv.c("HTTP/1.1 ", (Object) value));
} else if (!Http2ExchangeCodec.HTTP_2_SKIPPED_RESPONSE_HEADERS.contains(name)) {
builder.addLenient$okhttp(name, value);
}
}
if (statusLine == null) {
throw new ProtocolException("Expected ':status' header not present");
}
return new Response.Builder().protocol(protocol).code(statusLine.code).message(statusLine.message).headers(builder.build());
}
public /* synthetic */ Companion(C14953gcr c14953gcr) {
this();
}
}
@Override // okhttp3.internal.http.ExchangeCodec
public final RealConnection getConnection() {
return this.connection;
}
}