package io.netty.handler.codec.http;

import io.netty.handler.codec.DateFormatter;
import io.netty.handler.codec.http.cookie.CookieHeaderNames;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

/* JADX INFO: loaded from: classes.dex */
@Deprecated
public final class CookieDecoder {
    private static final String COMMENT = "Comment";
    private static final String COMMENTURL = "CommentURL";
    private static final String DISCARD = "Discard";
    private static final String PORT = "Port";
    private static final String VERSION = "Version";
    private final InternalLogger logger = InternalLoggerFactory.getInstance((Class<?>) CookieDecoder.class);
    private final boolean strict;
    private static final CookieDecoder STRICT = new CookieDecoder(true);
    private static final CookieDecoder LAX = new CookieDecoder(false);

    private CookieDecoder(boolean z2) {
        this.strict = z2;
    }

    public static Set<Cookie> decode(String str) {
        return decode(str, true);
    }

    public static Set<Cookie> decode(String str, boolean z2) {
        return (z2 ? STRICT : LAX).doDecode(str);
    }

    private Set<Cookie> doDecode(String str) {
        int i2;
        int i3;
        ArrayList arrayList;
        ArrayList arrayList2;
        ArrayList arrayList3 = new ArrayList(8);
        ArrayList arrayList4 = new ArrayList(8);
        extractKeyValuePairs(str, arrayList3, arrayList4);
        if (arrayList3.isEmpty()) {
            return Collections.emptySet();
        }
        boolean z2 = false;
        if (((String) arrayList3.get(0)).equalsIgnoreCase(VERSION)) {
            try {
                i2 = Integer.parseInt((String) arrayList4.get(0));
            } catch (NumberFormatException unused) {
                i2 = 0;
            }
            i3 = 1;
        } else {
            i2 = 0;
            i3 = 0;
        }
        if (arrayList3.size() <= i3) {
            return Collections.emptySet();
        }
        TreeSet treeSet = new TreeSet();
        while (i3 < arrayList3.size()) {
            String str2 = (String) arrayList3.get(i3);
            String str3 = (String) arrayList4.get(i3);
            if (str3 == null) {
                str3 = "";
            }
            DefaultCookie defaultCookieInitCookie = initCookie(str2, str3);
            if (defaultCookieInitCookie == null) {
                break;
            }
            long j2 = Long.MIN_VALUE;
            ArrayList arrayList5 = new ArrayList(2);
            int i4 = i3 + 1;
            String str4 = null;
            boolean z3 = z2;
            boolean z4 = z3;
            TreeSet treeSet2 = treeSet;
            String str5 = null;
            String str6 = null;
            boolean z5 = z4;
            String str7 = null;
            while (true) {
                if (i4 >= arrayList3.size()) {
                    arrayList = arrayList3;
                    arrayList2 = arrayList4;
                    break;
                }
                String str8 = (String) arrayList3.get(i4);
                arrayList = arrayList3;
                String str9 = (String) arrayList4.get(i4);
                arrayList2 = arrayList4;
                if (!DISCARD.equalsIgnoreCase(str8)) {
                    if (!CookieHeaderNames.SECURE.equalsIgnoreCase(str8)) {
                        if (!CookieHeaderNames.HTTPONLY.equalsIgnoreCase(str8)) {
                            if (!COMMENT.equalsIgnoreCase(str8)) {
                                if (!COMMENTURL.equalsIgnoreCase(str8)) {
                                    if (!CookieHeaderNames.DOMAIN.equalsIgnoreCase(str8)) {
                                        if (!CookieHeaderNames.PATH.equalsIgnoreCase(str8)) {
                                            if (!"Expires".equalsIgnoreCase(str8)) {
                                                if (!CookieHeaderNames.MAX_AGE.equalsIgnoreCase(str8)) {
                                                    if (!VERSION.equalsIgnoreCase(str8)) {
                                                        if (!PORT.equalsIgnoreCase(str8)) {
                                                            break;
                                                        }
                                                        String[] strArrSplit = str9.split(",");
                                                        int length = strArrSplit.length;
                                                        int i5 = 0;
                                                        while (i5 < length) {
                                                            String[] strArr = strArrSplit;
                                                            try {
                                                                arrayList5.add(Integer.valueOf(strArrSplit[i5]));
                                                            } catch (NumberFormatException unused2) {
                                                            }
                                                            i5++;
                                                            strArrSplit = strArr;
                                                        }
                                                    } else {
                                                        i2 = Integer.parseInt(str9);
                                                    }
                                                } else {
                                                    j2 = Integer.parseInt(str9);
                                                }
                                            } else {
                                                Date httpDate = DateFormatter.parseHttpDate(str9);
                                                if (httpDate != null) {
                                                    long time = httpDate.getTime() - System.currentTimeMillis();
                                                    j2 = (time / 1000) + ((long) (time % 1000 != 0 ? 1 : 0));
                                                }
                                            }
                                        } else {
                                            str5 = str9;
                                        }
                                    } else {
                                        str7 = str9;
                                    }
                                } else {
                                    str6 = str9;
                                }
                            } else {
                                str4 = str9;
                            }
                        } else {
                            z3 = true;
                        }
                    } else {
                        z5 = true;
                    }
                } else {
                    z4 = true;
                }
                i4++;
                i3++;
                arrayList4 = arrayList2;
                arrayList3 = arrayList;
            }
            defaultCookieInitCookie.setVersion(i2);
            defaultCookieInitCookie.setMaxAge(j2);
            defaultCookieInitCookie.setPath(str5);
            defaultCookieInitCookie.setDomain(str7);
            defaultCookieInitCookie.setSecure(z5);
            defaultCookieInitCookie.setHttpOnly(z3);
            if (i2 > 0) {
                defaultCookieInitCookie.setComment(str4);
            }
            if (i2 > 1) {
                defaultCookieInitCookie.setCommentUrl(str6);
                defaultCookieInitCookie.setPorts(arrayList5);
                defaultCookieInitCookie.setDiscard(z4);
            }
            treeSet2.add(defaultCookieInitCookie);
            i3++;
            treeSet = treeSet2;
            arrayList4 = arrayList2;
            arrayList3 = arrayList;
            z2 = false;
        }
        return treeSet;
    }

    private static void extractKeyValuePairs(String str, List<String> list, List<String> list2) {
        String strSubstring;
        String string;
        String string2;
        String strSubstring2;
        int length = str.length();
        int iIndexOf = 0;
        while (iIndexOf != length) {
            char cCharAt = str.charAt(iIndexOf);
            if (cCharAt != ' ' && cCharAt != ',' && cCharAt != ';') {
                switch (cCharAt) {
                    case '\t':
                    case '\n':
                    case 11:
                    case '\f':
                    case '\r':
                        break;
                    default:
                        while (iIndexOf != length) {
                            if (str.charAt(iIndexOf) != '$') {
                                String str2 = null;
                                if (iIndexOf == length) {
                                    string = null;
                                } else {
                                    int i2 = iIndexOf;
                                    while (true) {
                                        char cCharAt2 = str.charAt(i2);
                                        if (cCharAt2 == ';') {
                                            strSubstring = str.substring(iIndexOf, i2);
                                        } else if (cCharAt2 != '=') {
                                            i2++;
                                            if (i2 == length) {
                                                strSubstring = str.substring(iIndexOf);
                                            }
                                        } else {
                                            String strSubstring3 = str.substring(iIndexOf, i2);
                                            int i3 = i2 + 1;
                                            if (i3 == length) {
                                                string2 = "";
                                            } else {
                                                char cCharAt3 = str.charAt(i3);
                                                if (cCharAt3 == '\"' || cCharAt3 == '\'') {
                                                    StringBuilder sb = new StringBuilder(str.length() - i3);
                                                    i3++;
                                                    while (true) {
                                                        boolean z2 = false;
                                                        while (i3 != length) {
                                                            if (z2) {
                                                                int i4 = i3 + 1;
                                                                char cCharAt4 = str.charAt(i3);
                                                                if (cCharAt4 == '\"' || cCharAt4 == '\'' || cCharAt4 == '\\') {
                                                                    sb.setCharAt(sb.length() - 1, cCharAt4);
                                                                } else {
                                                                    sb.append(cCharAt4);
                                                                }
                                                                i3 = i4;
                                                            } else {
                                                                int i5 = i3 + 1;
                                                                char cCharAt5 = str.charAt(i3);
                                                                if (cCharAt5 == cCharAt3) {
                                                                    string = sb.toString();
                                                                    str2 = strSubstring3;
                                                                    iIndexOf = i5;
                                                                } else {
                                                                    sb.append(cCharAt5);
                                                                    if (cCharAt5 == '\\') {
                                                                        z2 = true;
                                                                    }
                                                                    i3 = i5;
                                                                }
                                                            }
                                                        }
                                                        string2 = sb.toString();
                                                    }
                                                } else {
                                                    iIndexOf = str.indexOf(59, i3);
                                                    if (iIndexOf > 0) {
                                                        strSubstring2 = str.substring(i3, iIndexOf);
                                                    } else {
                                                        strSubstring2 = str.substring(i3);
                                                        iIndexOf = length;
                                                    }
                                                    string = strSubstring2;
                                                    str2 = strSubstring3;
                                                }
                                            }
                                            string = string2;
                                            iIndexOf = i3;
                                            str2 = strSubstring3;
                                        }
                                    }
                                    string = null;
                                    str2 = strSubstring;
                                    iIndexOf = i2;
                                }
                                list.add(str2);
                                list2.add(string);
                                continue;
                            } else {
                                iIndexOf++;
                            }
                            break;
                        }
                        return;
                }
            }
            iIndexOf++;
        }
    }

    private DefaultCookie initCookie(String str, String str2) {
        int iFirstInvalidCookieValueOctet;
        int iFirstInvalidCookieNameOctet;
        if (str == null || str.length() == 0) {
            this.logger.debug("Skipping cookie with null name");
            return null;
        }
        if (str2 == null) {
            this.logger.debug("Skipping cookie with null value");
            return null;
        }
        CharSequence charSequenceUnwrapValue = CookieUtil.unwrapValue(str2);
        if (charSequenceUnwrapValue == null) {
            this.logger.debug("Skipping cookie because starting quotes are not properly balanced in '{}'", charSequenceUnwrapValue);
            return null;
        }
        if (this.strict && (iFirstInvalidCookieNameOctet = CookieUtil.firstInvalidCookieNameOctet(str)) >= 0) {
            if (this.logger.isDebugEnabled()) {
                this.logger.debug("Skipping cookie because name '{}' contains invalid char '{}'", str, Character.valueOf(str.charAt(iFirstInvalidCookieNameOctet)));
            }
            return null;
        }
        boolean z2 = charSequenceUnwrapValue.length() != str2.length();
        if (!this.strict || (iFirstInvalidCookieValueOctet = CookieUtil.firstInvalidCookieValueOctet(charSequenceUnwrapValue)) < 0) {
            DefaultCookie defaultCookie = new DefaultCookie(str, charSequenceUnwrapValue.toString());
            defaultCookie.setWrap(z2);
            return defaultCookie;
        }
        if (this.logger.isDebugEnabled()) {
            this.logger.debug("Skipping cookie because value '{}' contains invalid char '{}'", charSequenceUnwrapValue, Character.valueOf(charSequenceUnwrapValue.charAt(iFirstInvalidCookieValueOctet)));
        }
        return null;
    }
}
