package okhttp3.internal.http;

import S4.a;
import S4.p;
import S4.w;
import androidx.navigation.C1134i;
import com.google.firebase.sessions.settings.RemoteSettings;
import java.io.EOFException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import kotlin.jvm.internal.o;
import okhttp3.Challenge;
import okhttp3.Cookie;
import okhttp3.CookieJar;
import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.Response;
import okhttp3.internal.HostnamesKt;
import okhttp3.internal.Util;
import okhttp3.internal.publicsuffix.PublicSuffixDatabase;
import p5.C2740j;
import p5.C2743m;
import x4.y;
import x4.z;

/* JADX INFO: loaded from: classes3.dex */
public final class HttpHeaders {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public static final C2743m f22381a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static final C2743m f22382b;

    static {
        C2743m c2743m = C2743m.f23101d;
        f22381a = C1134i.n("\"\\");
        f22382b = C1134i.n("\t ,=");
    }

    public static final boolean a(Response response) {
        if (o.c(response.f22140a.f22126b, "HEAD")) {
            return false;
        }
        int i6 = response.f22143d;
        return (((i6 >= 100 && i6 < 200) || i6 == 204 || i6 == 304) && Util.j(response) == -1 && !"chunked".equalsIgnoreCase(Response.b("Transfer-Encoding", response))) ? false : true;
    }

    /* JADX WARN: Code restructure failed: missing block: B:75:0x0106, code lost:
    
        continue;
     */
    /* JADX WARN: Code restructure failed: missing block: B:76:0x0106, code lost:
    
        continue;
     */
    /* JADX WARN: Removed duplicated region for block: B:27:0x0082  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static final void b(C2740j c2740j, ArrayList arrayList) throws EOFException {
        String strC;
        int iU;
        LinkedHashMap linkedHashMap;
        String strC2;
        while (true) {
            String strC3 = null;
            while (true) {
                if (strC3 == null) {
                    e(c2740j);
                    strC3 = c(c2740j);
                    if (strC3 == null) {
                        return;
                    }
                }
                boolean zE = e(c2740j);
                strC = c(c2740j);
                if (strC == null) {
                    if (c2740j.d()) {
                        arrayList.add(new Challenge(strC3, z.f26066a));
                        return;
                    }
                    return;
                }
                iU = Util.u(c2740j);
                boolean zE2 = e(c2740j);
                if (zE || (!zE2 && !c2740j.d())) {
                    linkedHashMap = new LinkedHashMap();
                    int iU2 = Util.u(c2740j) + iU;
                    while (true) {
                        if (strC == null) {
                            strC = c(c2740j);
                            if (!e(c2740j)) {
                                iU2 = Util.u(c2740j);
                                if (iU2 == 0) {
                                    if (iU2 > 1 || e(c2740j)) {
                                        return;
                                    }
                                    if (c2740j.d() || c2740j.f(0L) != 34) {
                                        strC2 = c(c2740j);
                                    } else {
                                        if (c2740j.y() != 34) {
                                            throw new IllegalArgumentException("Failed requirement.");
                                        }
                                        C2740j c2740j2 = new C2740j();
                                        while (true) {
                                            long jI = c2740j.i(f22381a);
                                            if (jI == -1) {
                                                break;
                                            }
                                            if (c2740j.f(jI) == 34) {
                                                c2740j2.write(c2740j, jI);
                                                c2740j.y();
                                                strC2 = c2740j2.H();
                                                break;
                                            } else {
                                                if (c2740j.f23100b == jI + 1) {
                                                    break;
                                                }
                                                c2740j2.write(c2740j, jI);
                                                c2740j.y();
                                                c2740j2.write(c2740j, 1L);
                                            }
                                        }
                                        strC2 = null;
                                    }
                                    if (strC2 == null || ((String) linkedHashMap.put(strC, strC2)) != null) {
                                        return;
                                    }
                                    if (!e(c2740j) && !c2740j.d()) {
                                        return;
                                    } else {
                                        strC = null;
                                    }
                                }
                            }
                        } else if (iU2 == 0) {
                            break;
                        }
                    }
                }
                arrayList.add(new Challenge(strC3, linkedHashMap));
                strC3 = strC;
            }
            StringBuilder sbT = Z0.o.t(strC);
            sbT.append(w.w0(iU, "="));
            Map mapSingletonMap = Collections.singletonMap(null, sbT.toString());
            o.g(mapSingletonMap, "singletonMap<String, Str…ek + \"=\".repeat(eqCount))");
            arrayList.add(new Challenge(strC3, mapSingletonMap));
        }
    }

    public static final String c(C2740j c2740j) {
        long jI = c2740j.i(f22382b);
        if (jI == -1) {
            jI = c2740j.f23100b;
        }
        if (jI != 0) {
            return c2740j.G(jI, a.f6257a);
        }
        return null;
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:97:0x01cd  */
    /* JADX WARN: Removed duplicated region for block: B:9:0x0053  */
    /* JADX WARN: Type inference failed for: r25v3 */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static final void d(CookieJar cookieJar, HttpUrl url, Headers headers) {
        List listUnmodifiableList;
        Cookie cookie;
        int i6;
        Cookie cookie2;
        o.h(cookieJar, "<this>");
        o.h(url, "url");
        o.h(headers, "headers");
        if (cookieJar == CookieJar.f21999a) {
            return;
        }
        Cookie.f21986j.getClass();
        List listM = headers.m("Set-Cookie");
        int size = listM.size();
        int i7 = 0;
        int i8 = 0;
        ArrayList arrayList = null;
        while (i8 < size) {
            String setCookie = (String) listM.get(i8);
            o.h(setCookie, "setCookie");
            long jCurrentTimeMillis = System.currentTimeMillis();
            byte[] bArr = Util.f22179a;
            char c6 = ';';
            int iF = Util.f(setCookie, ';', i7, setCookie.length());
            char c7 = '=';
            int iF2 = Util.f(setCookie, '=', i7, iF);
            if (iF2 == iF) {
                i6 = i7;
                cookie2 = null;
            } else {
                String strA = Util.A(i7, iF2, setCookie);
                if (strA.length() != 0) {
                    if (Util.l(strA) == -1) {
                        String strA2 = Util.A(iF2 + 1, iF, setCookie);
                        if (Util.l(strA2) == -1) {
                            int i9 = iF + 1;
                            int length = setCookie.length();
                            long j4 = 253402300799999L;
                            int i10 = i7;
                            int i11 = i10;
                            boolean z6 = i11 == true ? 1 : 0;
                            long jB = 253402300799999L;
                            boolean z7 = true;
                            String str = null;
                            String str2 = null;
                            long j6 = -1;
                            boolean z8 = i11;
                            while (true) {
                                if (i9 < length) {
                                    int iF3 = Util.f(setCookie, c6, i9, length);
                                    int iF4 = Util.f(setCookie, c7, i9, iF3);
                                    String strA3 = Util.A(i9, iF4, setCookie);
                                    String strA4 = iF4 < iF3 ? Util.A(iF4 + 1, iF3, setCookie) : "";
                                    if (strA3.equalsIgnoreCase("expires")) {
                                        try {
                                            jB = Cookie.Companion.b(strA4.length(), strA4);
                                            z8 = 1;
                                        } catch (NumberFormatException | IllegalArgumentException unused) {
                                        }
                                    } else if (strA3.equalsIgnoreCase("max-age")) {
                                        try {
                                            long j7 = Long.parseLong(strA4);
                                            j6 = j7 <= 0 ? Long.MIN_VALUE : j7;
                                        } catch (NumberFormatException e6) {
                                            Pattern patternCompile = Pattern.compile("-?\\d+");
                                            o.g(patternCompile, "compile(...)");
                                            if (!patternCompile.matcher(strA4).matches()) {
                                                throw e6;
                                            }
                                            j6 = w.A0(strA4, "-", false) ? Long.MIN_VALUE : Long.MAX_VALUE;
                                        }
                                        z8 = 1;
                                    } else if (strA3.equalsIgnoreCase("domain")) {
                                        if (w.s0(strA4, ".", false)) {
                                            throw new IllegalArgumentException("Failed requirement.");
                                        }
                                        String strB = HostnamesKt.b(p.X0(strA4, "."));
                                        if (strB == null) {
                                            throw new IllegalArgumentException();
                                        }
                                        str = strB;
                                        z7 = false;
                                    } else if (strA3.equalsIgnoreCase("path")) {
                                        str2 = strA4;
                                    } else if (strA3.equalsIgnoreCase("secure")) {
                                        z6 = true;
                                    } else if (strA3.equalsIgnoreCase("httponly")) {
                                        i10 = 1;
                                    }
                                    i9 = iF3 + 1;
                                    c6 = ';';
                                    c7 = '=';
                                    z8 = z8;
                                    z6 = z6;
                                } else {
                                    if (j6 == Long.MIN_VALUE) {
                                        j4 = Long.MIN_VALUE;
                                    } else if (j6 != -1) {
                                        long j8 = jCurrentTimeMillis + (j6 <= 9223372036854775L ? j6 * ((long) 1000) : Long.MAX_VALUE);
                                        if (j8 >= jCurrentTimeMillis && j8 <= 253402300799999L) {
                                            j4 = j8;
                                        }
                                    } else {
                                        j4 = jB;
                                    }
                                    String str3 = url.f22027d;
                                    if (str == null) {
                                        str = str3;
                                    } else if (!o.c(str3, str) && (!w.s0(str3, str, false) || str3.charAt((str3.length() - str.length()) - 1) != '.' || Util.f22184f.d(str3))) {
                                        i6 = 0;
                                        cookie = null;
                                        cookie2 = cookie;
                                    }
                                    if (str3.length() != str.length()) {
                                        PublicSuffixDatabase.f22598e.getClass();
                                        if (PublicSuffixDatabase.f22601h.a(str) == null) {
                                        }
                                        cookie2 = cookie;
                                    }
                                    String strSubstring = RemoteSettings.FORWARD_SLASH_STRING;
                                    i6 = 0;
                                    if (str2 == null || !w.A0(str2, RemoteSettings.FORWARD_SLASH_STRING, false)) {
                                        String strB2 = url.b();
                                        int iT0 = p.T0(strB2, '/', 0, 6);
                                        if (iT0 != 0) {
                                            strSubstring = strB2.substring(0, iT0);
                                            o.g(strSubstring, "this as java.lang.String…ing(startIndex, endIndex)");
                                        }
                                        str2 = strSubstring;
                                    }
                                    cookie = new Cookie(strA, strA2, j4, str, str2, z6 ? 1 : 0, i10, z8, z7);
                                    cookie2 = cookie;
                                }
                            }
                        }
                    }
                }
                i6 = i7;
                cookie2 = null;
            }
            if (cookie2 != null) {
                if (arrayList == null) {
                    arrayList = new ArrayList();
                }
                arrayList.add(cookie2);
            }
            i8++;
            i7 = i6;
        }
        if (arrayList != null) {
            listUnmodifiableList = Collections.unmodifiableList(arrayList);
            o.g(listUnmodifiableList, "{\n        Collections.un…ableList(cookies)\n      }");
        } else {
            listUnmodifiableList = y.f26065a;
        }
        if (listUnmodifiableList.isEmpty()) {
            return;
        }
        cookieJar.a(url, listUnmodifiableList);
    }

    public static final boolean e(C2740j c2740j) throws EOFException {
        boolean z6 = false;
        while (!c2740j.d()) {
            byte bF = c2740j.f(0L);
            if (bF == 44) {
                c2740j.y();
                z6 = true;
            } else {
                if (bF != 32 && bF != 9) {
                    break;
                }
                c2740j.y();
            }
        }
        return z6;
    }
}
