package defpackage;

import android.os.SystemClock;
import android.text.TextUtils;
import android.util.Log;
import com.bumptech.glide.load.HttpException;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Map;

/* JADX INFO: renamed from: ᵽᵣˌʤ, reason: contains not printable characters */
/* JADX INFO: loaded from: classes3.dex */
public final class C5375 implements InterfaceC5071 {

    /* JADX INFO: renamed from: ˌᴵ, reason: contains not printable characters */
    public final C2556 f20348;

    /* JADX INFO: renamed from: ᵔᵖᵹᵫ, reason: contains not printable characters */
    public HttpURLConnection f20349;

    /* JADX INFO: renamed from: ᵜᵪʾ, reason: contains not printable characters */
    public final int f20350;

    /* JADX INFO: renamed from: ᵨᵼⁱ, reason: contains not printable characters */
    public InputStream f20351;

    /* JADX INFO: renamed from: ᵯᵷʽ, reason: contains not printable characters */
    public volatile boolean f20352;

    public C5375(C2556 c2556, int i) {
        this.f20348 = c2556;
        this.f20350 = i;
    }

    @Override // defpackage.InterfaceC5071
    public final void cancel() {
        this.f20352 = true;
    }

    @Override // defpackage.InterfaceC5071
    /* JADX INFO: renamed from: ʽˈˋ */
    public final Class mo1311() {
        return InputStream.class;
    }

    @Override // defpackage.InterfaceC5071
    /* JADX INFO: renamed from: ᵨᵣ */
    public final void mo1314(EnumC4810 enumC4810, InterfaceC3368 interfaceC3368) {
        C2556 c2556 = this.f20348;
        int i = AbstractC0925.f4045;
        SystemClock.elapsedRealtimeNanos();
        try {
            try {
                if (c2556.f9878 == null) {
                    c2556.f9878 = new URL(c2556.m6280());
                }
                interfaceC3368.mo1312(m10961(c2556.f9878, 0, null, c2556.f9872.mo2952()));
                if (Log.isLoggable("HttpUrlFetcher", 2)) {
                    SystemClock.elapsedRealtimeNanos();
                }
            } catch (IOException e) {
                Log.isLoggable("HttpUrlFetcher", 3);
                interfaceC3368.mo1313(e);
                if (Log.isLoggable("HttpUrlFetcher", 2)) {
                    SystemClock.elapsedRealtimeNanos();
                }
            }
        } catch (Throwable th) {
            if (Log.isLoggable("HttpUrlFetcher", 2)) {
                SystemClock.elapsedRealtimeNanos();
            }
            throw th;
        }
    }

    /* JADX INFO: renamed from: ᵲᵶᵳˈ, reason: contains not printable characters */
    public final InputStream m10961(URL url, int i, URL url2, Map map) throws HttpException {
        int responseCode;
        int responseCode2 = -1;
        if (i >= 5) {
            throw new HttpException(-1, null, "Too many (> 5) redirects!");
        }
        if (url2 != null) {
            try {
                if (url.toURI().equals(url2.toURI())) {
                    throw new HttpException(-1, null, "In re-direct loop");
                }
            } catch (URISyntaxException unused) {
            }
        }
        int i2 = this.f20350;
        try {
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            for (Map.Entry entry : map.entrySet()) {
                httpURLConnection.addRequestProperty((String) entry.getKey(), (String) entry.getValue());
            }
            httpURLConnection.setConnectTimeout(i2);
            httpURLConnection.setReadTimeout(i2);
            httpURLConnection.setUseCaches(false);
            httpURLConnection.setDoInput(true);
            httpURLConnection.setInstanceFollowRedirects(false);
            this.f20349 = httpURLConnection;
            try {
                httpURLConnection.connect();
                this.f20351 = this.f20349.getInputStream();
                if (this.f20352) {
                    return null;
                }
                try {
                    responseCode = this.f20349.getResponseCode();
                } catch (IOException unused2) {
                    Log.isLoggable("HttpUrlFetcher", 3);
                    responseCode = -1;
                }
                int i3 = responseCode / 100;
                if (i3 == 2) {
                    HttpURLConnection httpURLConnection2 = this.f20349;
                    try {
                        if (TextUtils.isEmpty(httpURLConnection2.getContentEncoding())) {
                            this.f20351 = new C2728(httpURLConnection2.getInputStream(), httpURLConnection2.getContentLength());
                        } else {
                            if (Log.isLoggable("HttpUrlFetcher", 3)) {
                                httpURLConnection2.getContentEncoding();
                            }
                            this.f20351 = httpURLConnection2.getInputStream();
                        }
                        return this.f20351;
                    } catch (IOException e) {
                        try {
                            responseCode2 = httpURLConnection2.getResponseCode();
                        } catch (IOException unused3) {
                            Log.isLoggable("HttpUrlFetcher", 3);
                        }
                        throw new HttpException(responseCode2, e, "Failed to obtain InputStream");
                    }
                }
                if (i3 != 3) {
                    if (responseCode == -1) {
                        throw new HttpException(responseCode, null, "Http request failed");
                    }
                    try {
                        throw new HttpException(responseCode, null, this.f20349.getResponseMessage());
                    } catch (IOException e2) {
                        throw new HttpException(responseCode, e2, "Failed to get a response message");
                    }
                }
                String headerField = this.f20349.getHeaderField("Location");
                if (TextUtils.isEmpty(headerField)) {
                    throw new HttpException(responseCode, null, "Received empty or null redirect url");
                }
                try {
                    URL url3 = new URL(url, headerField);
                    mo1316();
                    return m10961(url3, i + 1, url, map);
                } catch (MalformedURLException e3) {
                    throw new HttpException(responseCode, e3, AbstractC3589.m8102("Bad redirect url: ", headerField));
                }
            } catch (IOException e4) {
                try {
                    responseCode2 = this.f20349.getResponseCode();
                } catch (IOException unused4) {
                    Log.isLoggable("HttpUrlFetcher", 3);
                }
                throw new HttpException(responseCode2, e4, "Failed to connect or obtain data");
            }
        } catch (IOException e5) {
            throw new HttpException(0, e5, "URL.openConnection threw");
        }
    }

    @Override // defpackage.InterfaceC5071
    /* JADX INFO: renamed from: ᵷᵺʾᵖ */
    public final void mo1316() {
        InputStream inputStream = this.f20351;
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException unused) {
            }
        }
        HttpURLConnection httpURLConnection = this.f20349;
        if (httpURLConnection != null) {
            httpURLConnection.disconnect();
        }
        this.f20349 = null;
    }

    @Override // defpackage.InterfaceC5071
    /* JADX INFO: renamed from: ᵻˈᵦʼ */
    public final int mo1317() {
        return 2;
    }
}
