package fh;

import com.amplifyframework.storage.ObjectMetadata;
import com.google.firebase.perf.FirebasePerformance;
import com.google.firebase.perf.network.FirebasePerfUrlConnection;
import eh.n;
import java.io.DataOutputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSocketFactory;

/* loaded from: classes4.dex */
public class h extends fh.a {

    /* renamed from: a, reason: collision with root package name */
    private final SSLSocketFactory f18499a;

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes4.dex */
    public static class a extends FilterInputStream {

        /* renamed from: p, reason: collision with root package name */
        private final HttpURLConnection f18500p;

        a(HttpURLConnection httpURLConnection) {
            super(h.j(httpURLConnection));
            this.f18500p = httpURLConnection;
        }

        @Override // java.io.FilterInputStream, java.io.InputStream, java.io.Closeable, java.lang.AutoCloseable
        public void close() {
            super.close();
            this.f18500p.disconnect();
        }
    }

    /* loaded from: classes4.dex */
    public interface b {
    }

    public h() {
        this(null);
    }

    private void c(HttpURLConnection httpURLConnection, n nVar, byte[] bArr) {
        httpURLConnection.setDoOutput(true);
        if (!httpURLConnection.getRequestProperties().containsKey(ObjectMetadata.CONTENT_TYPE)) {
            httpURLConnection.setRequestProperty(ObjectMetadata.CONTENT_TYPE, nVar.w());
        }
        DataOutputStream dataOutputStream = new DataOutputStream(h(nVar, httpURLConnection, bArr.length));
        dataOutputStream.write(bArr);
        dataOutputStream.close();
    }

    private void d(HttpURLConnection httpURLConnection, n nVar) {
        byte[] v10 = nVar.v();
        if (v10 != null) {
            c(httpURLConnection, nVar, v10);
        }
    }

    static List e(Map map) {
        ArrayList arrayList = new ArrayList(map.size());
        for (Map.Entry entry : map.entrySet()) {
            if (entry.getKey() != null) {
                Iterator it = ((List) entry.getValue()).iterator();
                while (it.hasNext()) {
                    arrayList.add(new eh.g((String) entry.getKey(), (String) it.next()));
                }
            }
        }
        return arrayList;
    }

    private static boolean i(int i10, int i11) {
        return (i10 == 4 || (100 <= i11 && i11 < 200) || i11 == 204 || i11 == 304) ? false : true;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static InputStream j(HttpURLConnection httpURLConnection) {
        try {
            return httpURLConnection.getInputStream();
        } catch (IOException unused) {
            return httpURLConnection.getErrorStream();
        }
    }

    private HttpURLConnection k(URL url, n nVar) {
        SSLSocketFactory sSLSocketFactory;
        HttpURLConnection f10 = f(url);
        int I = nVar.I();
        f10.setConnectTimeout(I);
        f10.setReadTimeout(I);
        f10.setUseCaches(false);
        f10.setDoInput(true);
        if ("https".equals(url.getProtocol()) && (sSLSocketFactory = this.f18499a) != null) {
            ((HttpsURLConnection) f10).setSSLSocketFactory(sSLSocketFactory);
        }
        return f10;
    }

    @Override // fh.a
    public f a(n nVar, Map map) {
        String K = nVar.K();
        HashMap hashMap = new HashMap();
        hashMap.putAll(map);
        hashMap.putAll(nVar.z());
        HttpURLConnection k10 = k(new URL(K), nVar);
        try {
            for (String str : hashMap.keySet()) {
                k10.setRequestProperty(str, (String) hashMap.get(str));
            }
            l(k10, nVar);
            int responseCode = k10.getResponseCode();
            if (responseCode == -1) {
                throw new IOException("Could not retrieve response code from HttpUrlConnection.");
            }
            if (i(nVar.A(), responseCode)) {
                return new f(responseCode, e(k10.getHeaderFields()), k10.getContentLength(), g(nVar, k10));
            }
            f fVar = new f(responseCode, e(k10.getHeaderFields()));
            k10.disconnect();
            return fVar;
        } catch (Throwable th2) {
            if (0 == 0) {
                k10.disconnect();
            }
            throw th2;
        }
    }

    protected HttpURLConnection f(URL url) {
        HttpURLConnection httpURLConnection = (HttpURLConnection) ((URLConnection) FirebasePerfUrlConnection.instrument(url.openConnection()));
        httpURLConnection.setInstanceFollowRedirects(HttpURLConnection.getFollowRedirects());
        return httpURLConnection;
    }

    protected InputStream g(n nVar, HttpURLConnection httpURLConnection) {
        return new a(httpURLConnection);
    }

    protected OutputStream h(n nVar, HttpURLConnection httpURLConnection, int i10) {
        return httpURLConnection.getOutputStream();
    }

    void l(HttpURLConnection httpURLConnection, n nVar) {
        switch (nVar.A()) {
            case -1:
                byte[] D = nVar.D();
                if (D != null) {
                    httpURLConnection.setRequestMethod(FirebasePerformance.HttpMethod.POST);
                    c(httpURLConnection, nVar, D);
                    return;
                }
                return;
            case 0:
                httpURLConnection.setRequestMethod(FirebasePerformance.HttpMethod.GET);
                return;
            case 1:
                httpURLConnection.setRequestMethod(FirebasePerformance.HttpMethod.POST);
                d(httpURLConnection, nVar);
                return;
            case 2:
                httpURLConnection.setRequestMethod(FirebasePerformance.HttpMethod.PUT);
                d(httpURLConnection, nVar);
                return;
            case 3:
                httpURLConnection.setRequestMethod(FirebasePerformance.HttpMethod.DELETE);
                return;
            case 4:
                httpURLConnection.setRequestMethod(FirebasePerformance.HttpMethod.HEAD);
                return;
            case 5:
                httpURLConnection.setRequestMethod(FirebasePerformance.HttpMethod.OPTIONS);
                return;
            case 6:
                httpURLConnection.setRequestMethod(FirebasePerformance.HttpMethod.TRACE);
                return;
            case 7:
                httpURLConnection.setRequestMethod(FirebasePerformance.HttpMethod.PATCH);
                d(httpURLConnection, nVar);
                return;
            default:
                throw new IllegalStateException("Unknown method type.");
        }
    }

    public h(b bVar) {
        this(bVar, null);
    }

    public h(b bVar, SSLSocketFactory sSLSocketFactory) {
        this.f18499a = sSLSocketFactory;
    }
}
