package c.i.a.c.h2; import android.net.Uri; import android.text.TextUtils; import android.util.Log; import androidx.annotation.Nullable; import c.i.a.c.i2.f0; import c.i.a.c.i2.p; import c.i.b.a.h; import com.google.android.exoplayer2.upstream.DataSourceException; import com.google.android.exoplayer2.upstream.HttpDataSource$CleartextNotPermittedException; import com.google.android.exoplayer2.upstream.HttpDataSource$HttpDataSourceException; import com.google.android.exoplayer2.upstream.HttpDataSource$InvalidContentTypeException; import com.google.android.exoplayer2.upstream.HttpDataSource$InvalidResponseCodeException; import java.io.EOFException; import java.io.IOException; import java.io.InputStream; import java.io.InterruptedIOException; import java.io.OutputStream; import java.lang.reflect.Method; import java.net.HttpURLConnection; import java.net.NoRouteToHostException; import java.net.ProtocolException; import java.net.URL; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.zip.GZIPInputStream; /* compiled from: DefaultHttpDataSource */ public class q extends e implements j { public static final Pattern e = Pattern.compile("^bytes (\\d+)-(\\d+)/(\\d+)$"); public final boolean f; public final int g; public final int h; @Nullable public final String i; @Nullable public final t j; public final t k = new t(); @Nullable public h l = null; @Nullable public l m; @Nullable public HttpURLConnection n; @Nullable public InputStream o; public byte[] p; public boolean q; public int r; /* renamed from: s reason: collision with root package name */ public long f889s; public long t; public long u; public long v; /* compiled from: DefaultHttpDataSource */ public static final class b implements s { public final t a = new t(); @Nullable public String b; /* renamed from: c reason: collision with root package name */ public int f890c = 8000; public int d = 8000; /* renamed from: b */ public q a() { return new q(this.b, this.f890c, this.d, false, this.a, null, null); } } public q(String str, int i, int i2, boolean z2, t tVar, h hVar, a aVar) { super(true); this.i = str; this.g = i; this.h = i2; this.f = z2; this.j = tVar; } public static URL t(URL url, @Nullable String str) throws IOException { if (str != null) { URL url2 = new URL(url, str); String protocol = url2.getProtocol(); if ("https".equals(protocol) || "http".equals(protocol)) { return url2; } throw new ProtocolException(c.d.b.a.a.s("Unsupported protocol redirect: ", protocol)); } throw new ProtocolException("Null location redirect"); } public static void w(@Nullable HttpURLConnection httpURLConnection, long j) { int i; if (httpURLConnection != null && (i = f0.a) >= 19 && i <= 20) { try { InputStream inputStream = httpURLConnection.getInputStream(); if (j == -1) { if (inputStream.read() == -1) { return; } } else if (j <= 2048) { return; } String name = inputStream.getClass().getName(); if ("com.android.okhttp.internal.http.HttpTransport$ChunkedInputStream".equals(name) || "com.android.okhttp.internal.http.HttpTransport$FixedLengthInputStream".equals(name)) { Class superclass = inputStream.getClass().getSuperclass(); Objects.requireNonNull(superclass); Method declaredMethod = superclass.getDeclaredMethod("unexpectedEndOfInput", new Class[0]); declaredMethod.setAccessible(true); declaredMethod.invoke(inputStream, new Object[0]); } } catch (Exception unused) { } } } /* JADX WARNING: Code restructure failed: missing block: B:18:0x004e, code lost: if (r7 != 0) goto L_0x0052; */ /* JADX WARNING: Removed duplicated region for block: B:34:0x00a7 */ /* JADX WARNING: Removed duplicated region for block: B:48:0x011b */ /* JADX WARNING: Removed duplicated region for block: B:49:0x0120 */ @Override // c.i.a.c.h2.j public long a(l lVar) throws HttpDataSource$HttpDataSourceException { byte[] bArr; long j; long j2; String headerField; this.m = lVar; this.v = 0; this.u = 0; q(lVar); try { HttpURLConnection u = u(lVar); this.n = u; try { this.r = u.getResponseCode(); String responseMessage = u.getResponseMessage(); int i = this.r; if (i < 200 || i > 299) { Map> headerFields = u.getHeaderFields(); InputStream errorStream = u.getErrorStream(); if (errorStream != null) { try { bArr = f0.J(errorStream); } catch (IOException unused) { bArr = f0.f; } } else { bArr = f0.f; } s(); HttpDataSource$InvalidResponseCodeException httpDataSource$InvalidResponseCodeException = new HttpDataSource$InvalidResponseCodeException(this.r, responseMessage, headerFields, lVar, bArr); if (this.r == 416) { httpDataSource$InvalidResponseCodeException.initCause(new DataSourceException(0)); } throw httpDataSource$InvalidResponseCodeException; } String contentType = u.getContentType(); h hVar = this.l; if (hVar == null || hVar.apply(contentType)) { if (this.r == 200) { j = lVar.f; } j = 0; this.f889s = j; boolean equalsIgnoreCase = "gzip".equalsIgnoreCase(u.getHeaderField("Content-Encoding")); if (!equalsIgnoreCase) { long j3 = lVar.g; if (j3 != -1) { this.t = j3; } else { String headerField2 = u.getHeaderField("Content-Length"); if (!TextUtils.isEmpty(headerField2)) { try { j2 = Long.parseLong(headerField2); } catch (NumberFormatException unused2) { Log.e("DefaultHttpDataSource", "Unexpected Content-Length [" + headerField2 + "]"); } headerField = u.getHeaderField("Content-Range"); if (!TextUtils.isEmpty(headerField)) { Matcher matcher = e.matcher(headerField); if (matcher.find()) { try { String group = matcher.group(2); Objects.requireNonNull(group); long parseLong = Long.parseLong(group); String group2 = matcher.group(1); Objects.requireNonNull(group2); long parseLong2 = (parseLong - Long.parseLong(group2)) + 1; if (j2 < 0) { j2 = parseLong2; } else if (j2 != parseLong2) { Log.w("DefaultHttpDataSource", "Inconsistent headers [" + headerField2 + "] [" + headerField + "]"); j2 = Math.max(j2, parseLong2); } } catch (NumberFormatException unused3) { Log.e("DefaultHttpDataSource", "Unexpected Content-Range [" + headerField + "]"); } } } this.t = j2 == -1 ? j2 - this.f889s : -1; } j2 = -1; headerField = u.getHeaderField("Content-Range"); if (!TextUtils.isEmpty(headerField)) { } this.t = j2 == -1 ? j2 - this.f889s : -1; } } else { this.t = lVar.g; } try { this.o = u.getInputStream(); if (equalsIgnoreCase) { this.o = new GZIPInputStream(this.o); } this.q = true; r(lVar); return this.t; } catch (IOException e2) { s(); throw new HttpDataSource$HttpDataSourceException(e2, lVar, 1); } } else { s(); throw new HttpDataSource$InvalidContentTypeException(contentType, lVar); } } catch (IOException e3) { s(); throw new HttpDataSource$HttpDataSourceException("Unable to connect", e3, lVar, 1); } } catch (IOException e4) { String message = e4.getMessage(); if (message == null || !f0.K(message).matches("cleartext http traffic.*not permitted.*")) { throw new HttpDataSource$HttpDataSourceException("Unable to connect", e4, lVar, 1); } throw new HttpDataSource$CleartextNotPermittedException(e4, lVar); } } @Override // c.i.a.c.h2.j public void close() throws HttpDataSource$HttpDataSourceException { boolean z2 = false; InputStream inputStream = null; try { InputStream inputStream2 = this.o; if (inputStream2 != null) { HttpURLConnection httpURLConnection = this.n; long j = this.t; if (j != -1) { j -= this.v; } w(httpURLConnection, j); try { inputStream2.close(); } catch (IOException e2) { l lVar = this.m; int i = f0.a; throw new HttpDataSource$HttpDataSourceException(e2, lVar, 3); } } } finally { this.o = inputStream; s(); if (this.q) { this.q = z2; p(); } } } @Override // c.i.a.c.h2.e, c.i.a.c.h2.j public Map> j() { HttpURLConnection httpURLConnection = this.n; return httpURLConnection == null ? Collections.emptyMap() : httpURLConnection.getHeaderFields(); } @Override // c.i.a.c.h2.j @Nullable public Uri m() { HttpURLConnection httpURLConnection = this.n; if (httpURLConnection == null) { return null; } return Uri.parse(httpURLConnection.getURL().toString()); } @Override // c.i.a.c.h2.f public int read(byte[] bArr, int i, int i2) throws HttpDataSource$HttpDataSourceException { try { x(); if (i2 == 0) { return 0; } long j = this.t; if (j != -1) { long j2 = j - this.v; if (j2 != 0) { i2 = (int) Math.min((long) i2, j2); } return -1; } InputStream inputStream = this.o; int i3 = f0.a; int read = inputStream.read(bArr, i, i2); if (read != -1) { this.v += (long) read; o(read); return read; } else if (this.t == -1) { return -1; } else { throw new EOFException(); } } catch (IOException e2) { l lVar = this.m; int i4 = f0.a; throw new HttpDataSource$HttpDataSourceException(e2, lVar, 2); } } public final void s() { HttpURLConnection httpURLConnection = this.n; if (httpURLConnection != null) { try { httpURLConnection.disconnect(); } catch (Exception e2) { p.b("DefaultHttpDataSource", "Unexpected error while disconnecting", e2); } this.n = null; } } public final HttpURLConnection u(l lVar) throws IOException { HttpURLConnection v; l lVar2 = lVar; URL url = new URL(lVar2.a.toString()); int i = lVar2.f882c; byte[] bArr = lVar2.d; long j = lVar2.f; long j2 = lVar2.g; boolean c2 = lVar2.c(1); if (!this.f) { return v(url, i, bArr, j, j2, c2, true, lVar2.e); } int i2 = 0; while (true) { int i3 = i2 + 1; if (i2 <= 20) { v = v(url, i, bArr, j, j2, c2, false, lVar2.e); int responseCode = v.getResponseCode(); String headerField = v.getHeaderField("Location"); if ((i == 1 || i == 3) && (responseCode == 300 || responseCode == 301 || responseCode == 302 || responseCode == 303 || responseCode == 307 || responseCode == 308)) { v.disconnect(); url = t(url, headerField); } else if (i != 2 || (responseCode != 300 && responseCode != 301 && responseCode != 302 && responseCode != 303)) { break; } else { v.disconnect(); url = t(url, headerField); i = 1; bArr = null; } lVar2 = lVar; i2 = i3; j2 = j2; j = j; } else { throw new NoRouteToHostException(c.d.b.a.a.j("Too many redirects: ", i3)); } } return v; } public final HttpURLConnection v(URL url, int i, @Nullable byte[] bArr, long j, long j2, boolean z2, boolean z3, Map map) throws IOException { HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setConnectTimeout(this.g); httpURLConnection.setReadTimeout(this.h); HashMap hashMap = new HashMap(); t tVar = this.j; if (tVar != null) { hashMap.putAll(tVar.a()); } hashMap.putAll(this.k.a()); hashMap.putAll(map); for (Map.Entry entry : hashMap.entrySet()) { httpURLConnection.setRequestProperty((String) entry.getKey(), (String) entry.getValue()); } if (!(j == 0 && j2 == -1)) { String p = c.d.b.a.a.p("bytes=", j, "-"); if (j2 != -1) { StringBuilder K = c.d.b.a.a.K(p); K.append((j + j2) - 1); p = K.toString(); } httpURLConnection.setRequestProperty("Range", p); } String str = this.i; if (str != null) { httpURLConnection.setRequestProperty("User-Agent", str); } httpURLConnection.setRequestProperty("Accept-Encoding", z2 ? "gzip" : "identity"); httpURLConnection.setInstanceFollowRedirects(z3); httpURLConnection.setDoOutput(bArr != null); httpURLConnection.setRequestMethod(l.b(i)); if (bArr != null) { httpURLConnection.setFixedLengthStreamingMode(bArr.length); httpURLConnection.connect(); OutputStream outputStream = httpURLConnection.getOutputStream(); outputStream.write(bArr); outputStream.close(); } else { httpURLConnection.connect(); } return httpURLConnection; } public final void x() throws IOException { if (this.u != this.f889s) { if (this.p == null) { this.p = new byte[4096]; } while (true) { long j = this.u; long j2 = this.f889s; if (j != j2) { int min = (int) Math.min(j2 - j, (long) this.p.length); InputStream inputStream = this.o; int i = f0.a; int read = inputStream.read(this.p, 0, min); if (Thread.currentThread().isInterrupted()) { throw new InterruptedIOException(); } else if (read != -1) { this.u += (long) read; o(read); } else { throw new EOFException(); } } else { return; } } } } }