package okhttp3; import com.unity3d.ads.core.data.datasource.AndroidStaticDeviceInfoDataSource; 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.Collections; import java.util.List; 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.cache.InternalCache; import okhttp3.internal.http.HttpHeaders; import okhttp3.internal.http.HttpMethod; import okhttp3.internal.http.StatusLine; import okhttp3.internal.io.FileSystem; import okhttp3.internal.platform.Platform; import okio.Buffer; import okio.BufferedSink; import okio.BufferedSource; import okio.ByteString; import okio.ForwardingSink; import okio.ForwardingSource; import okio.Okio; import okio.Sink; import okio.Source; import org.apache.http.protocol.HTTP; /* loaded from: classes5.dex */ public final class Cache implements Closeable, Flushable { public final DiskLruCache cache; public int hitCount; public final InternalCache internalCache; public int networkCount; public int requestCount; public int writeAbortCount; public int writeSuccessCount; public Cache(File file, long j) { this(file, j, FileSystem.SYSTEM); } public Cache(File file, long j, FileSystem fileSystem) { this.internalCache = new InternalCache() { // from class: okhttp3.Cache.1 @Override // okhttp3.internal.cache.InternalCache public Response get(Request request) { return Cache.this.get(request); } @Override // okhttp3.internal.cache.InternalCache public CacheRequest put(Response response) { return Cache.this.put(response); } @Override // okhttp3.internal.cache.InternalCache public void remove(Request request) { Cache.this.remove(request); } @Override // okhttp3.internal.cache.InternalCache public void update(Response response, Response response2) { Cache.this.update(response, response2); } @Override // okhttp3.internal.cache.InternalCache public void trackConditionalCacheHit() { Cache.this.trackConditionalCacheHit(); } @Override // okhttp3.internal.cache.InternalCache public void trackResponse(CacheStrategy cacheStrategy) { Cache.this.trackResponse(cacheStrategy); } }; this.cache = DiskLruCache.create(fileSystem, file, 201105, 2, j); } public static String key(HttpUrl httpUrl) { return ByteString.encodeUtf8(httpUrl.toString()).md5().hex(); } public Response get(Request request) { try { DiskLruCache.Snapshot snapshot = this.cache.get(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; } Util.closeQuietly(response.body()); return null; } catch (IOException unused) { Util.closeQuietly(snapshot); return null; } } catch (IOException unused2) { } } public CacheRequest put(Response response) { DiskLruCache.Editor editor; String method = response.request().method(); if (HttpMethod.invalidatesCache(response.request().method())) { try { remove(response.request()); } catch (IOException unused) { } return null; } if (!method.equals("GET") || HttpHeaders.hasVaryAll(response)) { return null; } Entry entry = new Entry(response); try { editor = this.cache.edit(key(response.request().url())); if (editor == null) { return null; } try { entry.writeTo(editor); return new CacheRequestImpl(editor); } catch (IOException unused2) { abortQuietly(editor); return null; } } catch (IOException unused3) { editor = null; } } public void remove(Request request) { this.cache.remove(key(request.url())); } public void update(Response response, Response response2) { DiskLruCache.Editor editor; Entry entry = new Entry(response2); try { editor = ((CacheResponseBody) response.body()).snapshot.edit(); if (editor != null) { try { entry.writeTo(editor); editor.commit(); } catch (IOException unused) { abortQuietly(editor); } } } catch (IOException unused2) { editor = null; } } public final void abortQuietly(DiskLruCache.Editor editor) { if (editor != null) { try { editor.abort(); } catch (IOException unused) { } } } @Override // java.io.Flushable public void flush() { this.cache.flush(); } @Override // java.io.Closeable, java.lang.AutoCloseable public void close() { this.cache.close(); } public synchronized void trackResponse(CacheStrategy cacheStrategy) { try { this.requestCount++; if (cacheStrategy.networkRequest != null) { this.networkCount++; } else if (cacheStrategy.cacheResponse != null) { this.hitCount++; } } catch (Throwable th) { throw th; } } public synchronized void trackConditionalCacheHit() { this.hitCount++; } public final class CacheRequestImpl implements CacheRequest { public Sink body; public Sink cacheOut; public boolean done; public final DiskLruCache.Editor editor; @Override // okhttp3.internal.cache.CacheRequest public Sink body() { return this.body; } public CacheRequestImpl(final DiskLruCache.Editor editor) { this.editor = editor; Sink newSink = editor.newSink(1); this.cacheOut = newSink; this.body = new ForwardingSink(newSink) { // from class: okhttp3.Cache.CacheRequestImpl.1 @Override // okio.ForwardingSink, okio.Sink, java.io.Closeable, java.lang.AutoCloseable public void close() { synchronized (Cache.this) { try { CacheRequestImpl cacheRequestImpl = CacheRequestImpl.this; if (cacheRequestImpl.done) { return; } cacheRequestImpl.done = true; Cache.this.writeSuccessCount++; super.close(); editor.commit(); } catch (Throwable th) { throw th; } } } }; } @Override // okhttp3.internal.cache.CacheRequest public void abort() { synchronized (Cache.this) { try { if (this.done) { return; } this.done = true; Cache.this.writeAbortCount++; Util.closeQuietly(this.cacheOut); try { this.editor.abort(); } catch (IOException unused) { } } catch (Throwable th) { throw th; } } } } public static final class Entry { public final int code; public final Handshake handshake; public final String message; public final Protocol protocol; public final long receivedResponseMillis; public final String requestMethod; public final Headers responseHeaders; public final long sentRequestMillis; public final String url; public final Headers varyHeaders; public static final String SENT_MILLIS = Platform.get().getPrefix() + "-Sent-Millis"; public static final String RECEIVED_MILLIS = Platform.get().getPrefix() + "-Received-Millis"; public Entry(Source source) { TlsVersion tlsVersion; try { BufferedSource buffer = Okio.buffer(source); this.url = buffer.readUtf8LineStrict(); this.requestMethod = buffer.readUtf8LineStrict(); Headers.Builder builder = new Headers.Builder(); int readInt = Cache.readInt(buffer); for (int i = 0; i < readInt; i++) { builder.addLenient(buffer.readUtf8LineStrict()); } this.varyHeaders = builder.build(); StatusLine parse = StatusLine.parse(buffer.readUtf8LineStrict()); this.protocol = parse.protocol; this.code = parse.code; this.message = parse.message; Headers.Builder builder2 = new Headers.Builder(); int readInt2 = Cache.readInt(buffer); for (int i2 = 0; i2 < readInt2; i2++) { builder2.addLenient(buffer.readUtf8LineStrict()); } String str = SENT_MILLIS; String str2 = builder2.get(str); String str3 = RECEIVED_MILLIS; String str4 = builder2.get(str3); builder2.removeAll(str); builder2.removeAll(str3); this.sentRequestMillis = str2 != null ? Long.parseLong(str2) : 0L; this.receivedResponseMillis = str4 != null ? Long.parseLong(str4) : 0L; this.responseHeaders = builder2.build(); if (isHttps()) { String readUtf8LineStrict = buffer.readUtf8LineStrict(); if (readUtf8LineStrict.length() > 0) { throw new IOException("expected \"\" but was \"" + readUtf8LineStrict + "\""); } CipherSuite forJavaName = CipherSuite.forJavaName(buffer.readUtf8LineStrict()); List readCertificateList = readCertificateList(buffer); List readCertificateList2 = readCertificateList(buffer); if (!buffer.exhausted()) { tlsVersion = TlsVersion.forJavaName(buffer.readUtf8LineStrict()); } else { tlsVersion = TlsVersion.SSL_3_0; } this.handshake = Handshake.get(tlsVersion, forJavaName, readCertificateList, readCertificateList2); } else { this.handshake = null; } source.close(); } catch (Throwable th) { source.close(); throw th; } } public Entry(Response response) { this.url = response.request().url().toString(); this.varyHeaders = HttpHeaders.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 void writeTo(DiskLruCache.Editor editor) { BufferedSink buffer = Okio.buffer(editor.newSink(0)); buffer.writeUtf8(this.url).writeByte(10); buffer.writeUtf8(this.requestMethod).writeByte(10); buffer.writeDecimalLong(this.varyHeaders.size()).writeByte(10); int size = this.varyHeaders.size(); for (int i = 0; i < size; i++) { buffer.writeUtf8(this.varyHeaders.name(i)).writeUtf8(": ").writeUtf8(this.varyHeaders.value(i)).writeByte(10); } buffer.writeUtf8(new StatusLine(this.protocol, this.code, this.message).toString()).writeByte(10); buffer.writeDecimalLong(this.responseHeaders.size() + 2).writeByte(10); int size2 = this.responseHeaders.size(); for (int i2 = 0; i2 < size2; i2++) { buffer.writeUtf8(this.responseHeaders.name(i2)).writeUtf8(": ").writeUtf8(this.responseHeaders.value(i2)).writeByte(10); } buffer.writeUtf8(SENT_MILLIS).writeUtf8(": ").writeDecimalLong(this.sentRequestMillis).writeByte(10); buffer.writeUtf8(RECEIVED_MILLIS).writeUtf8(": ").writeDecimalLong(this.receivedResponseMillis).writeByte(10); if (isHttps()) { buffer.writeByte(10); buffer.writeUtf8(this.handshake.cipherSuite().javaName()).writeByte(10); writeCertList(buffer, this.handshake.peerCertificates()); writeCertList(buffer, this.handshake.localCertificates()); buffer.writeUtf8(this.handshake.tlsVersion().javaName()).writeByte(10); } buffer.close(); } public final boolean isHttps() { return this.url.startsWith("https://"); } public final List readCertificateList(BufferedSource bufferedSource) { int readInt = Cache.readInt(bufferedSource); if (readInt == -1) { return Collections.emptyList(); } try { CertificateFactory certificateFactory = CertificateFactory.getInstance(AndroidStaticDeviceInfoDataSource.CERTIFICATE_TYPE_X509); ArrayList arrayList = new ArrayList(readInt); for (int i = 0; i < readInt; i++) { String readUtf8LineStrict = bufferedSource.readUtf8LineStrict(); Buffer buffer = new Buffer(); buffer.write(ByteString.decodeBase64(readUtf8LineStrict)); arrayList.add(certificateFactory.generateCertificate(buffer.inputStream())); } return arrayList; } catch (CertificateException e) { throw new IOException(e.getMessage()); } } public final void writeCertList(BufferedSink bufferedSink, List list) { try { bufferedSink.writeDecimalLong(list.size()).writeByte(10); int size = list.size(); for (int i = 0; i < size; i++) { bufferedSink.writeUtf8(ByteString.of(((Certificate) list.get(i)).getEncoded()).base64()).writeByte(10); } } catch (CertificateEncodingException e) { throw new IOException(e.getMessage()); } } public boolean matches(Request request, Response response) { return this.url.equals(request.url().toString()) && this.requestMethod.equals(request.method()) && HttpHeaders.varyMatches(response, this.varyHeaders, request); } public Response response(DiskLruCache.Snapshot snapshot) { String str = this.responseHeaders.get("Content-Type"); String str2 = this.responseHeaders.get(HTTP.CONTENT_LEN); 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(); } } public static int readInt(BufferedSource bufferedSource) { try { long readDecimalLong = bufferedSource.readDecimalLong(); String readUtf8LineStrict = bufferedSource.readUtf8LineStrict(); if (readDecimalLong >= 0 && readDecimalLong <= 2147483647L && readUtf8LineStrict.isEmpty()) { return (int) readDecimalLong; } throw new IOException("expected an int but was \"" + readDecimalLong + readUtf8LineStrict + "\""); } catch (NumberFormatException e) { throw new IOException(e.getMessage()); } } public static class CacheResponseBody extends ResponseBody { public final BufferedSource bodySource; public final String contentLength; public final String contentType; public final DiskLruCache.Snapshot snapshot; @Override // okhttp3.ResponseBody public BufferedSource source() { return this.bodySource; } public CacheResponseBody(final DiskLruCache.Snapshot snapshot, String str, String str2) { this.snapshot = snapshot; this.contentType = str; this.contentLength = str2; this.bodySource = Okio.buffer(new ForwardingSource(snapshot.getSource(1)) { // from class: okhttp3.Cache.CacheResponseBody.1 @Override // okio.ForwardingSource, okio.Source, java.io.Closeable, java.lang.AutoCloseable public void close() { snapshot.close(); super.close(); } }); } @Override // okhttp3.ResponseBody public MediaType contentType() { String str = this.contentType; if (str != null) { return MediaType.parse(str); } return null; } @Override // okhttp3.ResponseBody public long contentLength() { try { String str = this.contentLength; if (str != null) { return Long.parseLong(str); } return -1L; } catch (NumberFormatException unused) { return -1L; } } } }