package okhttp3.internal.cache; import io.sentry.protocol.Response; import java.io.IOException; import java.util.concurrent.TimeUnit; import kotlin.Metadata; import kotlin.jvm.internal.DefaultConstructorMarker; import kotlin.jvm.internal.Intrinsics; import kotlin.text.StringsKt; import okhttp3.Cache; import okhttp3.Call; import okhttp3.EventListener; import okhttp3.Headers; import okhttp3.Interceptor; import okhttp3.Protocol; import okhttp3.Request; import okhttp3.Response; import okhttp3.ResponseBody; import okhttp3.internal.Util; import okhttp3.internal.cache.CacheStrategy; import okhttp3.internal.connection.RealCall; import okhttp3.internal.http.HttpHeaders; import okhttp3.internal.http.HttpMethod; import okhttp3.internal.http.RealResponseBody; import okio.Buffer; import okio.BufferedSink; import okio.BufferedSource; import okio.Okio; import okio.Sink; import okio.Source; import okio.Timeout; /* compiled from: CacheInterceptor.kt */ @Metadata(bv = {1, 0, 3}, d1 = {"\u0000(\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0004\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\b\u0002\u0018\u0000 \u000f2\u00020\u0001:\u0001\u000fB\u000f\u0012\b\u0010\u0002\u001a\u0004\u0018\u00010\u0003¢\u0006\u0002\u0010\u0004J\u001a\u0010\u0007\u001a\u00020\b2\b\u0010\t\u001a\u0004\u0018\u00010\n2\u0006\u0010\u000b\u001a\u00020\bH\u0002J\u0010\u0010\f\u001a\u00020\b2\u0006\u0010\r\u001a\u00020\u000eH\u0016R\u0016\u0010\u0002\u001a\u0004\u0018\u00010\u0003X\u0080\u0004¢\u0006\b\n\u0000\u001a\u0004\b\u0005\u0010\u0006¨\u0006\u0010"}, d2 = {"Lokhttp3/internal/cache/CacheInterceptor;", "Lokhttp3/Interceptor;", "cache", "Lokhttp3/Cache;", "(Lokhttp3/Cache;)V", "getCache$okhttp", "()Lokhttp3/Cache;", "cacheWritingResponse", "Lokhttp3/Response;", "cacheRequest", "Lokhttp3/internal/cache/CacheRequest;", Response.TYPE, "intercept", "chain", "Lokhttp3/Interceptor$Chain;", "Companion", "okhttp"}, k = 1, mv = {1, 4, 0}) /* loaded from: classes3.dex */ public final class CacheInterceptor implements Interceptor { /* renamed from: Companion, reason: from kotlin metadata */ public static final Companion INSTANCE = new Companion(null); private final Cache cache; /* renamed from: getCache$okhttp, reason: from getter */ public final Cache getCache() { return this.cache; } public CacheInterceptor(Cache cache) { this.cache = cache; } @Override // okhttp3.Interceptor public okhttp3.Response intercept(Interceptor.Chain chain) throws IOException { EventListener eventListener; ResponseBody body; ResponseBody body2; Intrinsics.checkNotNullParameter(chain, "chain"); Call call = chain.call(); Cache cache = this.cache; okhttp3.Response response = cache != null ? cache.get$okhttp(chain.request()) : null; CacheStrategy compute = new CacheStrategy.Factory(System.currentTimeMillis(), chain.request(), response).compute(); Request networkRequest = compute.getNetworkRequest(); okhttp3.Response cacheResponse = compute.getCacheResponse(); Cache cache2 = this.cache; if (cache2 != null) { cache2.trackResponse$okhttp(compute); } RealCall realCall = (RealCall) (!(call instanceof RealCall) ? null : call); if (realCall == null || (eventListener = realCall.getEventListener()) == null) { eventListener = EventListener.NONE; } if (response != null && cacheResponse == null && (body2 = response.body()) != null) { Util.closeQuietly(body2); } if (networkRequest == null && cacheResponse == null) { okhttp3.Response build = new Response.Builder().request(chain.request()).protocol(Protocol.HTTP_1_1).code(504).message("Unsatisfiable Request (only-if-cached)").body(Util.EMPTY_RESPONSE).sentRequestAtMillis(-1L).receivedResponseAtMillis(System.currentTimeMillis()).build(); eventListener.satisfactionFailure(call, build); return build; } if (networkRequest == null) { Intrinsics.checkNotNull(cacheResponse); okhttp3.Response build2 = cacheResponse.newBuilder().cacheResponse(INSTANCE.stripBody(cacheResponse)).build(); eventListener.cacheHit(call, build2); return build2; } if (cacheResponse != null) { eventListener.cacheConditionalHit(call, cacheResponse); } else if (this.cache != null) { eventListener.cacheMiss(call); } try { okhttp3.Response proceed = chain.proceed(networkRequest); if (proceed == null && response != null && body != null) { } if (cacheResponse != null) { if (proceed != null && proceed.code() == 304) { Response.Builder newBuilder = cacheResponse.newBuilder(); Companion companion = INSTANCE; okhttp3.Response build3 = newBuilder.headers(companion.combine(cacheResponse.headers(), proceed.headers())).sentRequestAtMillis(proceed.sentRequestAtMillis()).receivedResponseAtMillis(proceed.receivedResponseAtMillis()).cacheResponse(companion.stripBody(cacheResponse)).networkResponse(companion.stripBody(proceed)).build(); ResponseBody body3 = proceed.body(); Intrinsics.checkNotNull(body3); body3.close(); Cache cache3 = this.cache; Intrinsics.checkNotNull(cache3); cache3.trackConditionalCacheHit$okhttp(); this.cache.update$okhttp(cacheResponse, build3); eventListener.cacheHit(call, build3); return build3; } ResponseBody body4 = cacheResponse.body(); if (body4 != null) { Util.closeQuietly(body4); } } Intrinsics.checkNotNull(proceed); Response.Builder newBuilder2 = proceed.newBuilder(); Companion companion2 = INSTANCE; okhttp3.Response build4 = newBuilder2.cacheResponse(companion2.stripBody(cacheResponse)).networkResponse(companion2.stripBody(proceed)).build(); if (this.cache != null) { if (HttpHeaders.promisesBody(build4) && CacheStrategy.INSTANCE.isCacheable(build4, networkRequest)) { okhttp3.Response cacheWritingResponse = cacheWritingResponse(this.cache.put$okhttp(build4), build4); if (cacheResponse != null) { eventListener.cacheMiss(call); } return cacheWritingResponse; } if (HttpMethod.INSTANCE.invalidatesCache(networkRequest.method())) { try { this.cache.remove$okhttp(networkRequest); } catch (IOException unused) { } } } return build4; } finally { if (response != null && (body = response.body()) != null) { Util.closeQuietly(body); } } } private final okhttp3.Response cacheWritingResponse(final CacheRequest cacheRequest, okhttp3.Response response) throws IOException { if (cacheRequest == null) { return response; } Sink body = cacheRequest.getBody(); ResponseBody body2 = response.body(); Intrinsics.checkNotNull(body2); final BufferedSource source = body2.getSource(); final BufferedSink buffer = Okio.buffer(body); Source source2 = new Source() { // from class: okhttp3.internal.cache.CacheInterceptor$cacheWritingResponse$cacheWritingSource$1 private boolean cacheRequestClosed; @Override // okio.Source public long read(Buffer sink, long byteCount) throws IOException { Intrinsics.checkNotNullParameter(sink, "sink"); try { long read = BufferedSource.this.read(sink, byteCount); if (read != -1) { sink.copyTo(buffer.getBuffer(), sink.size() - read, read); buffer.emitCompleteSegments(); return read; } if (!this.cacheRequestClosed) { this.cacheRequestClosed = true; buffer.close(); } return -1L; } catch (IOException e) { if (!this.cacheRequestClosed) { this.cacheRequestClosed = true; cacheRequest.abort(); } throw e; } } @Override // okio.Source /* renamed from: timeout */ public Timeout getTimeout() { return BufferedSource.this.getTimeout(); } @Override // okio.Source, java.io.Closeable, java.lang.AutoCloseable public void close() throws IOException { if (!this.cacheRequestClosed && !Util.discard(this, 100, TimeUnit.MILLISECONDS)) { this.cacheRequestClosed = true; cacheRequest.abort(); } BufferedSource.this.close(); } }; return response.newBuilder().body(new RealResponseBody(okhttp3.Response.header$default(response, com.google.common.net.HttpHeaders.CONTENT_TYPE, null, 2, null), response.body().getContentLength(), Okio.buffer(source2))).build(); } /* compiled from: CacheInterceptor.kt */ @Metadata(bv = {1, 0, 3}, d1 = {"\u0000*\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0010\u000b\n\u0000\n\u0002\u0010\u000e\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\b\u0086\u0003\u0018\u00002\u00020\u0001B\u0007\b\u0002¢\u0006\u0002\u0010\u0002J\u0018\u0010\u0003\u001a\u00020\u00042\u0006\u0010\u0005\u001a\u00020\u00042\u0006\u0010\u0006\u001a\u00020\u0004H\u0002J\u0010\u0010\u0007\u001a\u00020\b2\u0006\u0010\t\u001a\u00020\nH\u0002J\u0010\u0010\u000b\u001a\u00020\b2\u0006\u0010\t\u001a\u00020\nH\u0002J\u0014\u0010\f\u001a\u0004\u0018\u00010\r2\b\u0010\u000e\u001a\u0004\u0018\u00010\rH\u0002¨\u0006\u000f"}, d2 = {"Lokhttp3/internal/cache/CacheInterceptor$Companion;", "", "()V", "combine", "Lokhttp3/Headers;", "cachedHeaders", "networkHeaders", "isContentSpecificHeader", "", "fieldName", "", "isEndToEnd", "stripBody", "Lokhttp3/Response;", io.sentry.protocol.Response.TYPE, "okhttp"}, k = 1, mv = {1, 4, 0}) /* loaded from: classes3.dex */ public static final class Companion { private Companion() { } public /* synthetic */ Companion(DefaultConstructorMarker defaultConstructorMarker) { this(); } /* JADX INFO: Access modifiers changed from: private */ public final okhttp3.Response stripBody(okhttp3.Response response) { return (response != null ? response.body() : null) != null ? response.newBuilder().body(null).build() : response; } /* JADX INFO: Access modifiers changed from: private */ public final Headers combine(Headers cachedHeaders, Headers networkHeaders) { Headers.Builder builder = new Headers.Builder(); int size = cachedHeaders.size(); for (int i = 0; i < size; i++) { String name = cachedHeaders.name(i); String value = cachedHeaders.value(i); if ((!StringsKt.equals(com.google.common.net.HttpHeaders.WARNING, name, true) || !StringsKt.startsWith$default(value, "1", false, 2, (Object) null)) && (isContentSpecificHeader(name) || !isEndToEnd(name) || networkHeaders.get(name) == null)) { builder.addLenient$okhttp(name, value); } } int size2 = networkHeaders.size(); for (int i2 = 0; i2 < size2; i2++) { String name2 = networkHeaders.name(i2); if (!isContentSpecificHeader(name2) && isEndToEnd(name2)) { builder.addLenient$okhttp(name2, networkHeaders.value(i2)); } } return builder.build(); } private final boolean isEndToEnd(String fieldName) { return (StringsKt.equals(com.google.common.net.HttpHeaders.CONNECTION, fieldName, true) || StringsKt.equals(com.google.common.net.HttpHeaders.KEEP_ALIVE, fieldName, true) || StringsKt.equals(com.google.common.net.HttpHeaders.PROXY_AUTHENTICATE, fieldName, true) || StringsKt.equals(com.google.common.net.HttpHeaders.PROXY_AUTHORIZATION, fieldName, true) || StringsKt.equals(com.google.common.net.HttpHeaders.TE, fieldName, true) || StringsKt.equals("Trailers", fieldName, true) || StringsKt.equals(com.google.common.net.HttpHeaders.TRANSFER_ENCODING, fieldName, true) || StringsKt.equals(com.google.common.net.HttpHeaders.UPGRADE, fieldName, true)) ? false : true; } private final boolean isContentSpecificHeader(String fieldName) { return StringsKt.equals(com.google.common.net.HttpHeaders.CONTENT_LENGTH, fieldName, true) || StringsKt.equals(com.google.common.net.HttpHeaders.CONTENT_ENCODING, fieldName, true) || StringsKt.equals(com.google.common.net.HttpHeaders.CONTENT_TYPE, fieldName, true); } } }