package okhttp3.n0.http2;

import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.rtsp.RtspHeaders;
import java.io.IOException;
import java.net.ProtocolException;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import kotlin.Metadata;
import kotlin.jvm.internal.o;
import kotlin.text.j;
import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Protocol;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.n0.c;
import okhttp3.n0.connection.RealConnection;
import okhttp3.n0.http.ExchangeCodec;
import okhttp3.n0.http.RealInterceptorChain;
import okhttp3.n0.http.StatusLine;
import okhttp3.n0.http.e;
import okhttp3.n0.http2.Http2Stream;
import okio.ByteString;
import okio.Sink;
import okio.Source;

/* JADX INFO: renamed from: y.n0.j.m, reason: from Kotlin metadata */
/* JADX INFO: loaded from: classes2.dex */
@Metadata(d1 = {"\u0000n\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\u000b\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\t\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\b\u0003\u0018\u0000 (2\u00020\u0001:\u0001(B%\u0012\u0006\u0010\u0002\u001a\u00020\u0003\u0012\u0006\u0010\u0004\u001a\u00020\u0005\u0012\u0006\u0010\u0006\u001a\u00020\u0007\u0012\u0006\u0010\b\u001a\u00020\t¢\u0006\u0002\u0010\nJ\b\u0010\u0013\u001a\u00020\u0014H\u0016J\u0018\u0010\u0015\u001a\u00020\u00162\u0006\u0010\u0017\u001a\u00020\u00182\u0006\u0010\u0019\u001a\u00020\u001aH\u0016J\b\u0010\u001b\u001a\u00020\u0014H\u0016J\b\u0010\u001c\u001a\u00020\u0014H\u0016J\u0010\u0010\u001d\u001a\u00020\u001e2\u0006\u0010\u001f\u001a\u00020 H\u0016J\u0012\u0010!\u001a\u0004\u0018\u00010\"2\u0006\u0010#\u001a\u00020\fH\u0016J\u0010\u0010$\u001a\u00020\u001a2\u0006\u0010\u001f\u001a\u00020 H\u0016J\b\u0010%\u001a\u00020&H\u0016J\u0010\u0010'\u001a\u00020\u00142\u0006\u0010\u0017\u001a\u00020\u0018H\u0016R\u000e\u0010\u000b\u001a\u00020\fX\u0082\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\u0006\u001a\u00020\u0007X\u0082\u0004¢\u0006\u0002\n\u0000R\u0014\u0010\u0004\u001a\u00020\u0005X\u0096\u0004¢\u0006\b\n\u0000\u001a\u0004\b\r\u0010\u000eR\u000e\u0010\b\u001a\u00020\tX\u0082\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\u000f\u001a\u00020\u0010X\u0082\u0004¢\u0006\u0002\n\u0000R\u0010\u0010\u0011\u001a\u0004\u0018\u00010\u0012X\u0082\u000e¢\u0006\u0002\n\u0000¨\u0006)"}, d2 = {"Lokhttp3/internal/http2/Http2ExchangeCodec;", "Lokhttp3/internal/http/ExchangeCodec;", "client", "Lokhttp3/OkHttpClient;", "connection", "Lokhttp3/internal/connection/RealConnection;", "chain", "Lokhttp3/internal/http/RealInterceptorChain;", "http2Connection", "Lokhttp3/internal/http2/Http2Connection;", "(Lokhttp3/OkHttpClient;Lokhttp3/internal/connection/RealConnection;Lokhttp3/internal/http/RealInterceptorChain;Lokhttp3/internal/http2/Http2Connection;)V", "canceled", "", "getConnection", "()Lokhttp3/internal/connection/RealConnection;", "protocol", "Lokhttp3/Protocol;", "stream", "Lokhttp3/internal/http2/Http2Stream;", "cancel", "", "createRequestBody", "Lokio/Sink;", "request", "Lokhttp3/Request;", "contentLength", "", "finishRequest", "flushRequest", "openResponseBodySource", "Lokio/Source;", "response", "Lokhttp3/Response;", "readResponseHeaders", "Lokhttp3/Response$Builder;", "expectContinue", "reportedContentLength", HttpHeaders.Values.TRAILERS, "Lokhttp3/Headers;", "writeRequestHeaders", "Companion", "okhttp"}, k = 1, mv = {1, 6, 0}, xi = 48)
public final class Http2ExchangeCodec implements ExchangeCodec {
    public static final List<String> a = c.m("connection", "host", "keep-alive", "proxy-connection", "te", "transfer-encoding", "encoding", "upgrade", ":method", ":path", ":scheme", ":authority");

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static final List<String> f9966b = c.m("connection", "host", "keep-alive", "proxy-connection", "te", "transfer-encoding", "encoding", "upgrade");

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

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

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

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

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

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

    public Http2ExchangeCodec(OkHttpClient okHttpClient, RealConnection realConnection, RealInterceptorChain realInterceptorChain, Http2Connection http2Connection) {
        o.f(okHttpClient, "client");
        o.f(realConnection, "connection");
        o.f(realInterceptorChain, "chain");
        o.f(http2Connection, "http2Connection");
        this.f9967c = realConnection;
        this.f9968d = realInterceptorChain;
        this.f9969e = http2Connection;
        List<Protocol> list = okHttpClient.F;
        Protocol protocol = Protocol.H2_PRIOR_KNOWLEDGE;
        this.f9971g = list.contains(protocol) ? protocol : Protocol.HTTP_2;
    }

    @Override // okhttp3.n0.http.ExchangeCodec
    public void a() throws SocketTimeoutException {
        Http2Stream http2Stream = this.f9970f;
        o.c(http2Stream);
        ((Http2Stream.a) http2Stream.g()).close();
    }

    @Override // okhttp3.n0.http.ExchangeCodec
    public void b(Request request) throws IOException {
        int i2;
        Http2Stream http2Stream;
        boolean z2;
        o.f(request, "request");
        if (this.f9970f != null) {
            return;
        }
        boolean z3 = request.f9623d != null;
        o.f(request, "request");
        Headers headers = request.f9622c;
        ArrayList arrayList = new ArrayList(headers.size() + 4);
        arrayList.add(new Header(Header.f9878c, request.f9621b));
        ByteString byteString = Header.f9879d;
        HttpUrl httpUrl = request.a;
        o.f(httpUrl, RtspHeaders.Values.URL);
        String strB = httpUrl.b();
        String strD = httpUrl.d();
        if (strD != null) {
            strB = strB + '?' + ((Object) strD);
        }
        arrayList.add(new Header(byteString, strB));
        String strB2 = request.b("Host");
        if (strB2 != null) {
            arrayList.add(new Header(Header.f9881f, strB2));
        }
        arrayList.add(new Header(Header.f9880e, request.a.f10088c));
        int size = headers.size();
        int i3 = 0;
        while (i3 < size) {
            int i4 = i3 + 1;
            String strC = headers.c(i3);
            Locale locale = Locale.US;
            o.e(locale, "US");
            String lowerCase = strC.toLowerCase(locale);
            o.e(lowerCase, "this as java.lang.String).toLowerCase(locale)");
            if (!a.contains(lowerCase) || (o.a(lowerCase, "te") && o.a(headers.e(i3), HttpHeaders.Values.TRAILERS))) {
                arrayList.add(new Header(lowerCase, headers.e(i3)));
            }
            i3 = i4;
        }
        Http2Connection http2Connection = this.f9969e;
        Objects.requireNonNull(http2Connection);
        o.f(arrayList, "requestHeaders");
        boolean z4 = !z3;
        synchronized (http2Connection.M) {
            synchronized (http2Connection) {
                if (http2Connection.f9914s > 1073741823) {
                    http2Connection.g(ErrorCode.REFUSED_STREAM);
                }
                if (http2Connection.f9915t) {
                    throw new ConnectionShutdownException();
                }
                i2 = http2Connection.f9914s;
                http2Connection.f9914s = i2 + 2;
                http2Stream = new Http2Stream(i2, http2Connection, z4, false, null);
                z2 = !z3 || http2Connection.J >= http2Connection.K || http2Stream.f9988e >= http2Stream.f9989f;
                if (http2Stream.i()) {
                    http2Connection.f9911p.put(Integer.valueOf(i2), http2Stream);
                }
            }
            http2Connection.M.g(z4, i2, arrayList);
        }
        if (z2) {
            http2Connection.M.flush();
        }
        this.f9970f = http2Stream;
        if (this.f9972h) {
            Http2Stream http2Stream2 = this.f9970f;
            o.c(http2Stream2);
            http2Stream2.e(ErrorCode.CANCEL);
            throw new IOException("Canceled");
        }
        Http2Stream http2Stream3 = this.f9970f;
        o.c(http2Stream3);
        Http2Stream.c cVar = http2Stream3.f9994k;
        long j2 = this.f9968d.f9831g;
        TimeUnit timeUnit = TimeUnit.MILLISECONDS;
        cVar.g(j2, timeUnit);
        Http2Stream http2Stream4 = this.f9970f;
        o.c(http2Stream4);
        http2Stream4.f9995l.g(this.f9968d.f9832h, timeUnit);
    }

    @Override // okhttp3.n0.http.ExchangeCodec
    public Source c(Response response) {
        o.f(response, "response");
        Http2Stream http2Stream = this.f9970f;
        o.c(http2Stream);
        return http2Stream.f9992i;
    }

    @Override // okhttp3.n0.http.ExchangeCodec
    public void cancel() {
        this.f9972h = true;
        Http2Stream http2Stream = this.f9970f;
        if (http2Stream == null) {
            return;
        }
        http2Stream.e(ErrorCode.CANCEL);
    }

    @Override // okhttp3.n0.http.ExchangeCodec
    public Response.a d(boolean z2) throws IOException {
        Headers headers;
        Http2Stream http2Stream = this.f9970f;
        if (http2Stream == null) {
            throw new IOException("stream wasn't created");
        }
        synchronized (http2Stream) {
            http2Stream.f9994k.h();
            while (http2Stream.f9990g.isEmpty() && http2Stream.f9996m == null) {
                try {
                    http2Stream.l();
                } catch (Throwable th) {
                    http2Stream.f9994k.l();
                    throw th;
                }
            }
            http2Stream.f9994k.l();
            if (!(!http2Stream.f9990g.isEmpty())) {
                IOException iOException = http2Stream.f9997n;
                if (iOException != null) {
                    throw iOException;
                }
                ErrorCode errorCode = http2Stream.f9996m;
                o.c(errorCode);
                throw new StreamResetException(errorCode);
            }
            Headers headersRemoveFirst = http2Stream.f9990g.removeFirst();
            o.e(headersRemoveFirst, "headersQueue.removeFirst()");
            headers = headersRemoveFirst;
        }
        Protocol protocol = this.f9971g;
        o.f(headers, "headerBlock");
        o.f(protocol, "protocol");
        ArrayList arrayList = new ArrayList(20);
        int size = headers.size();
        int i2 = 0;
        StatusLine statusLineA = null;
        while (i2 < size) {
            int i3 = i2 + 1;
            String strC = headers.c(i2);
            String strE = headers.e(i2);
            if (o.a(strC, ":status")) {
                statusLineA = StatusLine.a(o.l("HTTP/1.1 ", strE));
            } else if (!f9966b.contains(strC)) {
                o.f(strC, "name");
                o.f(strE, "value");
                arrayList.add(strC);
                arrayList.add(j.Y(strE).toString());
            }
            i2 = i3;
        }
        if (statusLineA == null) {
            throw new ProtocolException("Expected ':status' header not present");
        }
        Response.a aVar = new Response.a();
        aVar.g(protocol);
        aVar.f9655c = statusLineA.f9837b;
        aVar.f(statusLineA.f9838c);
        Object[] array = arrayList.toArray(new String[0]);
        Objects.requireNonNull(array, "null cannot be cast to non-null type kotlin.Array<T of kotlin.collections.ArraysKt__ArraysJVMKt.toTypedArray>");
        aVar.e(new Headers((String[]) array, null));
        if (z2 && aVar.f9655c == 100) {
            return null;
        }
        return aVar;
    }

    @Override // okhttp3.n0.http.ExchangeCodec
    /* JADX INFO: renamed from: e, reason: from getter */
    public RealConnection getF9967c() {
        return this.f9967c;
    }

    @Override // okhttp3.n0.http.ExchangeCodec
    public void f() {
        this.f9969e.M.flush();
    }

    @Override // okhttp3.n0.http.ExchangeCodec
    public long g(Response response) {
        o.f(response, "response");
        if (e.a(response)) {
            return c.l(response);
        }
        return 0L;
    }

    @Override // okhttp3.n0.http.ExchangeCodec
    public Sink h(Request request, long j2) {
        o.f(request, "request");
        Http2Stream http2Stream = this.f9970f;
        o.c(http2Stream);
        return http2Stream.g();
    }
}
