package com.bumptech.glide.load.data;

import android.text.TextUtils;
import android.util.Log;
import com.bumptech.glide.load.data.d;
import com.google.firebase.perf.network.FirebasePerfUrlConnection;
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.net.URLConnection;
import java.util.Map;

/* loaded from: classes4.dex */
public class j implements d {

    /* renamed from: v, reason: collision with root package name */
    static final b f12592v = new a();

    /* renamed from: p, reason: collision with root package name */
    private final ph.h f12593p;

    /* renamed from: q, reason: collision with root package name */
    private final int f12594q;

    /* renamed from: r, reason: collision with root package name */
    private final b f12595r;

    /* renamed from: s, reason: collision with root package name */
    private HttpURLConnection f12596s;

    /* renamed from: t, reason: collision with root package name */
    private InputStream f12597t;

    /* renamed from: u, reason: collision with root package name */
    private volatile boolean f12598u;

    /* loaded from: classes4.dex */
    private static class a implements b {
        a() {
        }

        @Override // com.bumptech.glide.load.data.j.b
        public HttpURLConnection a(URL url) {
            return (HttpURLConnection) ((URLConnection) FirebasePerfUrlConnection.instrument(url.openConnection()));
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes4.dex */
    public interface b {
        HttpURLConnection a(URL url);
    }

    public j(ph.h hVar, int i10) {
        this(hVar, i10, f12592v);
    }

    private HttpURLConnection c(URL url, Map map) {
        try {
            HttpURLConnection a10 = this.f12595r.a(url);
            for (Map.Entry entry : map.entrySet()) {
                a10.addRequestProperty((String) entry.getKey(), (String) entry.getValue());
            }
            a10.setConnectTimeout(this.f12594q);
            a10.setReadTimeout(this.f12594q);
            a10.setUseCaches(false);
            a10.setDoInput(true);
            a10.setInstanceFollowRedirects(false);
            return a10;
        } catch (IOException e10) {
            throw new jh.e("URL.openConnection threw", 0, e10);
        }
    }

    private static int f(HttpURLConnection httpURLConnection) {
        try {
            return httpURLConnection.getResponseCode();
        } catch (IOException e10) {
            if (!Log.isLoggable("HttpUrlFetcher", 3)) {
                return -1;
            }
            Log.d("HttpUrlFetcher", "Failed to get a response code", e10);
            return -1;
        }
    }

    private InputStream g(HttpURLConnection httpURLConnection) {
        try {
            if (TextUtils.isEmpty(httpURLConnection.getContentEncoding())) {
                this.f12597t = ei.c.b(httpURLConnection.getInputStream(), httpURLConnection.getContentLength());
            } else {
                if (Log.isLoggable("HttpUrlFetcher", 3)) {
                    Log.d("HttpUrlFetcher", "Got non empty content encoding: " + httpURLConnection.getContentEncoding());
                }
                this.f12597t = httpURLConnection.getInputStream();
            }
            return this.f12597t;
        } catch (IOException e10) {
            throw new jh.e("Failed to obtain InputStream", f(httpURLConnection), e10);
        }
    }

    private static boolean h(int i10) {
        return i10 / 100 == 2;
    }

    private static boolean i(int i10) {
        return i10 / 100 == 3;
    }

    private InputStream j(URL url, int i10, URL url2, Map map) {
        if (i10 >= 5) {
            throw new jh.e("Too many (> 5) redirects!", -1);
        }
        if (url2 != null) {
            try {
                if (url.toURI().equals(url2.toURI())) {
                    throw new jh.e("In re-direct loop", -1);
                }
            } catch (URISyntaxException unused) {
            }
        }
        HttpURLConnection c10 = c(url, map);
        this.f12596s = c10;
        try {
            c10.connect();
            this.f12597t = this.f12596s.getInputStream();
            if (this.f12598u) {
                return null;
            }
            int f10 = f(this.f12596s);
            if (h(f10)) {
                return g(this.f12596s);
            }
            if (!i(f10)) {
                if (f10 == -1) {
                    throw new jh.e(f10);
                }
                try {
                    throw new jh.e(this.f12596s.getResponseMessage(), f10);
                } catch (IOException e10) {
                    throw new jh.e("Failed to get a response message", f10, e10);
                }
            }
            String headerField = this.f12596s.getHeaderField("Location");
            if (TextUtils.isEmpty(headerField)) {
                throw new jh.e("Received empty or null redirect url", f10);
            }
            try {
                URL url3 = new URL(url, headerField);
                b();
                return j(url3, i10 + 1, url, map);
            } catch (MalformedURLException e11) {
                throw new jh.e("Bad redirect url: " + headerField, f10, e11);
            }
        } catch (IOException e12) {
            throw new jh.e("Failed to connect or obtain data", f(this.f12596s), e12);
        }
    }

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

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

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

    @Override // com.bumptech.glide.load.data.d
    public jh.a d() {
        return jh.a.REMOTE;
    }

    @Override // com.bumptech.glide.load.data.d
    public void e(com.bumptech.glide.g gVar, d.a aVar) {
        StringBuilder sb2;
        long b10 = ei.g.b();
        try {
            try {
                aVar.f(j(this.f12593p.i(), 0, null, this.f12593p.e()));
            } catch (IOException e10) {
                if (Log.isLoggable("HttpUrlFetcher", 3)) {
                    Log.d("HttpUrlFetcher", "Failed to load data for url", e10);
                }
                aVar.c(e10);
                if (!Log.isLoggable("HttpUrlFetcher", 2)) {
                    return;
                } else {
                    sb2 = new StringBuilder();
                }
            }
            if (Log.isLoggable("HttpUrlFetcher", 2)) {
                sb2 = new StringBuilder();
                sb2.append("Finished http url fetcher fetch in ");
                sb2.append(ei.g.a(b10));
                Log.v("HttpUrlFetcher", sb2.toString());
            }
        } catch (Throwable th2) {
            if (Log.isLoggable("HttpUrlFetcher", 2)) {
                Log.v("HttpUrlFetcher", "Finished http url fetcher fetch in " + ei.g.a(b10));
            }
            throw th2;
        }
    }

    j(ph.h hVar, int i10, b bVar) {
        this.f12593p = hVar;
        this.f12594q = i10;
        this.f12595r = bVar;
    }
}
