package com.fyber.inneractive.sdk.network; import android.net.Uri; import android.text.TextUtils; import android.util.Pair; import com.fyber.inneractive.sdk.util.IAlog; import com.google.firebase.perf.network.FirebasePerfUrlConnection; import java.io.BufferedInputStream; import java.io.FilterInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.SocketTimeoutException; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.zip.GZIPInputStream; import org.apache.http.protocol.HTTP; /* loaded from: classes2.dex */ public final class i extends h implements g { public static class a extends j { public final HttpURLConnection g; public a(HttpURLConnection httpURLConnection, int i, InputStream inputStream, Map> map, String str) { this.g = httpURLConnection; a(i); a(inputStream); a(map); a(str); } @Override // com.fyber.inneractive.sdk.network.j public final void a() throws IOException { try { HttpURLConnection httpURLConnection = this.g; if (httpURLConnection != null) { httpURLConnection.disconnect(); } } catch (Throwable unused) { } super.a(); } } @Override // com.fyber.inneractive.sdk.network.g public final j a(c0 c0Var, String str, String str2) throws Exception { j a2; FilterInputStream bufferedInputStream; IAlog.a("%s hurl network stack is in use", "HttpExecutorImpl"); URL url = new URL(c0Var.getUrl()); ArrayList arrayList = new ArrayList(); arrayList.add(c0Var.getUrl()); Pair a3 = a(c0Var, arrayList, url, str, str2); HttpURLConnection httpURLConnection = (HttpURLConnection) a3.second; List list = (List) a3.first; if (httpURLConnection == null) { try { if (list.size() <= 0) { throw new x0(); } } catch (SocketTimeoutException e) { throw new a1(e); } catch (IOException e2) { IAlog.a("failed reading network response", e2, new Object[0]); throw new x0(e2); } } FilterInputStream filterInputStream = null; if (httpURLConnection == null) { a2 = h.a(null, 200, "", null, null); } else { try { InputStream inputStream = httpURLConnection.getInputStream(); if (TextUtils.equals("gzip", httpURLConnection.getContentEncoding())) { IAlog.a("HttpExecutorBase: getInputStream found gzip encoding", new Object[0]); bufferedInputStream = new GZIPInputStream(inputStream); } else { IAlog.a("HttpExecutorBase: getInputStream no gzip encoding", new Object[0]); bufferedInputStream = new BufferedInputStream(inputStream); } filterInputStream = bufferedInputStream; } catch (Exception unused) { } a2 = h.a(filterInputStream, httpURLConnection.getResponseCode(), httpURLConnection.getResponseMessage(), httpURLConnection.getHeaderFields(), httpURLConnection.getHeaderField("Last-Modified")); } a aVar = new a(httpURLConnection, a2.a, a2.c, a2.d, a2.e); Iterator it = list.iterator(); while (it.hasNext()) { aVar.f.add((String) it.next()); } return aVar; } @Override // com.fyber.inneractive.sdk.network.g public final void a() { } public static Pair a(c0 c0Var, ArrayList arrayList, URL url, String str, String str2) throws b { String headerField; try { HttpURLConnection httpURLConnection = (HttpURLConnection) ((URLConnection) FirebasePerfUrlConnection.instrument(url.openConnection())); if (!c0Var.e()) { httpURLConnection.setInstanceFollowRedirects(false); } u0 f = c0Var.f(); httpURLConnection.setConnectTimeout(f.a); httpURLConnection.setReadTimeout(f.b); a(httpURLConnection, "User-Agent", str); a(httpURLConnection, "If-Modified-Since", str2); a(httpURLConnection, "Accept-Encoding", "gzip"); Map k = c0Var.k(); if (k != null) { for (String str3 : k.keySet()) { a(httpURLConnection, str3, k.get(str3)); } } c0Var.b(System.currentTimeMillis()); if (c0Var.getMethod() != z.POST && c0Var.getMethod() != z.PUT) { httpURLConnection.connect(); int responseCode = httpURLConnection.getResponseCode(); c0Var.a(System.currentTimeMillis()); IAlog.a("%s (%s) response code - %d", c0Var.toString(), url, Integer.valueOf(responseCode)); if (c0Var.e() && (((responseCode > 300 && responseCode < 304) || responseCode == 307 || responseCode == 308) && (headerField = httpURLConnection.getHeaderField("Location")) != null)) { if (!headerField.startsWith("http") && !headerField.contains("://") && arrayList.size() > 0) { Uri parse = Uri.parse((String) arrayList.get(arrayList.size() - 1)); headerField = String.format(headerField.startsWith("/") ? "%s://%s%s" : "%s://%s/%s", parse.getScheme(), parse.getHost(), headerField); } arrayList.add(headerField); if (arrayList.size() <= 20) { if (headerField.startsWith("http")) { return a(c0Var, arrayList, new URL(headerField), str, str2); } return Pair.create(arrayList, null); } throw new b("Url chain too big for us"); } return new Pair(arrayList, httpURLConnection); } a(httpURLConnection, c0Var); int responseCode2 = httpURLConnection.getResponseCode(); c0Var.a(System.currentTimeMillis()); IAlog.a("%s (%s) response code - %d", c0Var.toString(), url, Integer.valueOf(responseCode2)); if (c0Var.e()) { } return new Pair(arrayList, httpURLConnection); } catch (Exception e) { c0Var.a(System.currentTimeMillis()); IAlog.a("failed executing network request", e, new Object[0]); throw new b(e); } } public static void a(HttpURLConnection httpURLConnection, c0 c0Var) throws Exception { httpURLConnection.setRequestMethod(c0Var.getMethod().key); httpURLConnection.setDoInput(true); httpURLConnection.setDoOutput(true); byte[] r = c0Var.r(); httpURLConnection.setRequestProperty(HTTP.CONTENT_LEN, "" + (r != null ? r.length : 0)); httpURLConnection.setRequestProperty("Content-Type", c0Var.q()); httpURLConnection.connect(); OutputStream outputStream = httpURLConnection.getOutputStream(); outputStream.write(r); try { outputStream.close(); } catch (Throwable unused) { } } public static void a(HttpURLConnection httpURLConnection, String str, String str2) { if (TextUtils.isEmpty(str) || TextUtils.isEmpty(str2)) { return; } httpURLConnection.addRequestProperty(str, str2); } }