package com.helpshift.network; import androidx.webkit.ProxyConfig; import com.google.firebase.perf.network.FirebasePerfUrlConnection; import com.helpshift.log.HSLogger; import com.helpshift.util.ListUtil; import com.helpshift.util.Utils; import com.ironsource.nb; import com.mbridge.msdk.foundation.download.database.DownloadModel; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.net.URLConnection; import java.net.UnknownHostException; import java.util.List; import java.util.Map; import java.util.zip.GZIPInputStream; import javax.net.ssl.HttpsURLConnection; import org.json.JSONObject; /* loaded from: classes3.dex */ public class HSDownloaderNetwork { public final URLConnectionProvider urlConnectionProvider; public HSDownloaderNetwork(URLConnectionProvider uRLConnectionProvider) { this.urlConnectionProvider = uRLConnectionProvider; } /* JADX WARN: Multi-variable type inference failed */ /* JADX WARN: Type inference failed for: r10v0 */ /* JADX WARN: Type inference failed for: r10v13 */ /* JADX WARN: Type inference failed for: r10v18 */ /* JADX WARN: Type inference failed for: r10v2, types: [java.io.Closeable] */ /* JADX WARN: Type inference failed for: r10v7 */ public HSDownloaderResponse downloadResource(String str, Map map, File file) { String str2; boolean z; Exception exc; String str3; UnknownHostException unknownHostException; ?? r10; FileOutputStream fileOutputStream; boolean z2; String str4; String str5; String str6; FileOutputStream fileOutputStream2; str2 = ""; String str7 = nb.N; JSONObject jSONObject = new JSONObject(); int i = 404; try { try { try { HttpsURLConnection httpsURLConnection = (HttpsURLConnection) ((URLConnection) FirebasePerfUrlConnection.instrument(this.urlConnectionProvider.getURL(str).openConnection())); map.put("Accept-Encoding", "gzip"); for (Map.Entry entry : map.entrySet()) { try { httpsURLConnection.setRequestProperty((String) entry.getKey(), (String) entry.getValue()); } catch (UnknownHostException e) { unknownHostException = e; str3 = ""; z = false; fileOutputStream = null; HSLogger.e("dwnldrNet", "Error downloading resource: " + str + " \n " + unknownHostException.getMessage()); Utils.closeQuietly(fileOutputStream); r10 = str3; str5 = str2; str4 = str7; z2 = z; str6 = r10; return new HSDownloaderResponse(i, jSONObject, str6, str5, str4, z2); } catch (Exception e2) { exc = e2; str3 = ""; z = false; fileOutputStream = null; HSLogger.e("dwnldrNet", "Error downloading resource: " + str, exc); Utils.closeQuietly(fileOutputStream); r10 = str3; str5 = str2; str4 = str7; z2 = z; str6 = r10; return new HSDownloaderResponse(i, jSONObject, str6, str5, str4, z2); } } i = httpsURLConnection.getResponseCode(); str7 = httpsURLConnection.getContentEncoding(); String contentType = httpsURLConnection.getContentType(); try { Map> headerFields = httpsURLConnection.getHeaderFields(); for (Map.Entry> entry2 : headerFields.entrySet()) { try { if (entry2.getKey() != null || entry2.getValue() != null) { if (entry2.getKey() == null) { jSONObject.put("", generateHeaderValue(entry2.getValue())); } else { jSONObject.put(entry2.getKey(), generateHeaderValue(entry2.getValue())); if (entry2.getKey().equalsIgnoreCase("Access-Control-Allow-Origin")) { jSONObject.put(entry2.getKey(), ProxyConfig.MATCH_ALL_SCHEMES); } } } } catch (UnknownHostException e3) { unknownHostException = e3; str3 = str2; str2 = contentType; z = false; fileOutputStream = null; HSLogger.e("dwnldrNet", "Error downloading resource: " + str + " \n " + unknownHostException.getMessage()); Utils.closeQuietly(fileOutputStream); r10 = str3; str5 = str2; str4 = str7; z2 = z; str6 = r10; return new HSDownloaderResponse(i, jSONObject, str6, str5, str4, z2); } catch (Exception e4) { exc = e4; str3 = str2; str2 = contentType; z = false; fileOutputStream = null; HSLogger.e("dwnldrNet", "Error downloading resource: " + str, exc); Utils.closeQuietly(fileOutputStream); r10 = str3; str5 = str2; str4 = str7; z2 = z; str6 = r10; return new HSDownloaderResponse(i, jSONObject, str6, str5, str4, z2); } } List list = headerFields.get(DownloadModel.ETAG); str2 = ListUtil.isNotEmpty(list) ? list.get(0) : ""; if (i < 200 || i > 300) { z = false; fileOutputStream2 = null; } else { InputStream inputStream = httpsURLConnection.getInputStream(); if (Utils.isNotEmpty(str7) && str7.contains("gzip")) { inputStream = new GZIPInputStream(inputStream); } FileOutputStream fileOutputStream3 = new FileOutputStream(file); try { try { byte[] bArr = new byte[8192]; while (true) { int read = inputStream.read(bArr); if (read == -1) { break; } z = false; try { fileOutputStream3.write(bArr, 0, read); } catch (UnknownHostException e5) { e = e5; fileOutputStream = fileOutputStream3; unknownHostException = e; str3 = str2; str2 = contentType; HSLogger.e("dwnldrNet", "Error downloading resource: " + str + " \n " + unknownHostException.getMessage()); Utils.closeQuietly(fileOutputStream); r10 = str3; str5 = str2; str4 = str7; z2 = z; str6 = r10; return new HSDownloaderResponse(i, jSONObject, str6, str5, str4, z2); } catch (Exception e6) { e = e6; fileOutputStream = fileOutputStream3; exc = e; str3 = str2; str2 = contentType; HSLogger.e("dwnldrNet", "Error downloading resource: " + str, exc); Utils.closeQuietly(fileOutputStream); r10 = str3; str5 = str2; str4 = str7; z2 = z; str6 = r10; return new HSDownloaderResponse(i, jSONObject, str6, str5, str4, z2); } } z = false; fileOutputStream2 = fileOutputStream3; } catch (Throwable th) { th = th; r10 = fileOutputStream3; Utils.closeQuietly(r10); throw th; } } catch (UnknownHostException e7) { e = e7; z = false; } catch (Exception e8) { e = e8; z = false; } } if ((i >= 200 && i <= 300) || i == 304) { try { HSLogger.d("dwnldrNet", "Successfully downloaded the resource with Url: " + str + " headers: " + map); z = true; } catch (UnknownHostException e9) { unknownHostException = e9; str3 = str2; str2 = contentType; fileOutputStream = fileOutputStream2; HSLogger.e("dwnldrNet", "Error downloading resource: " + str + " \n " + unknownHostException.getMessage()); Utils.closeQuietly(fileOutputStream); r10 = str3; str5 = str2; str4 = str7; z2 = z; str6 = r10; return new HSDownloaderResponse(i, jSONObject, str6, str5, str4, z2); } catch (Exception e10) { exc = e10; str3 = str2; str2 = contentType; fileOutputStream = fileOutputStream2; HSLogger.e("dwnldrNet", "Error downloading resource: " + str, exc); Utils.closeQuietly(fileOutputStream); r10 = str3; str5 = str2; str4 = str7; z2 = z; str6 = r10; return new HSDownloaderResponse(i, jSONObject, str6, str5, str4, z2); } catch (Throwable th2) { th = th2; r10 = fileOutputStream2; Utils.closeQuietly(r10); throw th; } } Utils.closeQuietly(fileOutputStream2); str6 = str2; z2 = z; str5 = contentType; str4 = str7; } catch (UnknownHostException e11) { z = false; unknownHostException = e11; str3 = str2; str2 = contentType; fileOutputStream = null; HSLogger.e("dwnldrNet", "Error downloading resource: " + str + " \n " + unknownHostException.getMessage()); Utils.closeQuietly(fileOutputStream); r10 = str3; str5 = str2; str4 = str7; z2 = z; str6 = r10; return new HSDownloaderResponse(i, jSONObject, str6, str5, str4, z2); } catch (Exception e12) { z = false; exc = e12; str3 = str2; str2 = contentType; fileOutputStream = null; HSLogger.e("dwnldrNet", "Error downloading resource: " + str, exc); Utils.closeQuietly(fileOutputStream); r10 = str3; str5 = str2; str4 = str7; z2 = z; str6 = r10; return new HSDownloaderResponse(i, jSONObject, str6, str5, str4, z2); } } catch (Throwable th3) { th = th3; } } catch (Throwable th4) { th = th4; r10 = 0; } } catch (UnknownHostException e13) { z = false; unknownHostException = e13; str3 = ""; } catch (Exception e14) { z = false; exc = e14; str3 = ""; } return new HSDownloaderResponse(i, jSONObject, str6, str5, str4, z2); } public static String generateHeaderValue(List list) { if (ListUtil.isEmpty(list)) { return ""; } StringBuilder sb = new StringBuilder((String) list.get(0)); for (int i = 1; i < list.size(); i++) { sb.append(";"); sb.append((String) list.get(i)); } return sb.toString(); } }