导航菜单

页面标题

页面副标题

Weicon v2.1.4 - HttpUrlConnectionDownloader.java 源代码

正在查看: Weicon v2.1.4 应用的 HttpUrlConnectionDownloader.java JAVA 源代码文件

本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。


package com.tonyodev.fetch2;

import com.google.common.net.HttpHeaders;
import com.tonyodev.fetch2core.Downloader;
import com.tonyodev.fetch2core.FetchCoreUtils;
import com.tonyodev.fetch2core.InterruptMonitor;
import java.io.InputStream;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import kotlin.Metadata;
import kotlin.TypeCastException;
import kotlin.collections.CollectionsKt;
import kotlin.collections.SetsKt;
import kotlin.jvm.internal.Intrinsics;

@Metadata(bv = {1, 0, 3}, d1 = {"\u0000t\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0005\n\u0002\u0010%\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0010\u0002\n\u0002\b\u0006\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u000e\n\u0002\u0010 \n\u0002\b\u0003\n\u0002\u0010\b\n\u0000\n\u0002\u0010\t\n\u0002\b\u0002\n\u0002\u0010\u000b\n\u0002\b\u0004\n\u0002\u0010\"\n\u0002\b\t\b\u0016\u0018\u00002\u000e\u0012\u0004\u0012\u00020\u0002\u0012\u0004\u0012\u00020\u00030\u0001:\u00019B\u000f\b\u0016\u0012\u0006\u0010\u0004\u001a\u00020\u0005¢\u0006\u0002\u0010\u0006B\u001d\b\u0007\u0012\n\b\u0002\u0010\u0007\u001a\u0004\u0018\u00010\b\u0012\b\b\u0002\u0010\u0004\u001a\u00020\u0005¢\u0006\u0002\u0010\tJ\b\u0010\u0016\u001a\u00020\u0017H\u0016J\u0010\u0010\u0018\u001a\u00020\u00172\u0006\u0010\u0019\u001a\u00020\u000fH\u0016J\u0012\u0010\u001a\u001a\u00020\u00172\b\u0010\u001b\u001a\u0004\u0018\u00010\u0002H\u0002J\u001a\u0010\u001c\u001a\u0004\u0018\u00010\u000f2\u0006\u0010\u001d\u001a\u00020\u001e2\u0006\u0010\u001f\u001a\u00020 H\u0016J8\u0010!\u001a\u0014\u0012\u0004\u0012\u00020\"\u0012\n\u0012\b\u0012\u0004\u0012\u00020\"0#0\u000e2\u001c\u0010$\u001a\u0018\u0012\u0006\u0012\u0004\u0018\u00010\"\u0012\f\u0012\n\u0012\u0004\u0012\u00020\"\u0018\u00010#0\u000eH\u0002J\"\u0010%\u001a\u00020\"2\u0018\u0010$\u001a\u0014\u0012\u0004\u0012\u00020\"\u0012\n\u0012\b\u0012\u0004\u0012\u00020\"0#0\u000eH\u0016J\u001f\u0010&\u001a\u0004\u0018\u00010'2\u0006\u0010\u001d\u001a\u00020\u001e2\u0006\u0010(\u001a\u00020)H\u0016¢\u0006\u0002\u0010*J\u0010\u0010+\u001a\u00020,2\u0006\u0010\u001d\u001a\u00020\u001eH\u0016J\u0010\u0010-\u001a\u00020'2\u0006\u0010\u001d\u001a\u00020\u001eH\u0016J\u0010\u0010.\u001a\u00020)2\u0006\u0010\u001d\u001a\u00020\u001eH\u0016J\u001e\u0010/\u001a\u00020\u00052\u0006\u0010\u001d\u001a\u00020\u001e2\f\u00100\u001a\b\u0012\u0004\u0012\u00020\u000501H\u0016J\u0016\u00102\u001a\b\u0012\u0004\u0012\u00020\u0005012\u0006\u0010\u001d\u001a\u00020\u001eH\u0016J\u0010\u00103\u001a\u00020,2\u0006\u00104\u001a\u00020'H\u0004J\u001a\u00105\u001a\u0004\u0018\u00010\u00032\u0006\u0010\u001b\u001a\u00020\u00022\u0006\u0010\u001d\u001a\u00020\u001eH\u0016J\u0018\u00106\u001a\u00020\u00172\u0006\u0010\u001d\u001a\u00020\u001e2\u0006\u0010\u0019\u001a\u00020\u000fH\u0016J\u0018\u00107\u001a\u00020,2\u0006\u0010\u001d\u001a\u00020\u001e2\u0006\u00108\u001a\u00020\"H\u0016R\u0014\u0010\n\u001a\u00020\bX\u0084\u0004¢\u0006\b\n\u0000\u001a\u0004\b\u000b\u0010\fR \u0010\r\u001a\u000e\u0012\u0004\u0012\u00020\u000f\u0012\u0004\u0012\u00020\u00020\u000eX\u0084\u0004¢\u0006\b\n\u0000\u001a\u0004\b\u0010\u0010\u0011R\u0014\u0010\u0012\u001a\u00020\u0013X\u0084\u0004¢\u0006\b\n\u0000\u001a\u0004\b\u0014\u0010\u0015R\u000e\u0010\u0004\u001a\u00020\u0005X\u0082\u0004¢\u0006\u0002\n\u0000¨\u0006:"}, d2 = {"Lcom/tonyodev/fetch2/HttpUrlConnectionDownloader;", "Lcom/tonyodev/fetch2core/Downloader;", "Ljava/net/HttpURLConnection;", "Ljava/lang/Void;", "fileDownloaderType", "Lcom/tonyodev/fetch2core/Downloader$FileDownloaderType;", "(Lcom/tonyodev/fetch2core/Downloader$FileDownloaderType;)V", "httpUrlConnectionPreferences", "Lcom/tonyodev/fetch2/HttpUrlConnectionDownloader$HttpUrlConnectionPreferences;", "(Lcom/tonyodev/fetch2/HttpUrlConnectionDownloader$HttpUrlConnectionPreferences;Lcom/tonyodev/fetch2core/Downloader$FileDownloaderType;)V", "connectionPrefs", "getConnectionPrefs", "()Lcom/tonyodev/fetch2/HttpUrlConnectionDownloader$HttpUrlConnectionPreferences;", "connections", "", "Lcom/tonyodev/fetch2core/Downloader$Response;", "getConnections", "()Ljava/util/Map;", "cookieManager", "Ljava/net/CookieManager;", "getCookieManager", "()Ljava/net/CookieManager;", "close", "", "disconnect", "response", "disconnectClient", "client", "execute", "request", "Lcom/tonyodev/fetch2core/Downloader$ServerRequest;", "interruptMonitor", "Lcom/tonyodev/fetch2core/InterruptMonitor;", "getCleanedHeaders", "", "", "responseHeaders", "getContentHash", "getFileSlicingCount", "", "contentLength", "", "(Lcom/tonyodev/fetch2core/Downloader$ServerRequest;J)Ljava/lang/Integer;", "getHeadRequestMethodSupported", "", "getRequestBufferSize", "getRequestContentLength", "getRequestFileDownloaderType", "supportedFileDownloaderTypes", "", "getRequestSupportedFileDownloaderTypes", "isResponseOk", "responseCode", "onPreClientExecute", "onServerResponse", "verifyContentHash", "hash", "HttpUrlConnectionPreferences", "fetch2_release"}, k = 1, mv = {1, 1, 16})
public class HttpUrlConnectionDownloader implements Downloader<HttpURLConnection, Void> {
    private final HttpUrlConnectionPreferences connectionPrefs;
    private final Map<Downloader.Response, HttpURLConnection> connections;
    private final CookieManager cookieManager;
    private final Downloader.FileDownloaderType fileDownloaderType;

    public HttpUrlConnectionDownloader() {
        this(null, 0 == true ? 1 : 0, 3, 0 == true ? 1 : 0);
    }

    public HttpUrlConnectionDownloader(com.tonyodev.fetch2.HttpUrlConnectionDownloader.HttpUrlConnectionPreferences r3) {
        throw new UnsupportedOperationException("Method not decompiled: com.tonyodev.fetch2.HttpUrlConnectionDownloader.<init>(com.tonyodev.fetch2.HttpUrlConnectionDownloader$HttpUrlConnectionPreferences):void");
    }

    @Override
    public Integer getFileSlicingCount(Downloader.ServerRequest request, long contentLength) {
        Intrinsics.checkParameterIsNotNull(request, "request");
        return null;
    }

    @Override
    public boolean getHeadRequestMethodSupported(Downloader.ServerRequest request) {
        Intrinsics.checkParameterIsNotNull(request, "request");
        return false;
    }

    @Override
    public int getRequestBufferSize(Downloader.ServerRequest request) {
        Intrinsics.checkParameterIsNotNull(request, "request");
        return 8192;
    }

    protected final boolean isResponseOk(int responseCode) {
        return 200 <= responseCode && 299 >= responseCode;
    }

    @Override
    public void onServerResponse(Downloader.ServerRequest request, Downloader.Response response) {
        Intrinsics.checkParameterIsNotNull(request, "request");
        Intrinsics.checkParameterIsNotNull(response, "response");
    }

    public HttpUrlConnectionDownloader(HttpUrlConnectionPreferences httpUrlConnectionPreferences, Downloader.FileDownloaderType fileDownloaderType) {
        Intrinsics.checkParameterIsNotNull(fileDownloaderType, "fileDownloaderType");
        this.fileDownloaderType = fileDownloaderType;
        this.connectionPrefs = httpUrlConnectionPreferences == null ? new HttpUrlConnectionPreferences() : httpUrlConnectionPreferences;
        Map<Downloader.Response, HttpURLConnection> synchronizedMap = Collections.synchronizedMap(new HashMap());
        Intrinsics.checkExpressionValueIsNotNull(synchronizedMap, "Collections.synchronized…se, HttpURLConnection>())");
        this.connections = synchronizedMap;
        this.cookieManager = FetchCoreUtils.getDefaultCookieManager();
    }

    public HttpUrlConnectionDownloader(com.tonyodev.fetch2.HttpUrlConnectionDownloader.HttpUrlConnectionPreferences r1, com.tonyodev.fetch2core.Downloader.FileDownloaderType r2, int r3, kotlin.jvm.internal.DefaultConstructorMarker r4) {
        throw new UnsupportedOperationException("Method not decompiled: com.tonyodev.fetch2.HttpUrlConnectionDownloader.<init>(com.tonyodev.fetch2.HttpUrlConnectionDownloader$HttpUrlConnectionPreferences, com.tonyodev.fetch2core.Downloader$FileDownloaderType, int, kotlin.jvm.internal.DefaultConstructorMarker):void");
    }

    public HttpUrlConnectionDownloader(Downloader.FileDownloaderType fileDownloaderType) {
        this(null, fileDownloaderType);
        Intrinsics.checkParameterIsNotNull(fileDownloaderType, "fileDownloaderType");
    }

    protected final HttpUrlConnectionPreferences getConnectionPrefs() {
        return this.connectionPrefs;
    }

    protected final Map<Downloader.Response, HttpURLConnection> getConnections() {
        return this.connections;
    }

    protected final CookieManager getCookieManager() {
        return this.cookieManager;
    }

    @Override
    public Void onPreClientExecute(HttpURLConnection client, Downloader.ServerRequest request) {
        Intrinsics.checkParameterIsNotNull(client, "client");
        Intrinsics.checkParameterIsNotNull(request, "request");
        client.setRequestMethod(request.getRequestMethod());
        client.setReadTimeout(this.connectionPrefs.getReadTimeout());
        client.setConnectTimeout(this.connectionPrefs.getConnectTimeout());
        client.setUseCaches(this.connectionPrefs.getUsesCache());
        client.setDefaultUseCaches(this.connectionPrefs.getUsesDefaultCache());
        client.setInstanceFollowRedirects(this.connectionPrefs.getFollowsRedirect());
        client.setDoInput(true);
        Iterator<T> it = request.getHeaders().entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();
            client.addRequestProperty((String) entry.getKey(), (String) entry.getValue());
        }
        return null;
    }

    private final Map<String, List<String>> getCleanedHeaders(Map<String, List<String>> responseHeaders) {
        LinkedHashMap linkedHashMap = new LinkedHashMap();
        for (Map.Entry<String, List<String>> entry : responseHeaders.entrySet()) {
            String key = entry.getKey();
            if (key != null) {
                List<String> value = entry.getValue();
                if (value == null) {
                    value = CollectionsKt.emptyList();
                }
                linkedHashMap.put(key, value);
            }
        }
        return linkedHashMap;
    }

    @Override
    public Downloader.Response execute(Downloader.ServerRequest request, InterruptMonitor interruptMonitor) {
        HttpURLConnection httpURLConnection;
        Map<String, List<String>> cleanedHeaders;
        int responseCode;
        InputStream inputStream;
        String copyStreamToString;
        long j;
        String str;
        boolean z;
        Intrinsics.checkParameterIsNotNull(request, "request");
        Intrinsics.checkParameterIsNotNull(interruptMonitor, "interruptMonitor");
        CookieHandler.setDefault(this.cookieManager);
        URLConnection openConnection = new URL(request.getUrl()).openConnection();
        if (openConnection == null) {
            throw new TypeCastException("null cannot be cast to non-null type java.net.HttpURLConnection");
        }
        HttpURLConnection httpURLConnection2 = (HttpURLConnection) openConnection;
        onPreClientExecute(httpURLConnection2, request);
        if (httpURLConnection2.getRequestProperty(HttpHeaders.REFERER) == null) {
            httpURLConnection2.addRequestProperty(HttpHeaders.REFERER, FetchCoreUtils.getRefererFromUrl(request.getUrl()));
        }
        httpURLConnection2.connect();
        Map<String, List<String>> headerFields = httpURLConnection2.getHeaderFields();
        Intrinsics.checkExpressionValueIsNotNull(headerFields, "client.headerFields");
        Map<String, List<String>> cleanedHeaders2 = getCleanedHeaders(headerFields);
        int responseCode2 = httpURLConnection2.getResponseCode();
        if ((responseCode2 == 302 || responseCode2 == 301 || responseCode2 == 303) && FetchCoreUtils.getHeaderValue(cleanedHeaders2, HttpHeaders.LOCATION) != null) {
            try {
                httpURLConnection2.disconnect();
            } catch (Exception unused) {
            }
            String headerValue = FetchCoreUtils.getHeaderValue(cleanedHeaders2, HttpHeaders.LOCATION);
            if (headerValue == null) {
                headerValue = "";
            }
            URLConnection openConnection2 = new URL(headerValue).openConnection();
            if (openConnection2 == null) {
                throw new TypeCastException("null cannot be cast to non-null type java.net.HttpURLConnection");
            }
            HttpURLConnection httpURLConnection3 = (HttpURLConnection) openConnection2;
            onPreClientExecute(httpURLConnection3, request);
            if (httpURLConnection3.getRequestProperty(HttpHeaders.REFERER) == null) {
                httpURLConnection3.addRequestProperty(HttpHeaders.REFERER, FetchCoreUtils.getRefererFromUrl(request.getUrl()));
            }
            httpURLConnection3.connect();
            Map<String, List<String>> headerFields2 = httpURLConnection3.getHeaderFields();
            Intrinsics.checkExpressionValueIsNotNull(headerFields2, "client.headerFields");
            httpURLConnection = httpURLConnection3;
            cleanedHeaders = getCleanedHeaders(headerFields2);
            responseCode = httpURLConnection3.getResponseCode();
        } else {
            httpURLConnection = httpURLConnection2;
            cleanedHeaders = cleanedHeaders2;
            responseCode = responseCode2;
        }
        if (isResponseOk(responseCode)) {
            copyStreamToString = null;
            j = FetchCoreUtils.getContentLengthFromHeader(cleanedHeaders, -1L);
            inputStream = httpURLConnection.getInputStream();
            str = getContentHash(cleanedHeaders);
            z = true;
        } else {
            inputStream = null;
            copyStreamToString = FetchCoreUtils.copyStreamToString(httpURLConnection.getErrorStream(), false);
            j = -1;
            str = "";
            z = false;
        }
        boolean acceptRanges = FetchCoreUtils.acceptRanges(responseCode, cleanedHeaders);
        Map<String, List<String>> headerFields3 = httpURLConnection.getHeaderFields();
        Intrinsics.checkExpressionValueIsNotNull(headerFields3, "client.headerFields");
        int i = responseCode;
        boolean z2 = z;
        long j2 = j;
        String str2 = str;
        HttpURLConnection httpURLConnection4 = httpURLConnection;
        String str3 = copyStreamToString;
        onServerResponse(request, new Downloader.Response(i, z2, j2, null, request, str2, headerFields3, acceptRanges, str3));
        Downloader.Response response = new Downloader.Response(i, z2, j2, inputStream, request, str2, cleanedHeaders, acceptRanges, str3);
        this.connections.put(response, httpURLConnection4);
        return response;
    }

    @Override
    public void disconnect(Downloader.Response response) {
        Intrinsics.checkParameterIsNotNull(response, "response");
        if (this.connections.containsKey(response)) {
            HttpURLConnection httpURLConnection = this.connections.get(response);
            this.connections.remove(response);
            disconnectClient(httpURLConnection);
        }
    }

    @Override
    public String getContentHash(Map<String, List<String>> responseHeaders) {
        Intrinsics.checkParameterIsNotNull(responseHeaders, "responseHeaders");
        String headerValue = FetchCoreUtils.getHeaderValue(responseHeaders, HttpHeaders.CONTENT_MD5);
        return headerValue != null ? headerValue : "";
    }

    @Override
    public void close() {
        Iterator<T> it = this.connections.entrySet().iterator();
        while (it.hasNext()) {
            disconnectClient((HttpURLConnection) ((Map.Entry) it.next()).getValue());
        }
        this.connections.clear();
    }

    private final void disconnectClient(HttpURLConnection client) {
        if (client != null) {
            try {
                client.disconnect();
            } catch (Exception unused) {
            }
        }
    }

    @Override
    public Downloader.FileDownloaderType getRequestFileDownloaderType(Downloader.ServerRequest request, Set<? extends Downloader.FileDownloaderType> supportedFileDownloaderTypes) {
        Intrinsics.checkParameterIsNotNull(request, "request");
        Intrinsics.checkParameterIsNotNull(supportedFileDownloaderTypes, "supportedFileDownloaderTypes");
        return this.fileDownloaderType;
    }

    @Override
    public boolean verifyContentHash(Downloader.ServerRequest request, String hash) {
        String fileMd5String;
        Intrinsics.checkParameterIsNotNull(request, "request");
        Intrinsics.checkParameterIsNotNull(hash, "hash");
        String str = hash;
        if (str.length() == 0 || (fileMd5String = FetchCoreUtils.getFileMd5String(request.getFile())) == null) {
            return true;
        }
        if (fileMd5String != null) {
            return fileMd5String.contentEquals(str);
        }
        throw new TypeCastException("null cannot be cast to non-null type java.lang.String");
    }

    @Override
    public long getRequestContentLength(Downloader.ServerRequest request) {
        Intrinsics.checkParameterIsNotNull(request, "request");
        return FetchCoreUtils.getRequestContentLength(request, this);
    }

    @Override
    public Set<Downloader.FileDownloaderType> getRequestSupportedFileDownloaderTypes(Downloader.ServerRequest request) {
        Intrinsics.checkParameterIsNotNull(request, "request");
        if (this.fileDownloaderType == Downloader.FileDownloaderType.SEQUENTIAL) {
            return SetsKt.mutableSetOf(new Downloader.FileDownloaderType[]{this.fileDownloaderType});
        }
        try {
            return FetchCoreUtils.getRequestSupportedFileDownloaderTypes(request, this);
        } catch (Exception unused) {
            return SetsKt.mutableSetOf(new Downloader.FileDownloaderType[]{this.fileDownloaderType});
        }
    }

    @Metadata(bv = {1, 0, 3}, d1 = {"\u0000\u001c\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0002\n\u0002\u0010\b\n\u0002\b\u0005\n\u0002\u0010\u000b\n\u0002\b\u000e\b\u0016\u0018\u00002\u00020\u0001B\u0005¢\u0006\u0002\u0010\u0002R\u001a\u0010\u0003\u001a\u00020\u0004X\u0086\u000e¢\u0006\u000e\n\u0000\u001a\u0004\b\u0005\u0010\u0006\"\u0004\b\u0007\u0010\bR\u001a\u0010\t\u001a\u00020\nX\u0086\u000e¢\u0006\u000e\n\u0000\u001a\u0004\b\u000b\u0010\f\"\u0004\b\r\u0010\u000eR\u001a\u0010\u000f\u001a\u00020\u0004X\u0086\u000e¢\u0006\u000e\n\u0000\u001a\u0004\b\u0010\u0010\u0006\"\u0004\b\u0011\u0010\bR\u001a\u0010\u0012\u001a\u00020\nX\u0086\u000e¢\u0006\u000e\n\u0000\u001a\u0004\b\u0013\u0010\f\"\u0004\b\u0014\u0010\u000eR\u001a\u0010\u0015\u001a\u00020\nX\u0086\u000e¢\u0006\u000e\n\u0000\u001a\u0004\b\u0016\u0010\f\"\u0004\b\u0017\u0010\u000e¨\u0006\u0018"}, d2 = {"Lcom/tonyodev/fetch2/HttpUrlConnectionDownloader$HttpUrlConnectionPreferences;", "", "()V", "connectTimeout", "", "getConnectTimeout", "()I", "setConnectTimeout", "(I)V", "followsRedirect", "", "getFollowsRedirect", "()Z", "setFollowsRedirect", "(Z)V", "readTimeout", "getReadTimeout", "setReadTimeout", "usesCache", "getUsesCache", "setUsesCache", "usesDefaultCache", "getUsesDefaultCache", "setUsesDefaultCache", "fetch2_release"}, k = 1, mv = {1, 1, 16})
    public static class HttpUrlConnectionPreferences {
        private boolean usesCache;
        private boolean usesDefaultCache;
        private int readTimeout = 20000;
        private int connectTimeout = 15000;
        private boolean followsRedirect = true;

        public final int getReadTimeout() {
            return this.readTimeout;
        }

        public final void setReadTimeout(int i) {
            this.readTimeout = i;
        }

        public final int getConnectTimeout() {
            return this.connectTimeout;
        }

        public final void setConnectTimeout(int i) {
            this.connectTimeout = i;
        }

        public final boolean getUsesCache() {
            return this.usesCache;
        }

        public final void setUsesCache(boolean z) {
            this.usesCache = z;
        }

        public final boolean getUsesDefaultCache() {
            return this.usesDefaultCache;
        }

        public final void setUsesDefaultCache(boolean z) {
            this.usesDefaultCache = z;
        }

        public final boolean getFollowsRedirect() {
            return this.followsRedirect;
        }

        public final void setFollowsRedirect(boolean z) {
            this.followsRedirect = z;
        }
    }
}