package okhttp3.n0.cache;

import io.netty.handler.codec.http.HttpHeaders;
import java.util.ArrayList;
import java.util.Objects;
import kotlin.Metadata;
import kotlin.jvm.internal.DefaultConstructorMarker;
import kotlin.jvm.internal.o;
import kotlin.text.j;
import okhttp3.Cache;
import okhttp3.Handshake;
import okhttp3.Headers;
import okhttp3.Interceptor;
import okhttp3.Protocol;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okhttp3.n0.c;
import okhttp3.n0.connection.Exchange;
import okhttp3.n0.connection.RealCall;
import okhttp3.n0.http.RealInterceptorChain;

/* JADX INFO: renamed from: y.n0.e.a, reason: from Kotlin metadata */
/* JADX INFO: loaded from: classes2.dex */
@Metadata(d1 = {"\u0000(\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0004\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\b\u0002\u0018\u0000 \u000f2\u00020\u0001:\u0001\u000fB\u000f\u0012\b\u0010\u0002\u001a\u0004\u0018\u00010\u0003¢\u0006\u0002\u0010\u0004J\u001a\u0010\u0007\u001a\u00020\b2\b\u0010\t\u001a\u0004\u0018\u00010\n2\u0006\u0010\u000b\u001a\u00020\bH\u0002J\u0010\u0010\f\u001a\u00020\b2\u0006\u0010\r\u001a\u00020\u000eH\u0016R\u0016\u0010\u0002\u001a\u0004\u0018\u00010\u0003X\u0080\u0004¢\u0006\b\n\u0000\u001a\u0004\b\u0005\u0010\u0006¨\u0006\u0010"}, d2 = {"Lokhttp3/internal/cache/CacheInterceptor;", "Lokhttp3/Interceptor;", "cache", "Lokhttp3/Cache;", "(Lokhttp3/Cache;)V", "getCache$okhttp", "()Lokhttp3/Cache;", "cacheWritingResponse", "Lokhttp3/Response;", "cacheRequest", "Lokhttp3/internal/cache/CacheRequest;", "response", "intercept", "chain", "Lokhttp3/Interceptor$Chain;", "Companion", "okhttp"}, k = 1, mv = {1, 6, 0}, xi = 48)
public final class CacheInterceptor implements Interceptor {
    public static final a a = new a(null);

    /* JADX INFO: renamed from: y.n0.e.a$a */
    @Metadata(d1 = {"\u0000*\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0010\u000b\n\u0000\n\u0002\u0010\u000e\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\b\u0086\u0003\u0018\u00002\u00020\u0001B\u0007\b\u0002¢\u0006\u0002\u0010\u0002J\u0018\u0010\u0003\u001a\u00020\u00042\u0006\u0010\u0005\u001a\u00020\u00042\u0006\u0010\u0006\u001a\u00020\u0004H\u0002J\u0010\u0010\u0007\u001a\u00020\b2\u0006\u0010\t\u001a\u00020\nH\u0002J\u0010\u0010\u000b\u001a\u00020\b2\u0006\u0010\t\u001a\u00020\nH\u0002J\u0014\u0010\f\u001a\u0004\u0018\u00010\r2\b\u0010\u000e\u001a\u0004\u0018\u00010\rH\u0002¨\u0006\u000f"}, d2 = {"Lokhttp3/internal/cache/CacheInterceptor$Companion;", "", "()V", "combine", "Lokhttp3/Headers;", "cachedHeaders", "networkHeaders", "isContentSpecificHeader", "", "fieldName", "", "isEndToEnd", "stripBody", "Lokhttp3/Response;", "response", "okhttp"}, k = 1, mv = {1, 6, 0}, xi = 48)
    public static final class a {
        public a(DefaultConstructorMarker defaultConstructorMarker) {
        }

        public static final Response a(a aVar, Response response) {
            if ((response == null ? null : response.f9647r) == null) {
                return response;
            }
            Objects.requireNonNull(response);
            o.f(response, "response");
            Request request = response.f9641f;
            Protocol protocol = response.f9642m;
            int i2 = response.f9644o;
            String str = response.f9643n;
            Handshake handshake = response.f9645p;
            Headers.a aVarD = response.f9646q.d();
            Response response2 = response.f9648s;
            Response response3 = response.f9649t;
            Response response4 = response.f9650u;
            long j2 = response.f9651v;
            long j3 = response.f9652w;
            Exchange exchange = response.f9653x;
            if (!(i2 >= 0)) {
                throw new IllegalStateException(o.l("code < 0: ", Integer.valueOf(i2)).toString());
            }
            if (request == null) {
                throw new IllegalStateException("request == null".toString());
            }
            if (protocol == null) {
                throw new IllegalStateException("protocol == null".toString());
            }
            if (str != null) {
                return new Response(request, protocol, str, i2, handshake, aVarD.b(), null, response2, response3, response4, j2, j3, exchange);
            }
            throw new IllegalStateException("message == null".toString());
        }

        public final boolean b(String str) {
            return j.g("Content-Length", str, true) || j.g("Content-Encoding", str, true) || j.g("Content-Type", str, true);
        }

        public final boolean c(String str) {
            return (j.g("Connection", str, true) || j.g("Keep-Alive", str, true) || j.g("Proxy-Authenticate", str, true) || j.g(HttpHeaders.Names.PROXY_AUTHORIZATION, str, true) || j.g(HttpHeaders.Names.TE, str, true) || j.g("Trailers", str, true) || j.g(HttpHeaders.Names.TRANSFER_ENCODING, str, true) || j.g("Upgrade", str, true)) ? false : true;
        }
    }

    @Override // okhttp3.Interceptor
    public Response intercept(Interceptor.a aVar) {
        Headers headers;
        o.f(aVar, "chain");
        RealInterceptorChain realInterceptorChain = (RealInterceptorChain) aVar;
        RealCall realCall = realInterceptorChain.a;
        System.currentTimeMillis();
        Request request = realInterceptorChain.f9829e;
        o.f(request, "request");
        CacheStrategy cacheStrategy = new CacheStrategy(request, null);
        if (request.a().f9617k) {
            cacheStrategy = new CacheStrategy(null, null);
        }
        Request request2 = cacheStrategy.a;
        Response response = cacheStrategy.f9720b;
        if (realCall instanceof RealCall) {
        }
        if (request2 == null && response == null) {
            ArrayList arrayList = new ArrayList(20);
            Request request3 = realInterceptorChain.f9829e;
            o.f(request3, "request");
            Protocol protocol = Protocol.HTTP_1_1;
            o.f(protocol, "protocol");
            o.f("Unsatisfiable Request (only-if-cached)", "message");
            ResponseBody responseBody = c.f9713c;
            long jCurrentTimeMillis = System.currentTimeMillis();
            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>");
            Response response2 = new Response(request3, protocol, "Unsatisfiable Request (only-if-cached)", 504, null, new Headers((String[]) array, null), responseBody, null, null, null, -1L, jCurrentTimeMillis, null);
            o.f(realCall, "call");
            o.f(response2, "response");
            return response2;
        }
        if (request2 == null) {
            o.c(response);
            Response.a aVar2 = new Response.a(response);
            aVar2.c(a.a(a, response));
            Response responseB = aVar2.b();
            o.f(realCall, "call");
            o.f(responseB, "response");
            return responseB;
        }
        if (response != null) {
            o.f(realCall, "call");
            o.f(response, "cachedResponse");
        }
        Response responseA = ((RealInterceptorChain) aVar).a(request2);
        if (response != null) {
            boolean z2 = true;
            if (responseA.f9644o == 304) {
                Response.a aVar3 = new Response.a(response);
                a aVar4 = a;
                Headers headers2 = response.f9646q;
                Headers headers3 = responseA.f9646q;
                ArrayList arrayList2 = new ArrayList(20);
                int size = headers2.size();
                int i2 = 0;
                while (i2 < size) {
                    int i3 = i2 + 1;
                    String strC = headers2.c(i2);
                    String strE = headers2.e(i2);
                    if (j.g(HttpHeaders.Names.WARNING, strC, z2)) {
                        headers = headers2;
                        if (j.M(strE, "1", false, 2)) {
                        }
                        i2 = i3;
                        headers2 = headers;
                        z2 = true;
                    } else {
                        headers = headers2;
                    }
                    if (aVar4.b(strC) || !aVar4.c(strC) || headers3.a(strC) == null) {
                        o.f(strC, "name");
                        o.f(strE, "value");
                        arrayList2.add(strC);
                        arrayList2.add(j.Y(strE).toString());
                    }
                    i2 = i3;
                    headers2 = headers;
                    z2 = true;
                }
                int size2 = headers3.size();
                int i4 = 0;
                while (i4 < size2) {
                    int i5 = i4 + 1;
                    String strC2 = headers3.c(i4);
                    if (!aVar4.b(strC2) && aVar4.c(strC2)) {
                        String strE2 = headers3.e(i4);
                        o.f(strC2, "name");
                        o.f(strE2, "value");
                        arrayList2.add(strC2);
                        arrayList2.add(j.Y(strE2).toString());
                    }
                    i4 = i5;
                }
                Object[] array2 = arrayList2.toArray(new String[0]);
                Objects.requireNonNull(array2, "null cannot be cast to non-null type kotlin.Array<T of kotlin.collections.ArraysKt__ArraysJVMKt.toTypedArray>");
                aVar3.e(new Headers((String[]) array2, null));
                aVar3.f9663k = responseA.f9651v;
                aVar3.f9664l = responseA.f9652w;
                a aVar5 = a;
                aVar3.c(a.a(aVar5, response));
                Response responseA2 = a.a(aVar5, responseA);
                aVar3.d("networkResponse", responseA2);
                aVar3.f9660h = responseA2;
                aVar3.b();
                ResponseBody responseBody2 = responseA.f9647r;
                o.c(responseBody2);
                responseBody2.close();
                Cache cache = null;
                o.c(null);
                cache.a();
                throw null;
            }
            ResponseBody responseBody3 = response.f9647r;
            if (responseBody3 != null) {
                c.e(responseBody3);
            }
        }
        o.c(responseA);
        Response.a aVar6 = new Response.a(responseA);
        a aVar7 = a;
        aVar6.c(a.a(aVar7, response));
        Response responseA3 = a.a(aVar7, responseA);
        aVar6.d("networkResponse", responseA3);
        aVar6.f9660h = responseA3;
        return aVar6.b();
    }
}
