package okhttp3.internal.http2;

import com.google.android.recaptcha.internal.a;
import com.revenuecat.purchases.common.diagnostics.DiagnosticsTracker;
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 kotlin.jvm.internal.o;
import okhttp3.Headers;
import okhttp3.HttpUrl;
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;
import okhttp3.internal.http2.Http2Stream;
import p5.C2743m;
import p5.N;
import p5.P;

/* JADX INFO: loaded from: classes3.dex */
public final class Http2ExchangeCodec implements ExchangeCodec {

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public static final Companion f22533g = new Companion(0);

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public static final List f22534h = Util.k("connection", DiagnosticsTracker.HOST_KEY, "keep-alive", "proxy-connection", "te", "transfer-encoding", "encoding", "upgrade", ":method", ":path", ":scheme", ":authority");

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public static final List f22535i = Util.k("connection", DiagnosticsTracker.HOST_KEY, "keep-alive", "proxy-connection", "te", "transfer-encoding", "encoding", "upgrade");

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final RealConnection f22536a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final RealInterceptorChain f22537b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final Http2Connection f22538c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public volatile Http2Stream f22539d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final Protocol f22540e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public volatile boolean f22541f;

    public static final class Companion {
        public /* synthetic */ Companion(int i6) {
            this();
        }

        private Companion() {
        }
    }

    public Http2ExchangeCodec(OkHttpClient client, RealConnection connection, RealInterceptorChain realInterceptorChain, Http2Connection http2Connection) {
        o.h(client, "client");
        o.h(connection, "connection");
        o.h(http2Connection, "http2Connection");
        this.f22536a = connection;
        this.f22537b = realInterceptorChain;
        this.f22538c = http2Connection;
        Protocol protocol = Protocol.H2_PRIOR_KNOWLEDGE;
        this.f22540e = client.f22089v.contains(protocol) ? protocol : Protocol.HTTP_2;
    }

    @Override // okhttp3.internal.http.ExchangeCodec
    public final void a() {
        Http2Stream http2Stream = this.f22539d;
        o.e(http2Stream);
        http2Stream.f().close();
    }

    @Override // okhttp3.internal.http.ExchangeCodec
    public final void b(Request request) throws IOException {
        int i6;
        Http2Stream http2Stream;
        o.h(request, "request");
        if (this.f22539d != null) {
            return;
        }
        boolean z6 = true;
        boolean z7 = request.f22128d != null;
        f22533g.getClass();
        Headers headers = request.f22127c;
        ArrayList arrayList = new ArrayList(headers.size() + 4);
        arrayList.add(new Header(Header.f22438f, request.f22126b));
        C2743m c2743m = Header.f22439g;
        RequestLine requestLine = RequestLine.f22396a;
        HttpUrl httpUrl = request.f22125a;
        requestLine.getClass();
        arrayList.add(new Header(c2743m, RequestLine.a(httpUrl)));
        String strG = request.f22127c.g("Host");
        if (strG != null) {
            arrayList.add(new Header(Header.f22441i, strG));
        }
        arrayList.add(new Header(Header.f22440h, httpUrl.f22024a));
        int size = headers.size();
        for (int i7 = 0; i7 < size; i7++) {
            String strH = headers.h(i7);
            Locale locale = Locale.US;
            String strO = a.o(locale, "US", strH, locale, "this as java.lang.String).toLowerCase(locale)");
            if (!f22534h.contains(strO) || (strO.equals("te") && o.c(headers.l(i7), "trailers"))) {
                arrayList.add(new Header(strO, headers.l(i7)));
            }
        }
        Http2Connection http2Connection = this.f22538c;
        http2Connection.getClass();
        boolean z8 = !z7;
        synchronized (http2Connection.f22470A) {
            synchronized (http2Connection) {
                try {
                    if (http2Connection.f22477e > 1073741823) {
                        http2Connection.f(ErrorCode.REFUSED_STREAM);
                    }
                    if (http2Connection.f22478f) {
                        throw new ConnectionShutdownException();
                    }
                    i6 = http2Connection.f22477e;
                    http2Connection.f22477e = i6 + 2;
                    http2Stream = new Http2Stream(i6, http2Connection, z8, false, null);
                    if (z7 && http2Connection.f22492x < http2Connection.f22493y && http2Stream.f22557e < http2Stream.f22558f) {
                        z6 = false;
                    }
                    if (http2Stream.h()) {
                        http2Connection.f22474b.put(Integer.valueOf(i6), http2Stream);
                    }
                } catch (Throwable th) {
                    throw th;
                }
            }
            http2Connection.f22470A.f(z8, i6, arrayList);
        }
        if (z6) {
            http2Connection.f22470A.flush();
        }
        this.f22539d = http2Stream;
        if (this.f22541f) {
            Http2Stream http2Stream2 = this.f22539d;
            o.e(http2Stream2);
            http2Stream2.e(ErrorCode.CANCEL);
            throw new IOException("Canceled");
        }
        Http2Stream http2Stream3 = this.f22539d;
        o.e(http2Stream3);
        Http2Stream.StreamTimeout streamTimeout = http2Stream3.f22563k;
        long j4 = this.f22537b.f22390g;
        TimeUnit timeUnit = TimeUnit.MILLISECONDS;
        streamTimeout.g(j4, timeUnit);
        Http2Stream http2Stream4 = this.f22539d;
        o.e(http2Stream4);
        http2Stream4.l.g(this.f22537b.f22391h, timeUnit);
    }

    @Override // okhttp3.internal.http.ExchangeCodec
    public final P c(Response response) {
        Http2Stream http2Stream = this.f22539d;
        o.e(http2Stream);
        return http2Stream.f22561i;
    }

    @Override // okhttp3.internal.http.ExchangeCodec
    public final void cancel() {
        this.f22541f = true;
        Http2Stream http2Stream = this.f22539d;
        if (http2Stream != null) {
            http2Stream.e(ErrorCode.CANCEL);
        }
    }

    @Override // okhttp3.internal.http.ExchangeCodec
    public final Response.Builder d(boolean z6) throws IOException {
        Headers headers;
        Http2Stream http2Stream = this.f22539d;
        if (http2Stream == null) {
            throw new IOException("stream wasn't created");
        }
        synchronized (http2Stream) {
            http2Stream.f22563k.h();
            while (http2Stream.f22559g.isEmpty() && http2Stream.f22564m == null) {
                try {
                    http2Stream.k();
                } catch (Throwable th) {
                    http2Stream.f22563k.k();
                    throw th;
                }
            }
            http2Stream.f22563k.k();
            if (http2Stream.f22559g.isEmpty()) {
                IOException iOException = http2Stream.f22565n;
                if (iOException != null) {
                    throw iOException;
                }
                ErrorCode errorCode = http2Stream.f22564m;
                o.e(errorCode);
                throw new StreamResetException(errorCode);
            }
            Object objRemoveFirst = http2Stream.f22559g.removeFirst();
            o.g(objRemoveFirst, "headersQueue.removeFirst()");
            headers = (Headers) objRemoveFirst;
        }
        Companion companion = f22533g;
        Protocol protocol = this.f22540e;
        companion.getClass();
        o.h(protocol, "protocol");
        Headers.Builder builder = new Headers.Builder();
        int size = headers.size();
        StatusLine statusLineA = null;
        for (int i6 = 0; i6 < size; i6++) {
            String strH = headers.h(i6);
            String strL = headers.l(i6);
            if (o.c(strH, ":status")) {
                StatusLine.f22398d.getClass();
                statusLineA = StatusLine.Companion.a("HTTP/1.1 " + strL);
            } else if (!f22535i.contains(strH)) {
                builder.c(strH, strL);
            }
        }
        if (statusLineA == null) {
            throw new ProtocolException("Expected ':status' header not present");
        }
        Response.Builder builder2 = new Response.Builder();
        builder2.f22154b = protocol;
        builder2.f22155c = statusLineA.f22400b;
        String message = statusLineA.f22401c;
        o.h(message, "message");
        builder2.f22156d = message;
        builder2.c(builder.e());
        if (z6 && builder2.f22155c == 100) {
            return null;
        }
        return builder2;
    }

    @Override // okhttp3.internal.http.ExchangeCodec
    public final RealConnection e() {
        return this.f22536a;
    }

    @Override // okhttp3.internal.http.ExchangeCodec
    public final void f() {
        this.f22538c.f22470A.flush();
    }

    @Override // okhttp3.internal.http.ExchangeCodec
    public final long g(Response response) {
        if (HttpHeaders.a(response)) {
            return Util.j(response);
        }
        return 0L;
    }

    @Override // okhttp3.internal.http.ExchangeCodec
    public final N h(Request request, long j4) {
        o.h(request, "request");
        Http2Stream http2Stream = this.f22539d;
        o.e(http2Stream);
        return http2Stream.f();
    }
}
