package okhttp3.internal.http2;

import f.f;
import f.h;
import f.m;
import f.s;
import f.t;
import f.u;
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.OkHttpClient;
import okhttp3.Protocol;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okhttp3.internal.Internal;
import okhttp3.internal.Util;
import okhttp3.internal.connection.StreamAllocation;
import okhttp3.internal.http.HttpCodec;
import okhttp3.internal.http.RealResponseBody;
import okhttp3.internal.http.RequestLine;
import okhttp3.internal.http.StatusLine;

/* JADX INFO: loaded from: classes.dex */
public final class Http2Codec implements HttpCodec {
    private static final f CONNECTION;
    private static final f ENCODING;
    private static final f HOST;
    private static final List<f> HTTP_2_SKIPPED_REQUEST_HEADERS;
    private static final List<f> HTTP_2_SKIPPED_RESPONSE_HEADERS;
    private static final f KEEP_ALIVE;
    private static final f PROXY_CONNECTION;
    private static final f TE;
    private static final f TRANSFER_ENCODING;
    private static final f UPGRADE;
    private final OkHttpClient client;
    private final Http2Connection connection;
    private Http2Stream stream;
    final StreamAllocation streamAllocation;

    class StreamFinishingSource extends h {
        StreamFinishingSource(t tVar) {
            super(tVar);
        }

        @Override // f.h, f.t, java.io.Closeable, java.lang.AutoCloseable
        public void close() throws IOException {
            Http2Codec http2Codec = Http2Codec.this;
            http2Codec.streamAllocation.streamFinished(false, http2Codec);
            super.close();
        }
    }

    static {
        f fVarH = f.h("connection");
        CONNECTION = fVarH;
        f fVarH2 = f.h("host");
        HOST = fVarH2;
        f fVarH3 = f.h("keep-alive");
        KEEP_ALIVE = fVarH3;
        f fVarH4 = f.h("proxy-connection");
        PROXY_CONNECTION = fVarH4;
        f fVarH5 = f.h("transfer-encoding");
        TRANSFER_ENCODING = fVarH5;
        f fVarH6 = f.h("te");
        TE = fVarH6;
        f fVarH7 = f.h("encoding");
        ENCODING = fVarH7;
        f fVarH8 = f.h("upgrade");
        UPGRADE = fVarH8;
        HTTP_2_SKIPPED_REQUEST_HEADERS = Util.immutableList(fVarH, fVarH2, fVarH3, fVarH4, fVarH6, fVarH5, fVarH7, fVarH8, Header.TARGET_METHOD, Header.TARGET_PATH, Header.TARGET_SCHEME, Header.TARGET_AUTHORITY);
        HTTP_2_SKIPPED_RESPONSE_HEADERS = Util.immutableList(fVarH, fVarH2, fVarH3, fVarH4, fVarH6, fVarH5, fVarH7, fVarH8);
    }

    public Http2Codec(OkHttpClient okHttpClient, StreamAllocation streamAllocation, Http2Connection http2Connection) {
        this.client = okHttpClient;
        this.streamAllocation = streamAllocation;
        this.connection = http2Connection;
    }

    public static List<Header> 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 strHeader = request.header("Host");
        if (strHeader != null) {
            arrayList.add(new Header(Header.TARGET_AUTHORITY, strHeader));
        }
        arrayList.add(new Header(Header.TARGET_SCHEME, request.url().scheme()));
        int size = headers.size();
        for (int i = 0; i < size; i++) {
            f fVarH = f.h(headers.name(i).toLowerCase(Locale.US));
            if (!HTTP_2_SKIPPED_REQUEST_HEADERS.contains(fVarH)) {
                arrayList.add(new Header(fVarH, headers.value(i)));
            }
        }
        return arrayList;
    }

    public static Response.Builder readHttp2HeadersList(List<Header> list) throws IOException {
        Headers.Builder builder = new Headers.Builder();
        int size = list.size();
        StatusLine statusLine = null;
        for (int i = 0; i < size; i++) {
            Header header = list.get(i);
            if (header != null) {
                f fVar = header.name;
                String strV = header.value.v();
                if (fVar.equals(Header.RESPONSE_STATUS)) {
                    statusLine = StatusLine.parse("HTTP/1.1 " + strV);
                } else if (!HTTP_2_SKIPPED_RESPONSE_HEADERS.contains(fVar)) {
                    Internal.instance.addLenient(builder, fVar.v(), strV);
                }
            } else if (statusLine != null && statusLine.code == 100) {
                builder = new Headers.Builder();
                statusLine = null;
            }
        }
        if (statusLine != null) {
            return new Response.Builder().protocol(Protocol.HTTP_2).code(statusLine.code).message(statusLine.message).headers(builder.build());
        }
        throw new ProtocolException("Expected ':status' header not present");
    }

    @Override // okhttp3.internal.http.HttpCodec
    public void cancel() {
        Http2Stream http2Stream = this.stream;
        if (http2Stream != null) {
            http2Stream.closeLater(ErrorCode.CANCEL);
        }
    }

    @Override // okhttp3.internal.http.HttpCodec
    public s createRequestBody(Request request, long j) {
        return this.stream.getSink();
    }

    @Override // okhttp3.internal.http.HttpCodec
    public void finishRequest() throws IOException {
        this.stream.getSink().close();
    }

    @Override // okhttp3.internal.http.HttpCodec
    public void flushRequest() throws IOException {
        this.connection.flush();
    }

    @Override // okhttp3.internal.http.HttpCodec
    public ResponseBody openResponseBody(Response response) throws IOException {
        return new RealResponseBody(response.headers(), m.d(new StreamFinishingSource(this.stream.getSource())));
    }

    @Override // okhttp3.internal.http.HttpCodec
    public Response.Builder readResponseHeaders(boolean z) throws IOException {
        Response.Builder http2HeadersList = readHttp2HeadersList(this.stream.takeResponseHeaders());
        if (z && Internal.instance.code(http2HeadersList) == 100) {
            return null;
        }
        return http2HeadersList;
    }

    @Override // okhttp3.internal.http.HttpCodec
    public void writeRequestHeaders(Request request) throws IOException {
        if (this.stream != null) {
            return;
        }
        Http2Stream http2StreamNewStream = this.connection.newStream(http2HeadersList(request), request.body() != null);
        this.stream = http2StreamNewStream;
        u timeout = http2StreamNewStream.readTimeout();
        long timeoutMillis = this.client.readTimeoutMillis();
        TimeUnit timeUnit = TimeUnit.MILLISECONDS;
        timeout.timeout(timeoutMillis, timeUnit);
        this.stream.writeTimeout().timeout(this.client.writeTimeoutMillis(), timeUnit);
    }
}
