package io.netty.handler.codec.http;

import g.a.a.a.a;
import io.netty.util.internal.ObjectUtil;
import java.net.URI;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/* JADX INFO: loaded from: classes.dex */
public class QueryStringDecoder {
    private static final int DEFAULT_MAX_PARAMS = 1024;
    private final Charset charset;
    private final int maxParams;
    private Map<String, List<String>> params;
    private String path;
    private int pathEndIdx;
    private final String uri;

    public QueryStringDecoder(String str) {
        this(str, HttpConstants.DEFAULT_CHARSET);
    }

    public QueryStringDecoder(String str, Charset charset) {
        this(str, charset, true);
    }

    public QueryStringDecoder(String str, Charset charset, boolean z2) {
        this(str, charset, z2, 1024);
    }

    public QueryStringDecoder(String str, Charset charset, boolean z2, int i2) {
        this.uri = (String) ObjectUtil.checkNotNull(str, "uri");
        this.charset = (Charset) ObjectUtil.checkNotNull(charset, "charset");
        this.maxParams = ObjectUtil.checkPositive(i2, "maxParams");
        this.pathEndIdx = z2 ? -1 : 0;
    }

    public QueryStringDecoder(String str, boolean z2) {
        this(str, HttpConstants.DEFAULT_CHARSET, z2);
    }

    public QueryStringDecoder(URI uri) {
        this(uri, HttpConstants.DEFAULT_CHARSET);
    }

    public QueryStringDecoder(URI uri, Charset charset) {
        this(uri, charset, 1024);
    }

    public QueryStringDecoder(URI uri, Charset charset, int i2) {
        String rawPath = uri.getRawPath();
        rawPath = rawPath == null ? "" : rawPath;
        String rawQuery = uri.getRawQuery();
        this.uri = rawQuery == null ? rawPath : a.k(rawPath, '?', rawQuery);
        this.charset = (Charset) ObjectUtil.checkNotNull(charset, "charset");
        this.maxParams = ObjectUtil.checkPositive(i2, "maxParams");
        this.pathEndIdx = rawPath.length();
    }

    private static boolean addParam(String str, int i2, int i3, int i4, Map<String, List<String>> map, Charset charset) {
        if (i2 >= i4) {
            return false;
        }
        if (i3 <= i2) {
            i3 = i4 + 1;
        }
        String strDecodeComponent = decodeComponent(str, i2, i3 - 1, charset, false);
        String strDecodeComponent2 = decodeComponent(str, i3, i4, charset, false);
        List<String> arrayList = map.get(strDecodeComponent);
        if (arrayList == null) {
            arrayList = new ArrayList<>(1);
            map.put(strDecodeComponent, arrayList);
        }
        arrayList.add(strDecodeComponent2);
        return true;
    }

    public static String decodeComponent(String str) {
        return decodeComponent(str, HttpConstants.DEFAULT_CHARSET);
    }

    /* JADX WARN: Code restructure failed: missing block: B:37:0x006c, code lost:
    
        r1 = r9 - 1;
        r5.flip();
        r4.clear();
        r9 = r11.reset().decode(r5, r4, true);
     */
    /* JADX WARN: Code restructure failed: missing block: B:39:0x0080, code lost:
    
        if (r9.isUnderflow() != false) goto L41;
     */
    /* JADX WARN: Code restructure failed: missing block: B:40:0x0082, code lost:
    
        r9.throwException();
     */
    /* JADX WARN: Code restructure failed: missing block: B:41:0x0085, code lost:
    
        r9 = r11.flush(r4);
     */
    /* JADX WARN: Code restructure failed: missing block: B:42:0x008d, code lost:
    
        if (r9.isUnderflow() != false) goto L44;
     */
    /* JADX WARN: Code restructure failed: missing block: B:43:0x008f, code lost:
    
        r9.throwException();
     */
    /* JADX WARN: Code restructure failed: missing block: B:44:0x0092, code lost:
    
        r6.append(r4.flip());
     */
    /* JADX WARN: Code restructure failed: missing block: B:46:0x009b, code lost:
    
        r8 = move-exception;
     */
    /* JADX WARN: Code restructure failed: missing block: B:48:0x00a1, code lost:
    
        throw new java.lang.IllegalStateException(r8);
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    private static java.lang.String decodeComponent(java.lang.String r8, int r9, int r10, java.nio.charset.Charset r11, boolean r12) {
        /*
            int r0 = r10 - r9
            if (r0 > 0) goto L7
            java.lang.String r8 = ""
            return r8
        L7:
            r1 = r9
        L8:
            r2 = 43
            r3 = 37
            r4 = -1
            if (r1 >= r10) goto L1d
            char r5 = r8.charAt(r1)
            if (r5 == r3) goto L1e
            if (r5 != r2) goto L1a
            if (r12 != 0) goto L1a
            goto L1e
        L1a:
            int r1 = r1 + 1
            goto L8
        L1d:
            r1 = r4
        L1e:
            if (r1 != r4) goto L25
            java.lang.String r8 = r8.substring(r9, r10)
            return r8
        L25:
            java.nio.charset.CharsetDecoder r11 = io.netty.util.CharsetUtil.decoder(r11)
            int r4 = r10 - r1
            int r4 = r4 / 3
            java.nio.ByteBuffer r5 = java.nio.ByteBuffer.allocate(r4)
            java.nio.CharBuffer r4 = java.nio.CharBuffer.allocate(r4)
            java.lang.StringBuilder r6 = new java.lang.StringBuilder
            r6.<init>(r0)
            r6.append(r8, r9, r1)
        L3d:
            if (r1 >= r10) goto Lc1
            char r9 = r8.charAt(r1)
            r0 = 1
            if (r9 == r3) goto L51
            if (r9 != r2) goto L4d
            if (r12 == 0) goto L4b
            goto L4d
        L4b:
            r9 = 32
        L4d:
            r6.append(r9)
            goto L99
        L51:
            r5.clear()
        L54:
            int r9 = r1 + 3
            if (r9 > r10) goto La2
            int r1 = r1 + 1
            byte r1 = io.netty.util.internal.StringUtil.decodeHexByte(r8, r1)
            r5.put(r1)
            if (r9 >= r10) goto L6c
            char r1 = r8.charAt(r9)
            if (r1 == r3) goto L6a
            goto L6c
        L6a:
            r1 = r9
            goto L54
        L6c:
            int r1 = r9 + (-1)
            r5.flip()
            r4.clear()
            java.nio.charset.CharsetDecoder r9 = r11.reset()
            java.nio.charset.CoderResult r9 = r9.decode(r5, r4, r0)
            boolean r7 = r9.isUnderflow()     // Catch: java.nio.charset.CharacterCodingException -> L9b
            if (r7 != 0) goto L85
            r9.throwException()     // Catch: java.nio.charset.CharacterCodingException -> L9b
        L85:
            java.nio.charset.CoderResult r9 = r11.flush(r4)     // Catch: java.nio.charset.CharacterCodingException -> L9b
            boolean r7 = r9.isUnderflow()     // Catch: java.nio.charset.CharacterCodingException -> L9b
            if (r7 != 0) goto L92
            r9.throwException()     // Catch: java.nio.charset.CharacterCodingException -> L9b
        L92:
            java.nio.Buffer r9 = r4.flip()
            r6.append(r9)
        L99:
            int r1 = r1 + r0
            goto L3d
        L9b:
            r8 = move-exception
            java.lang.IllegalStateException r9 = new java.lang.IllegalStateException
            r9.<init>(r8)
            throw r9
        La2:
            java.lang.IllegalArgumentException r9 = new java.lang.IllegalArgumentException
            java.lang.StringBuilder r10 = new java.lang.StringBuilder
            r10.<init>()
            java.lang.String r11 = "unterminated escape sequence at index "
            r10.append(r11)
            r10.append(r1)
            java.lang.String r11 = " of: "
            r10.append(r11)
            r10.append(r8)
            java.lang.String r8 = r10.toString()
            r9.<init>(r8)
            throw r9
        Lc1:
            java.lang.String r8 = r6.toString()
            return r8
        */
        throw new UnsupportedOperationException("Method not decompiled: io.netty.handler.codec.http.QueryStringDecoder.decodeComponent(java.lang.String, int, int, java.nio.charset.Charset, boolean):java.lang.String");
    }

    public static String decodeComponent(String str, Charset charset) {
        return str == null ? "" : decodeComponent(str, 0, str.length(), charset, false);
    }

    private static Map<String, List<String>> decodeParams(String str, int i2, Charset charset, int i3) {
        int length = str.length();
        if (i2 >= length) {
            return Collections.emptyMap();
        }
        if (str.charAt(i2) == '?') {
            i2++;
        }
        LinkedHashMap linkedHashMap = new LinkedHashMap();
        int i4 = i2;
        int i5 = i4;
        int i6 = -1;
        while (i5 < length) {
            char cCharAt = str.charAt(i5);
            if (cCharAt == '#') {
                break;
            }
            if (cCharAt != '&' && cCharAt != ';') {
                if (cCharAt == '=') {
                    if (i4 != i5) {
                        if (i6 < i4) {
                            i6 = i5 + 1;
                        }
                    }
                }
                i5++;
            } else if (addParam(str, i4, i6, i5, linkedHashMap, charset) && i3 - 1 == 0) {
                return linkedHashMap;
            }
            i4 = i5 + 1;
            i5++;
        }
        addParam(str, i4, i6, i5, linkedHashMap, charset);
        return linkedHashMap;
    }

    private static int findPathEndIndex(String str) {
        int length = str.length();
        for (int i2 = 0; i2 < length; i2++) {
            char cCharAt = str.charAt(i2);
            if (cCharAt == '?' || cCharAt == '#') {
                return i2;
            }
        }
        return length;
    }

    private int pathEndIdx() {
        if (this.pathEndIdx == -1) {
            this.pathEndIdx = findPathEndIndex(this.uri);
        }
        return this.pathEndIdx;
    }

    public Map<String, List<String>> parameters() {
        if (this.params == null) {
            this.params = decodeParams(this.uri, pathEndIdx(), this.charset, this.maxParams);
        }
        return this.params;
    }

    public String path() {
        if (this.path == null) {
            this.path = decodeComponent(this.uri, 0, pathEndIdx(), this.charset, true);
        }
        return this.path;
    }

    public String rawPath() {
        return this.uri.substring(0, pathEndIdx());
    }

    public String rawQuery() {
        int iPathEndIdx = pathEndIdx() + 1;
        return iPathEndIdx < this.uri.length() ? this.uri.substring(iPathEndIdx) : "";
    }

    public String toString() {
        return uri();
    }

    public String uri() {
        return this.uri;
    }
}
