package com.google.android.gms.internal.ads;

import androidx.annotation.Nullable;
import androidx.media3.exoplayer.rtsp.RtspHeaders;
import java.io.Closeable;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.Set;

/* JADX INFO: loaded from: classes2.dex */
public final class ui1 implements Closeable {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public uq1 f17283a;

    @Nullable
    private ri1 zzc;

    @Nullable
    private HttpURLConnection zzd;

    public ui1(uq1 uq1Var, uq1 uq1Var2, @Nullable ri1 ri1Var) {
        this.f17283a = uq1Var2;
        this.zzc = ri1Var;
    }

    public static void zzi(@Nullable HttpURLConnection httpURLConnection) {
        if (httpURLConnection != null) {
            httpURLConnection.disconnect();
        }
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public final void close() {
        zzi(this.zzd);
    }

    public final HttpURLConnection e(y40 y40Var) throws IOException {
        ti1 ti1Var = new ti1(265, 0);
        this.f17283a = new ti1(-1, 1);
        this.zzc = y40Var;
        ((Integer) ti1Var.zza()).intValue();
        ((Integer) this.f17283a.zza()).intValue();
        ri1 ri1Var = this.zzc;
        ri1Var.getClass();
        Set set = z40.f19085f;
        xv1 xv1Var = m7.n.C.f30478q;
        int iIntValue = ((Integer) n7.y.f31093e.f31096c.b(ao.f8763a0)).intValue();
        URL url = new URL(((y40) ri1Var).f18653b);
        int i10 = 0;
        while (true) {
            i10++;
            if (i10 > 20) {
                throw new IOException("Too many redirects (20)");
            }
            URLConnection uRLConnectionOpenConnection = url.openConnection();
            uRLConnectionOpenConnection.setConnectTimeout(iIntValue);
            uRLConnectionOpenConnection.setReadTimeout(iIntValue);
            if (!(uRLConnectionOpenConnection instanceof HttpURLConnection)) {
                throw new IOException("Invalid protocol.");
            }
            HttpURLConnection httpURLConnection = (HttpURLConnection) uRLConnectionOpenConnection;
            q7.g gVar = new q7.g(null);
            gVar.zza(httpURLConnection, null);
            httpURLConnection.setInstanceFollowRedirects(false);
            int responseCode = httpURLConnection.getResponseCode();
            gVar.a(httpURLConnection, responseCode);
            if (responseCode / 100 != 3) {
                this.zzd = httpURLConnection;
                return httpURLConnection;
            }
            String headerField = httpURLConnection.getHeaderField(RtspHeaders.LOCATION);
            if (headerField == null) {
                throw new IOException("Missing Location header in redirect");
            }
            URL url2 = new URL(url, headerField);
            String protocol = url2.getProtocol();
            if (protocol == null) {
                throw new IOException("Protocol is null");
            }
            if (!protocol.equals("http") && !protocol.equals("https")) {
                throw new IOException("Unsupported scheme: ".concat(protocol));
            }
            q7.j.a("Redirecting to ".concat(headerField));
            httpURLConnection.disconnect();
            url = url2;
        }
    }
}
