package com.bumptech.glide.load.data;

import android.os.SystemClock;
import android.text.TextUtils;
import android.util.Log;
import androidx.media3.exoplayer.rtsp.RtspHeaders;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.DataSource;
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: loaded from: classes.dex */
public final class l implements e {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final g5.o f5623a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final int f5624b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public HttpURLConnection f5625c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public InputStream f5626d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public volatile boolean f5627e;

    public l(g5.o oVar, int i10) {
        this.f5623a = oVar;
        this.f5624b = i10;
    }

    @Override // com.bumptech.glide.load.data.e
    public final Class a() {
        return InputStream.class;
    }

    @Override // com.bumptech.glide.load.data.e
    public final void b() {
        InputStream inputStream = this.f5626d;
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException unused) {
            }
        }
        HttpURLConnection httpURLConnection = this.f5625c;
        if (httpURLConnection != null) {
            httpURLConnection.disconnect();
        }
        this.f5625c = null;
    }

    public final InputStream c(URL url, int i10, URL url2, Map map) throws HttpException {
        int responseCode;
        int responseCode2 = -1;
        if (i10 >= 5) {
            throw new HttpException("Too many (> 5) redirects!", -1, null);
        }
        if (url2 != null) {
            try {
                if (url.toURI().equals(url2.toURI())) {
                    throw new HttpException("In re-direct loop", -1, null);
                }
            } catch (URISyntaxException unused) {
            }
        }
        try {
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            for (Map.Entry entry : map.entrySet()) {
                httpURLConnection.addRequestProperty((String) entry.getKey(), (String) entry.getValue());
            }
            int i11 = this.f5624b;
            httpURLConnection.setConnectTimeout(i11);
            httpURLConnection.setReadTimeout(i11);
            httpURLConnection.setUseCaches(false);
            httpURLConnection.setDoInput(true);
            httpURLConnection.setInstanceFollowRedirects(false);
            this.f5625c = httpURLConnection;
            try {
                httpURLConnection.connect();
                this.f5626d = this.f5625c.getInputStream();
                if (this.f5627e) {
                    return null;
                }
                try {
                    responseCode = this.f5625c.getResponseCode();
                } catch (IOException unused2) {
                    responseCode = -1;
                }
                int i12 = responseCode / 100;
                if (i12 == 2) {
                    HttpURLConnection httpURLConnection2 = this.f5625c;
                    try {
                        if (TextUtils.isEmpty(httpURLConnection2.getContentEncoding())) {
                            this.f5626d = new t5.e(httpURLConnection2.getInputStream(), httpURLConnection2.getContentLength());
                        } else {
                            if (Log.isLoggable("HttpUrlFetcher", 3)) {
                                httpURLConnection2.getContentEncoding();
                            }
                            this.f5626d = httpURLConnection2.getInputStream();
                        }
                        return this.f5626d;
                    } catch (IOException e10) {
                        try {
                            responseCode2 = httpURLConnection2.getResponseCode();
                        } catch (IOException unused3) {
                        }
                        throw new HttpException("Failed to obtain InputStream", responseCode2, e10);
                    }
                }
                if (i12 != 3) {
                    if (responseCode == -1) {
                        throw new HttpException("Http request failed", responseCode, null);
                    }
                    try {
                        throw new HttpException(this.f5625c.getResponseMessage(), responseCode, null);
                    } catch (IOException e11) {
                        throw new HttpException("Failed to get a response message", responseCode, e11);
                    }
                }
                String headerField = this.f5625c.getHeaderField(RtspHeaders.LOCATION);
                if (TextUtils.isEmpty(headerField)) {
                    throw new HttpException("Received empty or null redirect url", responseCode, null);
                }
                try {
                    URL url3 = new URL(url, headerField);
                    b();
                    return c(url3, i10 + 1, url, map);
                } catch (MalformedURLException e12) {
                    throw new HttpException(defpackage.e.i("Bad redirect url: ", headerField), responseCode, e12);
                }
            } catch (IOException e13) {
                try {
                    responseCode2 = this.f5625c.getResponseCode();
                } catch (IOException unused4) {
                }
                throw new HttpException("Failed to connect or obtain data", responseCode2, e13);
            }
        } catch (IOException e14) {
            throw new HttpException("URL.openConnection threw", 0, e14);
        }
    }

    @Override // com.bumptech.glide.load.data.e
    public final void cancel() {
        this.f5627e = true;
    }

    @Override // com.bumptech.glide.load.data.e
    public final DataSource d() {
        return DataSource.f5590b;
    }

    @Override // com.bumptech.glide.load.data.e
    public final void e(Priority priority, d dVar) {
        g5.o oVar = this.f5623a;
        int i10 = t5.i.f32966a;
        SystemClock.elapsedRealtimeNanos();
        try {
            try {
                dVar.onDataReady(c(oVar.toURL(), 0, null, oVar.getHeaders()));
                if (!Log.isLoggable("HttpUrlFetcher", 2)) {
                    return;
                }
            } catch (IOException e10) {
                dVar.c(e10);
                if (!Log.isLoggable("HttpUrlFetcher", 2)) {
                    return;
                }
            }
            SystemClock.elapsedRealtimeNanos();
        } catch (Throwable th) {
            if (Log.isLoggable("HttpUrlFetcher", 2)) {
                SystemClock.elapsedRealtimeNanos();
            }
            throw th;
        }
    }
}
