package okhttp3.internal.http2; 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 okhttp3.Headers; import okhttp3.Interceptor; import okhttp3.OkHttpClient; import okhttp3.Protocol; 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.Sink; import okio.Source; import okio.Timeout; import org.apache.http.protocol.HTTP; /* loaded from: classes5.dex */ public final class Http2ExchangeCodec implements ExchangeCodec { public static final List HTTP_2_SKIPPED_REQUEST_HEADERS = Util.immutableList("connection", "host", "keep-alive", "proxy-connection", "te", "transfer-encoding", "encoding", "upgrade", com.mbridge.msdk.thrid.okhttp.internal.http2.Header.TARGET_METHOD_UTF8, com.mbridge.msdk.thrid.okhttp.internal.http2.Header.TARGET_PATH_UTF8, com.mbridge.msdk.thrid.okhttp.internal.http2.Header.TARGET_SCHEME_UTF8, com.mbridge.msdk.thrid.okhttp.internal.http2.Header.TARGET_AUTHORITY_UTF8); public static final List HTTP_2_SKIPPED_RESPONSE_HEADERS = Util.immutableList("connection", "host", "keep-alive", "proxy-connection", "te", "transfer-encoding", "encoding", "upgrade"); public volatile boolean canceled; public final Interceptor.Chain chain; public final Http2Connection connection; public final Protocol protocol; public final RealConnection realConnection; public volatile Http2Stream stream; @Override // okhttp3.internal.http.ExchangeCodec public RealConnection connection() { return this.realConnection; } public Http2ExchangeCodec(OkHttpClient okHttpClient, RealConnection realConnection, Interceptor.Chain chain, Http2Connection http2Connection) { this.realConnection = realConnection; this.chain = chain; this.connection = http2Connection; List protocols = okHttpClient.protocols(); Protocol protocol = Protocol.H2_PRIOR_KNOWLEDGE; this.protocol = protocols.contains(protocol) ? protocol : Protocol.HTTP_2; } @Override // okhttp3.internal.http.ExchangeCodec public Sink createRequestBody(Request request, long j) { return this.stream.getSink(); } @Override // okhttp3.internal.http.ExchangeCodec public void writeRequestHeaders(Request request) { if (this.stream != null) { return; } this.stream = this.connection.newStream(http2HeadersList(request), request.body() != null); if (this.canceled) { this.stream.closeLater(ErrorCode.CANCEL); throw new IOException("Canceled"); } Timeout readTimeout = this.stream.readTimeout(); long readTimeoutMillis = this.chain.readTimeoutMillis(); TimeUnit timeUnit = TimeUnit.MILLISECONDS; readTimeout.timeout(readTimeoutMillis, timeUnit); this.stream.writeTimeout().timeout(this.chain.writeTimeoutMillis(), timeUnit); } @Override // okhttp3.internal.http.ExchangeCodec public void flushRequest() { this.connection.flush(); } @Override // okhttp3.internal.http.ExchangeCodec public void finishRequest() { this.stream.getSink().close(); } @Override // okhttp3.internal.http.ExchangeCodec public Response.Builder readResponseHeaders(boolean z) { Response.Builder readHttp2HeadersList = readHttp2HeadersList(this.stream.takeHeaders(), this.protocol); if (z && Internal.instance.code(readHttp2HeadersList) == 100) { return null; } return readHttp2HeadersList; } public static List http2HeadersList(Request 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.requestPath(request.url()))); String header = request.header(HTTP.TARGET_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 lowerCase = headers.name(i).toLowerCase(Locale.US); if (!HTTP_2_SKIPPED_REQUEST_HEADERS.contains(lowerCase) || (lowerCase.equals("te") && headers.value(i).equals("trailers"))) { arrayList.add(new Header(lowerCase, headers.value(i))); } } return arrayList; } public static Response.Builder readHttp2HeadersList(Headers headers, Protocol 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 (name.equals(com.mbridge.msdk.thrid.okhttp.internal.http2.Header.RESPONSE_STATUS_UTF8)) { statusLine = StatusLine.parse("HTTP/1.1 " + value); } else if (!HTTP_2_SKIPPED_RESPONSE_HEADERS.contains(name)) { Internal.instance.addLenient(builder, 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()); } @Override // okhttp3.internal.http.ExchangeCodec public long reportedContentLength(Response response) { return HttpHeaders.contentLength(response); } @Override // okhttp3.internal.http.ExchangeCodec public Source openResponseBodySource(Response response) { return this.stream.getSource(); } @Override // okhttp3.internal.http.ExchangeCodec public void cancel() { this.canceled = true; if (this.stream != null) { this.stream.closeLater(ErrorCode.CANCEL); } } }