翰林优商.apk(点击下载) / e.java


package com.sigmob.sdk.base;

import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;
import android.webkit.CookieSyncManager;
import android.webkit.URLUtil;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.czhj.sdk.common.network.CustomSSLSocketFactory;
import com.czhj.sdk.logger.SigmobLog;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.CookieManager;
import java.net.HttpCookie;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.text.MessageFormat;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;

public class e extends WebViewClient {
    static final String a = "Set-Cookie";
    static final String b = "Referer";
    private static final String c = "mraid.js";
    private static final String d = "Cookie";
    private CookieManager e = new CookieManager();
    private boolean f = false;
    private String g;

    private WebResourceResponse a(String str, String str2, Map<String, String> map) {
        String str3 = "text/html";
        if (!(URLUtil.isHttpUrl(str) || URLUtil.isHttpsUrl(str))) {
            return null;
        }
        try {
            String host = Uri.parse(str).getHost();
            if (host != null) {
                if (!host.equals("127.0.0.1")) {
                    HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(str).openConnection();
                    httpURLConnection.setRequestMethod(str2);
                    if (URLUtil.isHttpsUrl(str)) {
                        HttpsURLConnection httpsURLConnection = (HttpsURLConnection) httpURLConnection;
                        httpsURLConnection.setHostnameVerifier(new HostnameVerifier() {
                            /* class com.sigmob.sdk.base.e.AnonymousClass1 */

                            public boolean verify(String str, SSLSession sSLSession) {
                                return true;
                            }
                        });
                        httpsURLConnection.setSSLSocketFactory(CustomSSLSocketFactory.getForceValid());
                    }
                    if (map != null && map.size() > 0) {
                        for (Map.Entry<String, String> entry : map.entrySet()) {
                            httpURLConnection.setRequestProperty(entry.getKey(), entry.getValue());
                        }
                    }
                    String cookie = android.webkit.CookieManager.getInstance().getCookie(str);
                    if (!TextUtils.isEmpty(cookie)) {
                        for (String str4 : cookie.split(";")) {
                            httpURLConnection.addRequestProperty(d.toLowerCase(), str4.replace(" ", ""));
                        }
                    }
                    httpURLConnection.connect();
                    String contentType = httpURLConnection.getContentType();
                    String contentEncoding = httpURLConnection.getContentEncoding();
                    InputStream inputStream = httpURLConnection.getInputStream();
                    a(httpURLConnection, this.e);
                    if (contentType.contains(str3)) {
                        inputStream = a(inputStream, contentEncoding);
                    } else {
                        str3 = contentType;
                    }
                    return new WebResourceResponse(str3, contentEncoding, inputStream);
                }
            }
            return null;
        } catch (Exception e2) {
            e2.printStackTrace();
            return null;
        }
    }

    private InputStream a(InputStream inputStream, String str) throws IOException {
        byte[] bArr = new byte[1024];
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        while (true) {
            int read = inputStream.read(bArr);
            if (read == -1) {
                break;
            }
            byteArrayOutputStream.write(bArr, 0, read);
        }
        String str2 = new String(byteArrayOutputStream.toByteArray());
        if (str == null) {
            str = Charset.defaultCharset().displayName();
        }
        return new ByteArrayInputStream(str2.getBytes(str));
    }

    public static void a(HttpURLConnection httpURLConnection, CookieManager cookieManager) {
        if (cookieManager != null && httpURLConnection != null) {
            Map<String, List<String>> headerFields = httpURLConnection.getHeaderFields();
            SigmobLog.d("headerFields : " + headerFields.toString());
            List<String> list = headerFields.get(a);
            if (list != null) {
                for (String str : list) {
                    try {
                        List<HttpCookie> parse = HttpCookie.parse(str);
                        if (parse != null) {
                            SigmobLog.d("{0} -- Reading Cookies from the response :" + httpURLConnection.getURL().toString());
                            SigmobLog.d("{0} -- Reading Cookies from the response :" + parse.get(0));
                            for (HttpCookie httpCookie : parse) {
                                SigmobLog.d(httpCookie.toString());
                                android.webkit.CookieManager instance = android.webkit.CookieManager.getInstance();
                                String url = httpURLConnection.getURL().toString();
                                instance.setCookie(url, httpCookie.getName() + "=" + httpCookie.getValue());
                            }
                        }
                    } catch (NullPointerException unused) {
                        SigmobLog.e(MessageFormat.format("{0} -- Null header for the cookie : {1}", httpURLConnection.getURL().toString(), str.toString()));
                    }
                }
                if (Build.VERSION.SDK_INT >= 21) {
                    android.webkit.CookieManager.getInstance().flush();
                } else {
                    CookieSyncManager.getInstance().sync();
                }
            }
        }
    }

    public void a(boolean z) {
        this.f = z;
    }

    /* access modifiers changed from: package-private */
    public boolean a(String str) {
        return c.equals(Uri.parse(str.toLowerCase(Locale.US)).getLastPathSegment());
    }

    public void b(String str) {
        try {
            Uri parse = Uri.parse(str);
            this.g = parse.getScheme() + "://" + parse.getHost() + "/" + parse.getPath();
        } catch (Throwable unused) {
        }
    }

    @Override // android.webkit.WebViewClient
    public WebResourceResponse shouldInterceptRequest(WebView webView, WebResourceRequest webResourceRequest) {
        WebResourceResponse a2;
        return (!this.f || a(webResourceRequest.getUrl().toString()) || (a2 = a(webResourceRequest.getUrl().toString(), webResourceRequest.getMethod(), webResourceRequest.getRequestHeaders())) == null) ? super.shouldInterceptRequest(webView, webResourceRequest) : a2;
    }

    @Override // android.webkit.WebViewClient
    public WebResourceResponse shouldInterceptRequest(WebView webView, String str) {
        WebResourceResponse a2;
        return (!this.f || a(str) || (a2 = a(str, "GET", null)) == null) ? super.shouldInterceptRequest(webView, str) : a2;
    }
}