package okhttp3; import com.google.common.net.HttpHeaders; import java.io.Closeable; import java.io.File; import java.io.Flushable; import java.io.IOException; import java.security.cert.Certificate; import java.security.cert.CertificateEncodingException; import java.security.cert.CertificateException; import java.security.cert.CertificateFactory; import java.util.ArrayList; import java.util.Collection; import java.util.Comparator; import java.util.Iterator; import java.util.List; import java.util.Set; import java.util.TreeSet; import o.AbstractC15151gif; import o.AbstractC15154gii; import o.C14851gaI; import o.C14866gag; import o.C14953gcr; import o.C14957gcv; import o.C15017gel; import o.C15111ghZ; import o.C15155gij; import o.C15160gio; import o.InterfaceC15148gic; import o.InterfaceC15149gid; import o.InterfaceC15168giw; import o.gaH; import o.gbF; import o.gcH; import o.gdP; import o.gdZ; import o.giC; import o.giD; import o.giE; import okhttp3.Headers; import okhttp3.Request; import okhttp3.Response; import okhttp3.internal.Util; import okhttp3.internal.cache.CacheRequest; import okhttp3.internal.cache.CacheStrategy; import okhttp3.internal.cache.DiskLruCache; import okhttp3.internal.concurrent.TaskRunner; import okhttp3.internal.http.HttpMethod; import okhttp3.internal.http.StatusLine; import okhttp3.internal.io.FileSystem; import okhttp3.internal.platform.Platform; /* loaded from: classes.dex */ public final class Cache implements Closeable, Flushable { public static final Companion Companion = new Companion(null); private static final int ENTRY_BODY = 1; private static final int ENTRY_COUNT = 2; private static final int ENTRY_METADATA = 0; private static final int VERSION = 201105; private final DiskLruCache cache; private int hitCount; private int networkCount; private int requestCount; private int writeAbortCount; private int writeSuccessCount; public Cache(File file, long j, FileSystem fileSystem) { C14957gcv.e(file, ""); C14957gcv.e(fileSystem, ""); this.cache = new DiskLruCache(fileSystem, file, VERSION, 2, j, TaskRunner.INSTANCE); } public final boolean isClosed() { return this.cache.isClosed(); } /* JADX WARN: 'this' call moved to the top of the method (can break code semantics) */ public Cache(File file, long j) { this(file, j, FileSystem.SYSTEM); C14957gcv.e(file, ""); } public final Response get$okhttp(Request request) { C14957gcv.e(request, ""); try { DiskLruCache.Snapshot snapshot = this.cache.get(Companion.key(request.url())); if (snapshot == null) { return null; } try { Entry entry = new Entry(snapshot.getSource(0)); Response response = entry.response(snapshot); if (entry.matches(request, response)) { return response; } ResponseBody body = response.body(); if (body != null) { Util.closeQuietly(body); } return null; } catch (IOException unused) { Util.closeQuietly(snapshot); return null; } } catch (IOException unused2) { } } public final CacheRequest put$okhttp(Response response) { DiskLruCache.Editor editor; C14957gcv.e(response, ""); String method = response.request().method(); if (HttpMethod.INSTANCE.invalidatesCache(response.request().method())) { try { remove$okhttp(response.request()); } catch (IOException unused) { } return null; } if (!C14957gcv.b((Object) method, (Object) "GET")) { return null; } Companion companion = Companion; if (companion.hasVaryAll(response)) { return null; } Entry entry = new Entry(response); try { editor = DiskLruCache.edit$default(this.cache, companion.key(response.request().url()), 0L, 2, null); if (editor == null) { return null; } try { entry.writeTo(editor); return new RealCacheRequest(this, editor); } catch (IOException unused2) { abortQuietly(editor); return null; } } catch (IOException unused3) { editor = null; } } public final void remove$okhttp(Request request) throws IOException { C14957gcv.e(request, ""); this.cache.remove(Companion.key(request.url())); } public final void update$okhttp(Response response, Response response2) { DiskLruCache.Editor editor; C14957gcv.e(response, ""); C14957gcv.e(response2, ""); Entry entry = new Entry(response2); ResponseBody body = response.body(); if (body == null) { throw new NullPointerException("null cannot be cast to non-null type okhttp3.Cache.CacheResponseBody"); } try { editor = ((CacheResponseBody) body).getSnapshot().edit(); if (editor == null) { return; } try { entry.writeTo(editor); editor.commit(); } catch (IOException unused) { abortQuietly(editor); } } catch (IOException unused2) { editor = null; } } private final void abortQuietly(DiskLruCache.Editor editor) { if (editor != null) { try { editor.abort(); } catch (IOException unused) { } } } public final void initialize() throws IOException { this.cache.initialize(); } public final void delete() throws IOException { this.cache.delete(); } public final void evictAll() throws IOException { this.cache.evictAll(); } public final Iterator urls() throws IOException { return new Cache$urls$1(this); } public final int writeAbortCount() { int i; synchronized (this) { i = this.writeAbortCount; } return i; } public final int writeSuccessCount() { int i; synchronized (this) { i = this.writeSuccessCount; } return i; } public final long size() throws IOException { return this.cache.size(); } public final long maxSize() { return this.cache.getMaxSize(); } @Override // java.io.Flushable public final void flush() throws IOException { this.cache.flush(); } @Override // java.io.Closeable, java.lang.AutoCloseable public final void close() throws IOException { this.cache.close(); } public final File directory() { return this.cache.getDirectory(); } /* renamed from: -deprecated_directory, reason: not valid java name */ public final File m368deprecated_directory() { return this.cache.getDirectory(); } public final void trackResponse$okhttp(CacheStrategy cacheStrategy) { synchronized (this) { C14957gcv.e(cacheStrategy, ""); this.requestCount++; if (cacheStrategy.getNetworkRequest() != null) { this.networkCount++; } else if (cacheStrategy.getCacheResponse() != null) { this.hitCount++; } } } public final void trackConditionalCacheHit$okhttp() { synchronized (this) { this.hitCount++; } } public final int networkCount() { int i; synchronized (this) { i = this.networkCount; } return i; } public final int hitCount() { int i; synchronized (this) { i = this.hitCount; } return i; } public final int requestCount() { int i; synchronized (this) { i = this.requestCount; } return i; } /* loaded from: classes.dex */ final class RealCacheRequest implements CacheRequest { private final InterfaceC15168giw body; private final InterfaceC15168giw cacheOut; private boolean done; private final DiskLruCache.Editor editor; final Cache this$0; public RealCacheRequest(Cache cache, DiskLruCache.Editor editor) { C14957gcv.e(cache, ""); C14957gcv.e(editor, ""); this.this$0 = cache; this.editor = editor; InterfaceC15168giw newSink = editor.newSink(1); this.cacheOut = newSink; this.body = new AbstractC15151gif(cache, this, newSink) { // from class: okhttp3.Cache.RealCacheRequest.1 final Cache this$0; final RealCacheRequest this$1; /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */ { super(newSink); this.this$0 = cache; this.this$1 = this; } @Override // o.AbstractC15151gif, o.InterfaceC15168giw, java.io.Closeable, java.lang.AutoCloseable public final void close() throws IOException { Cache cache2 = this.this$0; RealCacheRequest realCacheRequest = this.this$1; synchronized (cache2) { if (realCacheRequest.getDone()) { return; } realCacheRequest.setDone(true); cache2.setWriteSuccessCount$okhttp(cache2.getWriteSuccessCount$okhttp() + 1); super.close(); this.this$1.editor.commit(); } } }; } @Override // okhttp3.internal.cache.CacheRequest public final void abort() { Cache cache = this.this$0; synchronized (cache) { if (getDone()) { return; } setDone(true); cache.setWriteAbortCount$okhttp(cache.getWriteAbortCount$okhttp() + 1); Util.closeQuietly(this.cacheOut); try { this.editor.abort(); } catch (IOException unused) { } } } public final void setDone(boolean z) { this.done = z; } public final boolean getDone() { return this.done; } @Override // okhttp3.internal.cache.CacheRequest public final InterfaceC15168giw body() { return this.body; } } /* loaded from: classes.dex */ static final class Entry { private final int code; private final Handshake handshake; private final String message; private final Protocol protocol; private final long receivedResponseMillis; private final String requestMethod; private final Headers responseHeaders; private final long sentRequestMillis; private final HttpUrl url; private final Headers varyHeaders; public static final Companion Companion = new Companion(null); private static final String SENT_MILLIS = C14957gcv.c(Platform.Companion.get().getPrefix(), (Object) "-Sent-Millis"); private static final String RECEIVED_MILLIS = C14957gcv.c(Platform.Companion.get().getPrefix(), (Object) "-Received-Millis"); private final boolean isHttps() { return C14957gcv.b((Object) this.url.scheme(), (Object) "https"); } public Entry(giD gid) throws IOException { TlsVersion tlsVersion; C14957gcv.e(gid, ""); giD gid2 = gid; try { giD gid3 = gid2; InterfaceC15149gid c = C15160gio.c(gid); String t = c.t(); HttpUrl parse = HttpUrl.Companion.parse(t); if (parse == null) { IOException iOException = new IOException(C14957gcv.c("Cache corruption for ", (Object) t)); Platform.Companion.get().log("cache corruption", 5, iOException); throw iOException; } this.url = parse; this.requestMethod = c.t(); Headers.Builder builder = new Headers.Builder(); int readInt$okhttp = Cache.Companion.readInt$okhttp(c); int i = 0; int i2 = 0; while (i2 < readInt$okhttp) { i2++; builder.addLenient$okhttp(c.t()); } this.varyHeaders = builder.build(); StatusLine parse2 = StatusLine.Companion.parse(c.t()); this.protocol = parse2.protocol; this.code = parse2.code; this.message = parse2.message; Headers.Builder builder2 = new Headers.Builder(); int readInt$okhttp2 = Cache.Companion.readInt$okhttp(c); while (i < readInt$okhttp2) { i++; builder2.addLenient$okhttp(c.t()); } String str = SENT_MILLIS; String str2 = builder2.get(str); String str3 = RECEIVED_MILLIS; String str4 = builder2.get(str3); builder2.removeAll(str); builder2.removeAll(str3); long j = 0; this.sentRequestMillis = str2 == null ? 0L : Long.parseLong(str2); if (str4 != null) { j = Long.parseLong(str4); } this.receivedResponseMillis = j; this.responseHeaders = builder2.build(); if (isHttps()) { String t2 = c.t(); if (t2.length() > 0) { StringBuilder sb = new StringBuilder(); sb.append("expected \"\" but was \""); sb.append(t2); sb.append('\"'); throw new IOException(sb.toString()); } CipherSuite forJavaName = CipherSuite.Companion.forJavaName(c.t()); List readCertificateList = readCertificateList(c); List readCertificateList2 = readCertificateList(c); if (!c.j()) { tlsVersion = TlsVersion.Companion.forJavaName(c.t()); } else { tlsVersion = TlsVersion.SSL_3_0; } this.handshake = Handshake.Companion.get(tlsVersion, forJavaName, readCertificateList, readCertificateList2); } else { this.handshake = null; } C14866gag c14866gag = C14866gag.c; gbF.d(gid2, null); } catch (Throwable th) { try { throw th; } catch (Throwable th2) { gbF.d(gid2, th); throw th2; } } } public Entry(Response response) { C14957gcv.e(response, ""); this.url = response.request().url(); this.varyHeaders = Cache.Companion.varyHeaders(response); this.requestMethod = response.request().method(); this.protocol = response.protocol(); this.code = response.code(); this.message = response.message(); this.responseHeaders = response.headers(); this.handshake = response.handshake(); this.sentRequestMillis = response.sentRequestAtMillis(); this.receivedResponseMillis = response.receivedResponseAtMillis(); } public final void writeTo(DiskLruCache.Editor editor) throws IOException { C14957gcv.e(editor, ""); InterfaceC15148gic c = C15160gio.c(editor.newSink(0)); try { InterfaceC15148gic interfaceC15148gic = c; interfaceC15148gic.d(this.url.toString()).b(10); interfaceC15148gic.d(this.requestMethod).b(10); interfaceC15148gic.h(this.varyHeaders.size()).b(10); int size = this.varyHeaders.size(); for (int i = 0; i < size; i++) { interfaceC15148gic.d(this.varyHeaders.name(i)).d(": ").d(this.varyHeaders.value(i)).b(10); } interfaceC15148gic.d(new StatusLine(this.protocol, this.code, this.message).toString()).b(10); interfaceC15148gic.h(this.responseHeaders.size() + 2).b(10); int size2 = this.responseHeaders.size(); for (int i2 = 0; i2 < size2; i2++) { interfaceC15148gic.d(this.responseHeaders.name(i2)).d(": ").d(this.responseHeaders.value(i2)).b(10); } interfaceC15148gic.d(SENT_MILLIS).d(": ").h(this.sentRequestMillis).b(10); interfaceC15148gic.d(RECEIVED_MILLIS).d(": ").h(this.receivedResponseMillis).b(10); if (isHttps()) { interfaceC15148gic.b(10); Handshake handshake = this.handshake; C14957gcv.e(handshake); interfaceC15148gic.d(handshake.cipherSuite().javaName()).b(10); writeCertList(interfaceC15148gic, this.handshake.peerCertificates()); writeCertList(interfaceC15148gic, this.handshake.localCertificates()); interfaceC15148gic.d(this.handshake.tlsVersion().javaName()).b(10); } C14866gag c14866gag = C14866gag.c; gbF.d(c, null); } finally { } } private final List readCertificateList(InterfaceC15149gid interfaceC15149gid) throws IOException { int readInt$okhttp = Cache.Companion.readInt$okhttp(interfaceC15149gid); if (readInt$okhttp != -1) { try { CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509"); ArrayList arrayList = new ArrayList(readInt$okhttp); int i = 0; while (i < readInt$okhttp) { i++; String t = interfaceC15149gid.t(); C15111ghZ c15111ghZ = new C15111ghZ(); C15155gij.LWm lWm = C15155gij.c; C14957gcv.e(t, ""); byte[] d = giC.d(t); C15155gij c15155gij = d != null ? new C15155gij(d) : null; C14957gcv.e(c15155gij); C14957gcv.e(c15155gij, ""); c15155gij.a(c15111ghZ, c15155gij.a()); arrayList.add(certificateFactory.generateCertificate(new C15111ghZ.HBt(c15111ghZ))); } return arrayList; } catch (CertificateException e) { throw new IOException(e.getMessage()); } } return C14851gaI.c; } private final void writeCertList(InterfaceC15148gic interfaceC15148gic, List list) throws IOException { try { interfaceC15148gic.h(list.size()).b(10); Iterator it = list.iterator(); while (it.hasNext()) { byte[] encoded = it.next().getEncoded(); C15155gij.LWm lWm = C15155gij.c; C14957gcv.c(encoded, ""); interfaceC15148gic.d(C15155gij.LWm.b(encoded, giE.d()).e()).b(10); } } catch (CertificateEncodingException e) { throw new IOException(e.getMessage()); } } public final boolean matches(Request request, Response response) { C14957gcv.e(request, ""); C14957gcv.e(response, ""); return C14957gcv.b(this.url, request.url()) && C14957gcv.b((Object) this.requestMethod, (Object) request.method()) && Cache.Companion.varyMatches(response, this.varyHeaders, request); } public final Response response(DiskLruCache.Snapshot snapshot) { C14957gcv.e(snapshot, ""); String str = this.responseHeaders.get(HttpHeaders.CONTENT_TYPE); String str2 = this.responseHeaders.get(HttpHeaders.CONTENT_LENGTH); return new Response.Builder().request(new Request.Builder().url(this.url).method(this.requestMethod, null).headers(this.varyHeaders).build()).protocol(this.protocol).code(this.code).message(this.message).headers(this.responseHeaders).body(new CacheResponseBody(snapshot, str, str2)).handshake(this.handshake).sentRequestAtMillis(this.sentRequestMillis).receivedResponseAtMillis(this.receivedResponseMillis).build(); } /* loaded from: classes.dex */ public static final class Companion { private Companion() { } public /* synthetic */ Companion(C14953gcr c14953gcr) { this(); } } } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes.dex */ public static final class CacheResponseBody extends ResponseBody { private final InterfaceC15149gid bodySource; private final String contentLength; private final String contentType; private final DiskLruCache.Snapshot snapshot; public CacheResponseBody(DiskLruCache.Snapshot snapshot, String str, String str2) { C14957gcv.e(snapshot, ""); this.snapshot = snapshot; this.contentType = str; this.contentLength = str2; this.bodySource = C15160gio.c(new AbstractC15154gii(snapshot.getSource(1), this) { // from class: okhttp3.Cache.CacheResponseBody.1 final giD $source; final CacheResponseBody this$0; /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */ { super(r1); this.$source = r1; this.this$0 = this; } @Override // o.AbstractC15154gii, o.giD, java.io.Closeable, java.lang.AutoCloseable public final void close() throws IOException { this.this$0.getSnapshot().close(); super.close(); } }); } @Override // okhttp3.ResponseBody public final MediaType contentType() { String str = this.contentType; if (str == null) { return null; } return MediaType.Companion.parse(str); } @Override // okhttp3.ResponseBody public final long contentLength() { String str = this.contentLength; if (str != null) { return Util.toLongOrDefault(str, -1L); } return -1L; } @Override // okhttp3.ResponseBody public final InterfaceC15149gid source() { return this.bodySource; } public final DiskLruCache.Snapshot getSnapshot() { return this.snapshot; } } /* loaded from: classes.dex */ public static final class Companion { private Companion() { } public final String key(HttpUrl httpUrl) { C14957gcv.e(httpUrl, ""); C15155gij.LWm lWm = C15155gij.c; String obj = httpUrl.toString(); C14957gcv.e(obj, ""); C14957gcv.e(obj, ""); byte[] bytes = obj.getBytes(gdP.a); C14957gcv.c(bytes, ""); C15155gij c15155gij = new C15155gij(bytes); c15155gij.d = obj; return c15155gij.b("MD5").c(); } public final int readInt$okhttp(InterfaceC15149gid interfaceC15149gid) throws IOException { C14957gcv.e(interfaceC15149gid, ""); try { long o2 = interfaceC15149gid.o(); String t = interfaceC15149gid.t(); if (o2 >= 0 && o2 <= 2147483647L && t.length() <= 0) { return (int) o2; } StringBuilder sb = new StringBuilder("expected an int but was \""); sb.append(o2); sb.append(t); sb.append('\"'); throw new IOException(sb.toString()); } catch (NumberFormatException e) { throw new IOException(e.getMessage()); } } public final boolean varyMatches(Response response, Headers headers, Request request) { C14957gcv.e(response, ""); C14957gcv.e(headers, ""); C14957gcv.e(request, ""); Set varyFields = varyFields(response.headers()); if ((varyFields instanceof Collection) && varyFields.isEmpty()) { return true; } for (String str : varyFields) { if (!C14957gcv.b(headers.values(str), request.headers(str))) { return false; } } return true; } public final boolean hasVaryAll(Response response) { C14957gcv.e(response, ""); return varyFields(response.headers()).contains("*"); } private final Set varyFields(Headers headers) { int size = headers.size(); TreeSet treeSet = null; for (int i = 0; i < size; i++) { if (gdZ.a(HttpHeaders.VARY, headers.name(i), true)) { String value = headers.value(i); if (treeSet == null) { C14957gcv.e(gcH.d, ""); Comparator comparator = String.CASE_INSENSITIVE_ORDER; C14957gcv.c(comparator, ""); treeSet = new TreeSet(comparator); } String str = value; char[] cArr = {','}; C14957gcv.e(str, ""); C14957gcv.e(cArr, ""); Iterator it = C15017gel.c((CharSequence) str, String.valueOf(cArr[0]), false, 0).iterator(); while (it.hasNext()) { treeSet.add(gdZ.b((CharSequence) it.next()).toString()); } } } return treeSet == null ? gaH.e : treeSet; } public final Headers varyHeaders(Response response) { C14957gcv.e(response, ""); Response networkResponse = response.networkResponse(); C14957gcv.e(networkResponse); return varyHeaders(networkResponse.request().headers(), response.headers()); } private final Headers varyHeaders(Headers headers, Headers headers2) { Set varyFields = varyFields(headers2); if (varyFields.isEmpty()) { return Util.EMPTY_HEADERS; } Headers.Builder builder = new Headers.Builder(); int size = headers.size(); for (int i = 0; i < size; i++) { String name = headers.name(i); if (varyFields.contains(name)) { builder.add(name, headers.value(i)); } } return builder.build(); } public /* synthetic */ Companion(C14953gcr c14953gcr) { this(); } } public final void setWriteSuccessCount$okhttp(int i) { this.writeSuccessCount = i; } public final void setWriteAbortCount$okhttp(int i) { this.writeAbortCount = i; } public final int getWriteSuccessCount$okhttp() { return this.writeSuccessCount; } public final int getWriteAbortCount$okhttp() { return this.writeAbortCount; } public final DiskLruCache getCache$okhttp() { return this.cache; } public static final String key(HttpUrl httpUrl) { return Companion.key(httpUrl); } }