package com.danikula.videocache; import a.f; import a40.j; import android.text.TextUtils; import androidx.core.view.accessibility.AccessibilityEventCompat; import com.danikula.videocache.cacheHeader.IHeadCache; import com.danikula.videocache.headers.EmptyHeadersInjector; import com.danikula.videocache.headers.HeaderInjector; import com.danikula.videocache.interfacers.Source; import com.danikula.videocache.sourcestorage.SourceInfoStorage; import com.danikula.videocache.sourcestorage.SourceInfoStorageFactory; import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InterruptedIOException; import java.net.HttpURLConnection; import java.net.URL; import java.util.Map; import java.util.Objects; import org.slf4j.Logger; import rm1.a; public class HttpUrlSource implements Source { private static final Logger LOG = a.e("HttpUrlSource"); private HttpURLConnection connection; private DownloadManger downloadManger; private IHeadCache duHeadCache; private final HeaderInjector headerInjector; private InputStream inputStream; private SourceInfo sourceInfo; private final SourceInfoStorage sourceInfoStorage; public HttpUrlSource(String str) { this(str, SourceInfoStorageFactory.newEmptySourceInfoStorage()); } /* JADX WARNING: Removed duplicated region for block: B:21:0x00a2 */ /* JADX WARNING: Removed duplicated region for block: B:24:? A[RETURN, SYNTHETIC] */ private void fetchContentInfo() throws ProxyCacheException { HttpURLConnection httpURLConnection; Throwable th2; HttpURLConnection httpURLConnection2; IOException e; String str = this.sourceInfo.url; Logger logger = LOG; StringBuilder h12 = f.h("Read content info from "); h12.append(this.sourceInfo.url); logger.debug(h12.toString()); String str2 = this.sourceInfo.url; InputStream inputStream2 = null; try { httpURLConnection2 = openConnectionForHeader(0, 10000); try { long contentLength = getContentLength(httpURLConnection2); String contentType = httpURLConnection2.getContentType(); inputStream2 = httpURLConnection2.getInputStream(); SourceInfo sourceInfo2 = new SourceInfo(this.sourceInfo.url, contentLength, contentType); this.sourceInfo = sourceInfo2; this.sourceInfoStorage.put(sourceInfo2.url, sourceInfo2); putSourceInfo(); logger.debug("Source info fetched: " + this.sourceInfo); Objects.toString(this.sourceInfo); ProxyCacheUtils.close(inputStream2); } catch (IOException e6) { e = e6; try { LOG.error("Error fetching info from " + this.sourceInfo.url, (Throwable) e); String str3 = this.sourceInfo.url; ProxyCacheUtils.close(inputStream2); if (httpURLConnection2 == null) { return; } httpURLConnection2.disconnect(); } catch (Throwable th3) { th2 = th3; inputStream2 = inputStream2; httpURLConnection = httpURLConnection2; ProxyCacheUtils.close(inputStream2); if (httpURLConnection != null) { httpURLConnection.disconnect(); } throw th2; } } } catch (IOException e12) { e = e12; httpURLConnection2 = null; LOG.error("Error fetching info from " + this.sourceInfo.url, (Throwable) e); String str32 = this.sourceInfo.url; ProxyCacheUtils.close(inputStream2); if (httpURLConnection2 == null) { } httpURLConnection2.disconnect(); } catch (Throwable th4) { th2 = th4; httpURLConnection = null; ProxyCacheUtils.close(inputStream2); if (httpURLConnection != null) { } throw th2; } httpURLConnection2.disconnect(); } private long getContentLength(HttpURLConnection httpURLConnection) { String headerField = httpURLConnection.getHeaderField("Content-Length"); if (headerField == null) { return -1; } return Long.parseLong(headerField); } private void injectCustomHeaders(HttpURLConnection httpURLConnection, String str) { for (Map.Entry<String, String> entry : this.headerInjector.addHeaders(str).entrySet()) { httpURLConnection.setRequestProperty(entry.getKey(), entry.getValue()); } } private void loadSourceInfo() { SourceInfo sourceInfo2; IHeadCache iHeadCache = this.duHeadCache; if (iHeadCache != null && (sourceInfo2 = iHeadCache.get(getUrl())) != null) { this.sourceInfo = sourceInfo2; } } private HttpURLConnection openConnection(long j, int i) throws IOException, ProxyCacheException { HttpURLConnection httpURLConnection; boolean z; String str = this.sourceInfo.url; int i3 = 0; do { Logger logger = LOG; StringBuilder h12 = f.h("Open connection "); int i12 = (j > 0 ? 1 : (j == 0 ? 0 : -1)); h12.append(i12 > 0 ? a0.a.e(" with offset ", j) : ""); h12.append(" to "); h12.append(str); logger.debug(h12.toString()); httpURLConnection = (HttpURLConnection) new URL(str).openConnection(); injectCustomHeaders(httpURLConnection, str); if (i12 > 0) { httpURLConnection.setRequestProperty("Range", a40.f.f("bytes=", j, "-")); } if (i > 0) { httpURLConnection.setConnectTimeout(i); httpURLConnection.setReadTimeout(i); } int responseCode = httpURLConnection.getResponseCode(); z = responseCode == 301 || responseCode == 302 || responseCode == 303; if (z) { str = httpURLConnection.getHeaderField("Location"); i3++; httpURLConnection.disconnect(); } if (i3 > 5) { throw new ProxyCacheException(j.h("Too many redirects: ", i3)); } } while (z); return httpURLConnection; } private HttpURLConnection openConnectionForHeader(long j, int i) throws IOException, ProxyCacheException { HttpURLConnection httpURLConnection; boolean z; String str = this.sourceInfo.url; int i3 = 0; do { Logger logger = LOG; StringBuilder h12 = f.h("Open connection "); int i12 = (j > 0 ? 1 : (j == 0 ? 0 : -1)); h12.append(i12 > 0 ? a0.a.e(" with offset ", j) : ""); h12.append(" to "); h12.append(str); logger.debug(h12.toString()); httpURLConnection = (HttpURLConnection) new URL(str).openConnection(); injectCustomHeaders(httpURLConnection, str); if (i12 > 0) { httpURLConnection.setRequestProperty("Range", a40.f.f("bytes=", j, "-")); } if (i > 0) { httpURLConnection.setConnectTimeout(i); httpURLConnection.setReadTimeout(i); } httpURLConnection.setRequestMethod("HEAD"); int responseCode = httpURLConnection.getResponseCode(); z = responseCode == 301 || responseCode == 302 || responseCode == 303; if (z) { str = httpURLConnection.getHeaderField("Location"); i3++; httpURLConnection.disconnect(); } if (i3 > 5) { throw new ProxyCacheException(j.h("Too many redirects: ", i3)); } } while (z); return httpURLConnection; } private void putSourceInfo() { IHeadCache iHeadCache = this.duHeadCache; if (iHeadCache != null) { SourceInfo sourceInfo2 = this.sourceInfo; iHeadCache.put(sourceInfo2.url, sourceInfo2); } } private long readSourceAvailableBytes(HttpURLConnection httpURLConnection, long j, int i) throws IOException { long contentLength = getContentLength(httpURLConnection); if (i == 200) { return contentLength; } if (i == 206) { return contentLength + j; } return this.sourceInfo.length; } @Override // com.danikula.videocache.interfacers.Source public void close() throws ProxyCacheException { HttpURLConnection httpURLConnection = this.connection; if (httpURLConnection != null) { try { httpURLConnection.disconnect(); } catch (IllegalArgumentException | NullPointerException e) { throw new RuntimeException("Wait... but why? WTF!? Really shouldn't happen any more after fixing https://github.com/danikula/AndroidVideoCache/issues/43. If you read it on your device log, please, notify me danikula@gmail.com or create issue here https://github.com/danikula/AndroidVideoCache/issues.", e); } catch (ArrayIndexOutOfBoundsException e6) { LOG.error("Error closing connection correctly. Should happen only on Android L. If anybody know how to fix it, please visit https://github.com/danikula/AndroidVideoCache/issues/88. Until good solution is not know, just ignore this issue :(", (Throwable) e6); } } } public synchronized String getMime() throws ProxyCacheException { if (TextUtils.isEmpty(this.sourceInfo.mime)) { loadSourceInfo(); } if (TextUtils.isEmpty(this.sourceInfo.mime)) { fetchContentInfo(); } return this.sourceInfo.mime; } @Override // com.danikula.videocache.interfacers.Source public String getUrl() { return this.sourceInfo.url; } @Override // com.danikula.videocache.interfacers.Source public synchronized long length() throws ProxyCacheException { if (this.sourceInfo.length == -2147483648L) { loadSourceInfo(); } if (this.sourceInfo.length == -2147483648L) { fetchContentInfo(); } return this.sourceInfo.length; } @Override // com.danikula.videocache.interfacers.Source public void open(long j) throws ProxyCacheException { try { HttpURLConnection openConnection = openConnection(j, -1); this.connection = openConnection; String contentType = openConnection.getContentType(); this.inputStream = new BufferedInputStream(new DownloadInputStream(this.connection.getInputStream(), this.downloadManger), AccessibilityEventCompat.TYPE_VIEW_TEXT_SELECTION_CHANGED); HttpURLConnection httpURLConnection = this.connection; SourceInfo sourceInfo2 = new SourceInfo(this.sourceInfo.url, readSourceAvailableBytes(httpURLConnection, j, httpURLConnection.getResponseCode()), contentType); this.sourceInfo = sourceInfo2; this.sourceInfoStorage.put(sourceInfo2.url, sourceInfo2); } catch (IOException e) { StringBuilder h12 = f.h("Error opening connection for "); h12.append(this.sourceInfo.url); h12.append(" with offset "); h12.append(j); throw new ProxyCacheException(h12.toString(), e); } catch (NumberFormatException e6) { throw new ProxyCacheException("Error format for contentLength", e6); } } @Override // com.danikula.videocache.interfacers.Source public int read(byte[] bArr) throws ProxyCacheException { InputStream inputStream2 = this.inputStream; if (inputStream2 != null) { try { return inputStream2.read(bArr, 0, bArr.length); } catch (InterruptedIOException e) { throw new InterruptedProxyCacheException(f.g(f.h("Reading source "), this.sourceInfo.url, " is interrupted"), e); } catch (IOException e6) { StringBuilder h12 = f.h("Error reading data from "); h12.append(this.sourceInfo.url); throw new ProxyCacheException(h12.toString(), e6); } } else { throw new ProxyCacheException(f.g(f.h("Error reading data from "), this.sourceInfo.url, ": connection is absent!")); } } @Override // com.danikula.videocache.interfacers.Source public void refreshStartTab() { } public String toString() { StringBuilder h12 = f.h("HttpUrlSource{sourceInfo='"); h12.append(this.sourceInfo); h12.append("}"); return h12.toString(); } public HttpUrlSource(String str, SourceInfoStorage sourceInfoStorage2) { this(str, sourceInfoStorage2, new EmptyHeadersInjector()); } public HttpUrlSource(String str, SourceInfoStorage sourceInfoStorage2, HeaderInjector headerInjector2) { this.sourceInfoStorage = (SourceInfoStorage) Preconditions.checkNotNull(sourceInfoStorage2); this.headerInjector = (HeaderInjector) Preconditions.checkNotNull(headerInjector2); SourceInfo sourceInfo2 = sourceInfoStorage2.get(str); this.sourceInfo = sourceInfo2 == null ? new SourceInfo(str, -2147483648L, ProxyCacheUtils.getSupposablyMime(str)) : sourceInfo2; } public HttpUrlSource(String str, SourceInfoStorage sourceInfoStorage2, HeaderInjector headerInjector2, IHeadCache iHeadCache) { this.sourceInfoStorage = (SourceInfoStorage) Preconditions.checkNotNull(sourceInfoStorage2); this.headerInjector = (HeaderInjector) Preconditions.checkNotNull(headerInjector2); SourceInfo sourceInfo2 = sourceInfoStorage2.get(str); this.sourceInfo = sourceInfo2 == null ? new SourceInfo(str, -2147483648L, ProxyCacheUtils.getSupposablyMime(str)) : sourceInfo2; this.duHeadCache = iHeadCache; } public HttpUrlSource(String str, Config config, IHeadCache iHeadCache) { this(str, config.sourceInfoStorage, config.headerInjector, iHeadCache); long j = config.limitSpeed; this.downloadManger = new DownloadManger((int) j, j); } public HttpUrlSource(HttpUrlSource httpUrlSource) { this.sourceInfo = httpUrlSource.sourceInfo; this.sourceInfoStorage = httpUrlSource.sourceInfoStorage; this.headerInjector = httpUrlSource.headerInjector; } }