package okhttp3.internal.cache; import com.google.common.net.HttpHeaders; import java.util.Date; import java.util.concurrent.TimeUnit; import o.C14953gcr; import o.C14957gcv; import o.gdZ; import okhttp3.CacheControl; import okhttp3.Headers; import okhttp3.Request; import okhttp3.Response; import okhttp3.internal.Util; import okhttp3.internal.http.DatesKt; /* loaded from: classes.dex */ public final class CacheStrategy { public static final Companion Companion = new Companion(null); private final Response cacheResponse; private final Request networkRequest; public CacheStrategy(Request request, Response response) { this.networkRequest = request; this.cacheResponse = response; } /* loaded from: classes.dex */ public static final class Factory { private int ageSeconds; private final Response cacheResponse; private String etag; private Date expires; private Date lastModified; private String lastModifiedString; private final long nowMillis; private long receivedResponseMillis; private final Request request; private long sentRequestMillis; private Date servedDate; private String servedDateString; public Factory(long j, Request request, Response response) { C14957gcv.e(request, ""); this.nowMillis = j; this.request = request; this.cacheResponse = response; this.ageSeconds = -1; if (response != null) { this.sentRequestMillis = response.sentRequestAtMillis(); this.receivedResponseMillis = response.receivedResponseAtMillis(); Headers headers = response.headers(); int size = headers.size(); for (int i = 0; i < size; i++) { String name = headers.name(i); String value = headers.value(i); if (gdZ.a(name, HttpHeaders.DATE, true)) { this.servedDate = DatesKt.toHttpDateOrNull(value); this.servedDateString = value; } else if (gdZ.a(name, HttpHeaders.EXPIRES, true)) { this.expires = DatesKt.toHttpDateOrNull(value); } else if (gdZ.a(name, HttpHeaders.LAST_MODIFIED, true)) { this.lastModified = DatesKt.toHttpDateOrNull(value); this.lastModifiedString = value; } else if (gdZ.a(name, HttpHeaders.ETAG, true)) { this.etag = value; } else if (gdZ.a(name, HttpHeaders.AGE, true)) { this.ageSeconds = Util.toNonNegativeInt(value, -1); } } } } private final boolean isFreshnessLifetimeHeuristic() { Response response = this.cacheResponse; C14957gcv.e(response); return response.cacheControl().maxAgeSeconds() == -1 && this.expires == null; } public final CacheStrategy compute() { CacheStrategy computeCandidate = computeCandidate(); return (computeCandidate.getNetworkRequest() == null || !this.request.cacheControl().onlyIfCached()) ? computeCandidate : new CacheStrategy(null, null); } private final CacheStrategy computeCandidate() { String str; if (this.cacheResponse == null) { return new CacheStrategy(this.request, null); } if (this.request.isHttps() && this.cacheResponse.handshake() == null) { return new CacheStrategy(this.request, null); } if (!CacheStrategy.Companion.isCacheable(this.cacheResponse, this.request)) { return new CacheStrategy(this.request, null); } CacheControl cacheControl = this.request.cacheControl(); if (cacheControl.noCache() || hasConditions(this.request)) { return new CacheStrategy(this.request, null); } CacheControl cacheControl2 = this.cacheResponse.cacheControl(); long cacheResponseAge = cacheResponseAge(); long computeFreshnessLifetime = computeFreshnessLifetime(); if (cacheControl.maxAgeSeconds() != -1) { computeFreshnessLifetime = Math.min(computeFreshnessLifetime, TimeUnit.SECONDS.toMillis(cacheControl.maxAgeSeconds())); } long j = 0; long millis = cacheControl.minFreshSeconds() != -1 ? TimeUnit.SECONDS.toMillis(cacheControl.minFreshSeconds()) : 0L; if (!cacheControl2.mustRevalidate() && cacheControl.maxStaleSeconds() != -1) { j = TimeUnit.SECONDS.toMillis(cacheControl.maxStaleSeconds()); } if (!cacheControl2.noCache()) { long j2 = millis + cacheResponseAge; if (j2 < j + computeFreshnessLifetime) { Response.Builder newBuilder = this.cacheResponse.newBuilder(); if (j2 >= computeFreshnessLifetime) { newBuilder.addHeader(HttpHeaders.WARNING, "110 HttpURLConnection \"Response is stale\""); } if (cacheResponseAge > 86400000 && isFreshnessLifetimeHeuristic()) { newBuilder.addHeader(HttpHeaders.WARNING, "113 HttpURLConnection \"Heuristic expiration\""); } return new CacheStrategy(null, newBuilder.build()); } } String str2 = this.etag; if (str2 != null) { str = HttpHeaders.IF_NONE_MATCH; } else { if (this.lastModified != null) { str2 = this.lastModifiedString; } else { if (this.servedDate == null) { return new CacheStrategy(this.request, null); } str2 = this.servedDateString; } str = HttpHeaders.IF_MODIFIED_SINCE; } Headers.Builder newBuilder2 = this.request.headers().newBuilder(); C14957gcv.e(str2); newBuilder2.addLenient$okhttp(str, str2); return new CacheStrategy(this.request.newBuilder().headers(newBuilder2.build()).build(), this.cacheResponse); } private final long computeFreshnessLifetime() { Long valueOf; Response response = this.cacheResponse; C14957gcv.e(response); if (response.cacheControl().maxAgeSeconds() != -1) { return TimeUnit.SECONDS.toMillis(r0.maxAgeSeconds()); } Date date = this.expires; if (date != null) { Date date2 = this.servedDate; valueOf = date2 != null ? Long.valueOf(date2.getTime()) : null; long time = date.getTime() - (valueOf == null ? this.receivedResponseMillis : valueOf.longValue()); if (time > 0) { return time; } return 0L; } if (this.lastModified == null || this.cacheResponse.request().url().query() != null) { return 0L; } Date date3 = this.servedDate; valueOf = date3 != null ? Long.valueOf(date3.getTime()) : null; long longValue = valueOf == null ? this.sentRequestMillis : valueOf.longValue(); Date date4 = this.lastModified; C14957gcv.e(date4); long time2 = longValue - date4.getTime(); if (time2 > 0) { return time2 / 10; } return 0L; } private final long cacheResponseAge() { Date date = this.servedDate; long max = date != null ? Math.max(0L, this.receivedResponseMillis - date.getTime()) : 0L; if (this.ageSeconds != -1) { max = Math.max(max, TimeUnit.SECONDS.toMillis(this.ageSeconds)); } long j = this.receivedResponseMillis; return max + (j - this.sentRequestMillis) + (this.nowMillis - j); } private final boolean hasConditions(Request request) { return (request.header(HttpHeaders.IF_MODIFIED_SINCE) == null && request.header(HttpHeaders.IF_NONE_MATCH) == null) ? false : true; } public final Request getRequest$okhttp() { return this.request; } } /* loaded from: classes.dex */ public static final class Companion { private Companion() { } public final boolean isCacheable(Response response, Request request) { C14957gcv.e(response, ""); C14957gcv.e(request, ""); int code = response.code(); if (code != 200 && code != 410 && code != 414 && code != 501 && code != 203 && code != 204) { if (code != 307) { if (code != 308 && code != 404 && code != 405) { switch (code) { case 300: case 301: break; case 302: break; default: return false; } } } if (Response.header$default(response, HttpHeaders.EXPIRES, null, 2, null) == null && response.cacheControl().maxAgeSeconds() == -1 && !response.cacheControl().isPublic() && !response.cacheControl().isPrivate()) { return false; } } return (response.cacheControl().noStore() || request.cacheControl().noStore()) ? false : true; } public /* synthetic */ Companion(C14953gcr c14953gcr) { this(); } } public final Request getNetworkRequest() { return this.networkRequest; } public final Response getCacheResponse() { return this.cacheResponse; } }