Rabbit-R1/switch port/java/sources/okhttp3/internal/cache/CacheStrategy.java
2024-05-21 17:08:36 -04:00

245 lines
14 KiB
Java

package okhttp3.internal.cache;
import androidx.compose.material3.CalendarModelKt;
import com.google.common.net.HttpHeaders;
import io.sentry.SentryBaseEvent;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import kotlin.Metadata;
import kotlin.jvm.internal.DefaultConstructorMarker;
import kotlin.jvm.internal.Intrinsics;
import kotlin.text.StringsKt;
import okhttp3.CacheControl;
import okhttp3.Headers;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.internal.Util;
import okhttp3.internal.http.DatesKt;
/* compiled from: CacheStrategy.kt */
@Metadata(bv = {1, 0, 3}, d1 = {"\u0000\u0018\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\b\u0018\u0000 \u000b2\u00020\u0001:\u0002\u000b\fB\u001b\b\u0000\u0012\b\u0010\u0002\u001a\u0004\u0018\u00010\u0003\u0012\b\u0010\u0004\u001a\u0004\u0018\u00010\u0005¢\u0006\u0002\u0010\u0006R\u0013\u0010\u0004\u001a\u0004\u0018\u00010\u0005¢\u0006\b\n\u0000\u001a\u0004\b\u0007\u0010\bR\u0013\u0010\u0002\u001a\u0004\u0018\u00010\u0003¢\u0006\b\n\u0000\u001a\u0004\b\t\u0010\\u0006\r"}, d2 = {"Lokhttp3/internal/cache/CacheStrategy;", "", "networkRequest", "Lokhttp3/Request;", "cacheResponse", "Lokhttp3/Response;", "(Lokhttp3/Request;Lokhttp3/Response;)V", "getCacheResponse", "()Lokhttp3/Response;", "getNetworkRequest", "()Lokhttp3/Request;", "Companion", "Factory", "okhttp"}, k = 1, mv = {1, 4, 0})
/* loaded from: classes3.dex */
public final class CacheStrategy {
/* renamed from: Companion, reason: from kotlin metadata */
public static final Companion INSTANCE = new Companion(null);
private final Response cacheResponse;
private final Request networkRequest;
public final Response getCacheResponse() {
return this.cacheResponse;
}
public final Request getNetworkRequest() {
return this.networkRequest;
}
public CacheStrategy(Request request, Response response) {
this.networkRequest = request;
this.cacheResponse = response;
}
/* compiled from: CacheStrategy.kt */
@Metadata(bv = {1, 0, 3}, d1 = {"\u0000B\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0000\n\u0002\u0010\t\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\b\n\u0000\n\u0002\u0010\u000e\n\u0000\n\u0002\u0018\u0002\n\u0002\b\n\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0010\u000b\n\u0002\b\u0002\u0018\u00002\u00020\u0001B\u001f\u0012\u0006\u0010\u0002\u001a\u00020\u0003\u0012\u0006\u0010\u0004\u001a\u00020\u0005\u0012\b\u0010\u0006\u001a\u0004\u0018\u00010\u0007¢\u0006\u0002\u0010\bJ\b\u0010\u0017\u001a\u00020\u0003H\u0002J\u0006\u0010\u0018\u001a\u00020\u0019J\b\u0010\u001a\u001a\u00020\u0019H\u0002J\b\u0010\u001b\u001a\u00020\u0003H\u0002J\u0010\u0010\u001c\u001a\u00020\u001d2\u0006\u0010\u0004\u001a\u00020\u0005H\u0002J\b\u0010\u001e\u001a\u00020\u001dH\u0002R\u000e\u0010\t\u001a\u00020\nX\u0082\u000e¢\u0006\u0002\n\u0000R\u0010\u0010\u0006\u001a\u0004\u0018\u00010\u0007X\u0082\u0004¢\u0006\u0002\n\u0000R\u0010\u0010\u000b\u001a\u0004\u0018\u00010\fX\u0082\u000e¢\u0006\u0002\n\u0000R\u0010\u0010\r\u001a\u0004\u0018\u00010\u000eX\u0082\u000e¢\u0006\u0002\n\u0000R\u0010\u0010\u000f\u001a\u0004\u0018\u00010\u000eX\u0082\u000e¢\u0006\u0002\n\u0000R\u0010\u0010\u0010\u001a\u0004\u0018\u00010\fX\u0082\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\u0002\u001a\u00020\u0003X\u0082\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\u0011\u001a\u00020\u0003X\u0082\u000e¢\u0006\u0002\n\u0000R\u0014\u0010\u0004\u001a\u00020\u0005X\u0080\u0004¢\u0006\b\n\u0000\u001a\u0004\b\u0012\u0010\u0013R\u000e\u0010\u0014\u001a\u00020\u0003X\u0082\u000e¢\u0006\u0002\n\u0000R\u0010\u0010\u0015\u001a\u0004\u0018\u00010\u000eX\u0082\u000e¢\u0006\u0002\n\u0000R\u0010\u0010\u0016\u001a\u0004\u0018\u00010\fX\u0082\u000e¢\u0006\u0002\n\u0000¨\u0006\u001f"}, d2 = {"Lokhttp3/internal/cache/CacheStrategy$Factory;", "", "nowMillis", "", SentryBaseEvent.JsonKeys.REQUEST, "Lokhttp3/Request;", "cacheResponse", "Lokhttp3/Response;", "(JLokhttp3/Request;Lokhttp3/Response;)V", "ageSeconds", "", "etag", "", "expires", "Ljava/util/Date;", "lastModified", "lastModifiedString", "receivedResponseMillis", "getRequest$okhttp", "()Lokhttp3/Request;", "sentRequestMillis", "servedDate", "servedDateString", "cacheResponseAge", "compute", "Lokhttp3/internal/cache/CacheStrategy;", "computeCandidate", "computeFreshnessLifetime", "hasConditions", "", "isFreshnessLifetimeHeuristic", "okhttp"}, k = 1, mv = {1, 4, 0})
/* loaded from: classes3.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;
/* renamed from: getRequest$okhttp, reason: from getter */
public final Request getRequest() {
return this.request;
}
public Factory(long j, Request request, Response response) {
Intrinsics.checkNotNullParameter(request, "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 (StringsKt.equals(name, HttpHeaders.DATE, true)) {
this.servedDate = DatesKt.toHttpDateOrNull(value);
this.servedDateString = value;
} else if (StringsKt.equals(name, HttpHeaders.EXPIRES, true)) {
this.expires = DatesKt.toHttpDateOrNull(value);
} else if (StringsKt.equals(name, HttpHeaders.LAST_MODIFIED, true)) {
this.lastModified = DatesKt.toHttpDateOrNull(value);
this.lastModifiedString = value;
} else if (StringsKt.equals(name, HttpHeaders.ETAG, true)) {
this.etag = value;
} else if (StringsKt.equals(name, HttpHeaders.AGE, true)) {
this.ageSeconds = Util.toNonNegativeInt(value, -1);
}
}
}
}
private final boolean isFreshnessLifetimeHeuristic() {
Response response = this.cacheResponse;
Intrinsics.checkNotNull(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.INSTANCE.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 > CalendarModelKt.MillisecondsIn24Hours && 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();
Intrinsics.checkNotNull(str2);
newBuilder2.addLenient$okhttp(str, str2);
return new CacheStrategy(this.request.newBuilder().headers(newBuilder2.build()).build(), this.cacheResponse);
}
private final long computeFreshnessLifetime() {
Response response = this.cacheResponse;
Intrinsics.checkNotNull(response);
if (response.cacheControl().maxAgeSeconds() != -1) {
return TimeUnit.SECONDS.toMillis(r0.maxAgeSeconds());
}
Date date = this.expires;
if (date != null) {
Date date2 = this.servedDate;
long time = date.getTime() - (date2 != null ? date2.getTime() : this.receivedResponseMillis);
if (time > 0) {
return time;
}
return 0L;
}
if (this.lastModified == null || this.cacheResponse.request().url().query() != null) {
return 0L;
}
Date date3 = this.servedDate;
long time2 = date3 != null ? date3.getTime() : this.sentRequestMillis;
Date date4 = this.lastModified;
Intrinsics.checkNotNull(date4);
long time3 = time2 - date4.getTime();
if (time3 > 0) {
return time3 / 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;
}
}
/* compiled from: CacheStrategy.kt */
@Metadata(bv = {1, 0, 3}, d1 = {"\u0000\u001e\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0002\n\u0002\u0010\u000b\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\b\u0086\u0003\u0018\u00002\u00020\u0001B\u0007\b\u0002¢\u0006\u0002\u0010\u0002J\u0016\u0010\u0003\u001a\u00020\u00042\u0006\u0010\u0005\u001a\u00020\u00062\u0006\u0010\u0007\u001a\u00020\\u0006\t"}, d2 = {"Lokhttp3/internal/cache/CacheStrategy$Companion;", "", "()V", "isCacheable", "", io.sentry.protocol.Response.TYPE, "Lokhttp3/Response;", SentryBaseEvent.JsonKeys.REQUEST, "Lokhttp3/Request;", "okhttp"}, k = 1, mv = {1, 4, 0})
/* loaded from: classes3.dex */
public static final class Companion {
private Companion() {
}
public /* synthetic */ Companion(DefaultConstructorMarker defaultConstructorMarker) {
this();
}
public final boolean isCacheable(Response response, Request request) {
Intrinsics.checkNotNullParameter(response, "response");
Intrinsics.checkNotNullParameter(request, "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().getIsPublic() && !response.cacheControl().getIsPrivate()) {
return false;
}
}
return (response.cacheControl().noStore() || request.cacheControl().noStore()) ? false : true;
}
}
}