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;

/* renamed from: ᵧᵨᵗ, reason: contains not printable characters */
/* loaded from: classes3.dex */
public final class C3665 implements InterfaceC2918 {

    /* renamed from: ᵢˇᵭ, reason: contains not printable characters */
    public final int f13655;

    /* renamed from: ᵤᵙᵷ, reason: contains not printable characters */
    public volatile boolean f13656;

    /* renamed from: ᵧᵾˀˈ, reason: contains not printable characters */
    public final C0734 f13657;

    /* renamed from: ᵾᵬ, reason: contains not printable characters */
    public InputStream f13658;

    /* renamed from: ᵿʗᵝʽ, reason: contains not printable characters */
    public HttpURLConnection f13659;

    public C3665(C0734 c0734, int i) {
        this.f13657 = c0734;
        this.f13655 = i;
    }

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

    @Override // defpackage.InterfaceC2918
    /* renamed from: ˇʼʤˎ */
    public final void mo5387(EnumC5505 enumC5505, InterfaceC0782 interfaceC0782) {
        C0734 c0734 = this.f13657;
        int i = AbstractC4846.f17991;
        SystemClock.elapsedRealtimeNanos();
        try {
            try {
                if (c0734.f3543 == null) {
                    c0734.f3543 = new URL(c0734.m2793());
                }
                interfaceC0782.mo2869(m8256(c0734.f3543, 0, null, c0734.f3545.mo2986()));
                if (Log.isLoggable("HttpUrlFetcher", 2)) {
                    SystemClock.elapsedRealtimeNanos();
                }
            } catch (IOException e) {
                Log.isLoggable("HttpUrlFetcher", 3);
                interfaceC0782.mo2870(e);
                if (Log.isLoggable("HttpUrlFetcher", 2)) {
                    SystemClock.elapsedRealtimeNanos();
                }
            }
        } catch (Throwable th) {
            if (Log.isLoggable("HttpUrlFetcher", 2)) {
                SystemClock.elapsedRealtimeNanos();
            }
            throw th;
        }
    }

    @Override // defpackage.InterfaceC2918
    /* renamed from: ˏˋٴ */
    public final int mo5388() {
        return 2;
    }

    /* renamed from: ᵥᵯˎ, reason: contains not printable characters */
    public final InputStream m8256(URL url, int i, URL url2, Map map) throws IOException {
        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.f13655;
        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.f13659 = httpURLConnection;
            try {
                httpURLConnection.connect();
                this.f13658 = this.f13659.getInputStream();
                if (this.f13656) {
                    return null;
                }
                try {
                    responseCode = this.f13659.getResponseCode();
                } catch (IOException unused2) {
                    Log.isLoggable("HttpUrlFetcher", 3);
                    responseCode = -1;
                }
                int i3 = responseCode / 100;
                if (i3 == 2) {
                    HttpURLConnection httpURLConnection2 = this.f13659;
                    try {
                        if (TextUtils.isEmpty(httpURLConnection2.getContentEncoding())) {
                            this.f13658 = new C2294(httpURLConnection2.getInputStream(), httpURLConnection2.getContentLength());
                        } else {
                            if (Log.isLoggable("HttpUrlFetcher", 3)) {
                                httpURLConnection2.getContentEncoding();
                            }
                            this.f13658 = httpURLConnection2.getInputStream();
                        }
                        return this.f13658;
                    } 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.f13659.getResponseMessage());
                    } catch (IOException e2) {
                        throw new HttpException(responseCode, e2, "Failed to get a response message");
                    }
                }
                String headerField = this.f13659.getHeaderField("Location");
                if (TextUtils.isEmpty(headerField)) {
                    throw new HttpException(responseCode, null, "Received empty or null redirect url");
                }
                try {
                    URL url3 = new URL(url, headerField);
                    mo5389();
                    return m8256(url3, i + 1, url, map);
                } catch (MalformedURLException e3) {
                    throw new HttpException(responseCode, e3, AbstractC4486.m9562("Bad redirect url: ", headerField));
                }
            } catch (IOException e4) {
                try {
                    responseCode2 = this.f13659.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.InterfaceC2918
    /* renamed from: ᵴᵳ */
    public final Class mo1817() {
        return InputStream.class;
    }

    @Override // defpackage.InterfaceC2918
    /* renamed from: ᵻᵢﹳᵰ */
    public final void mo5389() throws IOException {
        InputStream inputStream = this.f13658;
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException unused) {
            }
        }
        HttpURLConnection httpURLConnection = this.f13659;
        if (httpURLConnection != null) {
            httpURLConnection.disconnect();
        }
        this.f13659 = null;
    }
}
