package com.tencent.mm.plugin.webview.modelcache;

import android.net.Uri;
import android.webkit.URLUtil;
import com.tencent.matrix.trace.core.AppMethodBeat;
import com.tencent.mm.plugin.webview.ui.tools.jsapi.i;
import com.tencent.mm.sdk.platformtools.ab;
import com.tencent.mm.sdk.platformtools.bo;
import com.tencent.ytcommon.BuildConfig;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

public final class q {
    public static boolean Ed(String str) {
        AppMethodBeat.i(6867);
        if (bo.isNullOrNil(str) || !(URLUtil.isHttpsUrl(str) || URLUtil.isHttpUrl(str))) {
            AppMethodBeat.o(6867);
            return false;
        }
        AppMethodBeat.o(6867);
        return true;
    }

    public static String aeJ(String str) {
        AppMethodBeat.i(6868);
        if (Ed(str)) {
            String aeK;
            try {
                URI uri = new URI(str);
                String toLowerCase = bo.nullAsNil(uri.getScheme()).toLowerCase();
                String toLowerCase2 = bo.nullAsNil(uri.getHost()).toLowerCase();
                if (bo.isNullOrNil(toLowerCase2)) {
                    AppMethodBeat.o(6868);
                    return null;
                }
                int port = uri.getPort() == -1 ? toLowerCase.equalsIgnoreCase("http") ? 80 : 443 : uri.getPort();
                str = toLowerCase + "://" + toLowerCase2 + ":" + port + "/" + bo.nullAsNil(uri.getPath()) + (bo.isNullOrNil(uri.getQuery()) ? BuildConfig.FLAVOR : "?" + uri.getQuery()) + (bo.isNullOrNil(uri.getFragment()) ? BuildConfig.FLAVOR : "#" + uri.getFragment());
                if (str.endsWith("/")) {
                    str = str + "/";
                }
                aeK = aeK(str);
                AppMethodBeat.o(6868);
                return aeK;
            } catch (URISyntaxException e) {
                aeK = str;
                ab.e("MicroMsg.WebViewCacheUtils", "getFormattedHttpURL URISyntaxException : %s", new Object[]{e.getMessage()});
                AppMethodBeat.o(6868);
                return aeK;
            } catch (Exception e2) {
                aeK = str;
                ab.e("MicroMsg.WebViewCacheUtils", "getFormattedHttpURL normal : %s", new Object[]{e2.getMessage()});
                AppMethodBeat.o(6868);
                return aeK;
            }
        }
        AppMethodBeat.o(6868);
        return null;
    }

    private static String aeK(String str) {
        AppMethodBeat.i(6869);
        Uri parse = Uri.parse(str);
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(parse.getScheme()).append("://");
        stringBuilder.append(parse.getHost()).append(":").append(parse.getPort());
        if (bo.ek(parse.getPathSegments())) {
            stringBuilder.append("/");
        } else {
            for (String append : parse.getPathSegments()) {
                stringBuilder.append("/").append(append);
            }
        }
        if (!bo.isNullOrNil(parse.getQuery())) {
            stringBuilder.append("?").append(parse.getQuery());
        }
        if (!bo.isNullOrNil(parse.getFragment())) {
            stringBuilder.append("#").append(parse.getFragment());
        }
        if (str.endsWith("/")) {
            stringBuilder.append("/");
        }
        String append2 = stringBuilder.toString();
        AppMethodBeat.o(6869);
        return append2;
    }

    public static String aeL(String str) {
        AppMethodBeat.i(6870);
        String aeJ = aeJ(str);
        if (bo.isNullOrNil(aeJ)) {
            AppMethodBeat.o(6870);
            return null;
        }
        aeJ = Uri.parse(aeJ).getHost();
        AppMethodBeat.o(6870);
        return aeJ;
    }

    public static String aeM(String str) {
        AppMethodBeat.i(6871);
        String aeN = aeN(str);
        if (bo.isNullOrNil(aeN)) {
            AppMethodBeat.o(6871);
            return null;
        }
        aeN = aeN.replaceAll("http://", BuildConfig.FLAVOR).replaceAll("https://", BuildConfig.FLAVOR);
        AppMethodBeat.o(6871);
        return aeN;
    }

    public static String aeN(String str) {
        AppMethodBeat.i(6872);
        String aeJ = aeJ(str);
        if (bo.isNullOrNil(aeJ)) {
            ab.e("MicroMsg.WebViewCacheUtils", "evaluateResURLWithScheme, original url is invalid = %s", new Object[]{str});
            AppMethodBeat.o(6872);
            return null;
        }
        Uri parse = Uri.parse(aeJ);
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(parse.getScheme()).append("://").append(parse.getHost()).append(":").append(parse.getPort());
        if (!bo.ek(parse.getPathSegments())) {
            for (String aeJ2 : parse.getPathSegments()) {
                stringBuilder.append("/").append(bo.nullAsNil(aeJ2));
            }
        }
        if (str.endsWith("/")) {
            stringBuilder.append("/");
        }
        aeJ2 = stringBuilder.toString();
        AppMethodBeat.o(6872);
        return aeJ2;
    }

    public static byte[] aeO(String str) {
        Exception e;
        Throwable th;
        AppMethodBeat.i(6873);
        Closeable byteArrayOutputStream;
        Closeable inputStream;
        try {
            byteArrayOutputStream = new ByteArrayOutputStream();
            try {
                HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(str).openConnection();
                httpURLConnection.setRequestMethod("GET");
                int responseCode = httpURLConnection.getResponseCode();
                int contentLength = httpURLConnection.getContentLength();
                ab.d("MicroMsg.WebViewCacheUtils", "getBytesFromURL, url = %s, statusCode = %d, contentLength = %d", new Object[]{str, Integer.valueOf(responseCode), Integer.valueOf(contentLength)});
                inputStream = httpURLConnection.getInputStream();
                if (inputStream != null) {
                    try {
                        byte[] bArr = new byte[1024];
                        while (true) {
                            contentLength = inputStream.read(bArr);
                            if (contentLength != -1) {
                                byteArrayOutputStream.write(bArr, 0, contentLength);
                            } else {
                                bArr = byteArrayOutputStream.toByteArray();
                                bo.b(inputStream);
                                bo.b(byteArrayOutputStream);
                                AppMethodBeat.o(6873);
                                return bArr;
                            }
                        }
                    } catch (Exception e2) {
                        e = e2;
                        try {
                            ab.e("MicroMsg.WebViewCacheUtils", "getBytesFromURL, url = %s, e = %s", new Object[]{str, e});
                            bo.b(inputStream);
                            bo.b(byteArrayOutputStream);
                            AppMethodBeat.o(6873);
                            return null;
                        } catch (Throwable th2) {
                            th = th2;
                            bo.b(inputStream);
                            bo.b(byteArrayOutputStream);
                            AppMethodBeat.o(6873);
                            throw th;
                        }
                    }
                }
                bo.b(inputStream);
                bo.b(byteArrayOutputStream);
                AppMethodBeat.o(6873);
                return null;
            } catch (Exception e3) {
                e = e3;
                inputStream = null;
            } catch (Throwable th3) {
                th = th3;
                inputStream = null;
                bo.b(inputStream);
                bo.b(byteArrayOutputStream);
                AppMethodBeat.o(6873);
                throw th;
            }
        } catch (Exception e4) {
            e = e4;
            byteArrayOutputStream = null;
            inputStream = null;
            ab.e("MicroMsg.WebViewCacheUtils", "getBytesFromURL, url = %s, e = %s", new Object[]{str, e});
            bo.b(inputStream);
            bo.b(byteArrayOutputStream);
            AppMethodBeat.o(6873);
            return null;
        } catch (Throwable th4) {
            th = th4;
            byteArrayOutputStream = null;
            inputStream = null;
            bo.b(inputStream);
            bo.b(byteArrayOutputStream);
            AppMethodBeat.o(6873);
            throw th;
        }
    }

    /* JADX WARNING: Unknown top exception splitter block from list: {B:16:0x0041=Splitter:B:16:0x0041, B:11:0x0026=Splitter:B:11:0x0026} */
    public static java.lang.String aeP(java.lang.String r8) {
        /*
        r0 = 0;
        r7 = 6874; // 0x1ada float:9.633E-42 double:3.396E-320;
        com.tencent.matrix.trace.core.AppMethodBeat.i(r7);
        r1 = com.tencent.mm.vfs.e.ct(r8);
        if (r1 != 0) goto L_0x0010;
    L_0x000c:
        com.tencent.matrix.trace.core.AppMethodBeat.o(r7);
    L_0x000f:
        return r0;
    L_0x0010:
        r2 = com.tencent.mm.vfs.e.asZ(r8);	 Catch:{ FileNotFoundException -> 0x0024, Exception -> 0x003f, all -> 0x005a }
        r1 = (int) r2;	 Catch:{ FileNotFoundException -> 0x0024, Exception -> 0x003f, all -> 0x005a }
        r2 = com.tencent.mm.vfs.e.openRead(r8);	 Catch:{ FileNotFoundException -> 0x0024, Exception -> 0x003f, all -> 0x005a }
        r0 = com.tencent.mm.a.g.b(r2, r1);	 Catch:{ FileNotFoundException -> 0x0068, Exception -> 0x0066 }
        com.tencent.mm.sdk.platformtools.bo.b(r2);
        com.tencent.matrix.trace.core.AppMethodBeat.o(r7);
        goto L_0x000f;
    L_0x0024:
        r1 = move-exception;
        r2 = r0;
    L_0x0026:
        r3 = "MicroMsg.WebViewCacheUtils";
        r4 = "getContentMd5, localPath = %s, exception = %s";
        r5 = 2;
        r5 = new java.lang.Object[r5];	 Catch:{ all -> 0x0063 }
        r6 = 0;
        r5[r6] = r8;	 Catch:{ all -> 0x0063 }
        r6 = 1;
        r5[r6] = r1;	 Catch:{ all -> 0x0063 }
        com.tencent.mm.sdk.platformtools.ab.e(r3, r4, r5);	 Catch:{ all -> 0x0063 }
        com.tencent.mm.sdk.platformtools.bo.b(r2);
        com.tencent.matrix.trace.core.AppMethodBeat.o(r7);
        goto L_0x000f;
    L_0x003f:
        r1 = move-exception;
        r2 = r0;
    L_0x0041:
        r3 = "MicroMsg.WebViewCacheUtils";
        r4 = "getContentMd5, localPath = %s, exception = %s";
        r5 = 2;
        r5 = new java.lang.Object[r5];	 Catch:{ all -> 0x0063 }
        r6 = 0;
        r5[r6] = r8;	 Catch:{ all -> 0x0063 }
        r6 = 1;
        r5[r6] = r1;	 Catch:{ all -> 0x0063 }
        com.tencent.mm.sdk.platformtools.ab.e(r3, r4, r5);	 Catch:{ all -> 0x0063 }
        com.tencent.mm.sdk.platformtools.bo.b(r2);
        com.tencent.matrix.trace.core.AppMethodBeat.o(r7);
        goto L_0x000f;
    L_0x005a:
        r1 = move-exception;
        r2 = r0;
    L_0x005c:
        com.tencent.mm.sdk.platformtools.bo.b(r2);
        com.tencent.matrix.trace.core.AppMethodBeat.o(r7);
        throw r1;
    L_0x0063:
        r0 = move-exception;
        r1 = r0;
        goto L_0x005c;
    L_0x0066:
        r1 = move-exception;
        goto L_0x0041;
    L_0x0068:
        r1 = move-exception;
        goto L_0x0026;
        */
        throw new UnsupportedOperationException("Method not decompiled: com.tencent.mm.plugin.webview.modelcache.q.aeP(java.lang.String):java.lang.String");
    }

    public static int a(i iVar) {
        AppMethodBeat.i(6875);
        if (!bo.nullAsNil(iVar.uIJ).equals("cache")) {
            AppMethodBeat.o(6875);
            return -1;
        } else if (Boolean.parseBoolean((String) iVar.puc.get("async")) && !bo.isNullOrNil((String) iVar.puc.get("src"))) {
            AppMethodBeat.o(6875);
            return 2;
        } else if (bo.isNullOrNil((String) iVar.puc.get("resourceList"))) {
            AppMethodBeat.o(6875);
            return -1;
        } else {
            AppMethodBeat.o(6875);
            return 1;
        }
    }
}
