rl-client: never cache 4/5xx requests

This commit is contained in:
Max Weber
2021-05-29 15:01:58 -06:00
committed by Adam
parent b6462e4619
commit 14545aac26
2 changed files with 147 additions and 2 deletions

View File

@@ -73,6 +73,7 @@ import net.runelite.client.ui.overlay.worldmap.WorldMapOverlay;
import net.runelite.http.api.RuneLiteAPI;
import okhttp3.Cache;
import okhttp3.OkHttpClient;
import okhttp3.Response;
import org.slf4j.LoggerFactory;
@Singleton
@@ -191,8 +192,8 @@ public class RuneLite
}
});
OkHttpClient.Builder okHttpClientBuilder = RuneLiteAPI.CLIENT.newBuilder()
.cache(new Cache(new File(CACHE_DIR, "okhttp"), MAX_OKHTTP_CACHE_SIZE));
OkHttpClient.Builder okHttpClientBuilder = RuneLiteAPI.CLIENT.newBuilder();
setupCache(okHttpClientBuilder, new File(CACHE_DIR, "okhttp"));
final boolean insecureSkipTlsVerification = options.has("insecure-skip-tls-verification");
if (insecureSkipTlsVerification || RuneLiteProperties.isInsecureSkipTlsVerification())
@@ -381,6 +382,25 @@ public class RuneLite
}
}
@VisibleForTesting
static void setupCache(OkHttpClient.Builder builder, File cacheDir)
{
builder.cache(new Cache(cacheDir, MAX_OKHTTP_CACHE_SIZE))
.addNetworkInterceptor(chain ->
{
// This has to be a network interceptor so it gets hit before the cache tries to store stuff
Response res = chain.proceed(chain.request());
if (res.code() >= 400 && "GET".equals(res.request().method()))
{
// if the request 404'd we don't want to cache it because its probably temporary
res = res.newBuilder()
.header("Cache-Control", "no-store")
.build();
}
return res;
});
}
private static void setupInsecureTrustManager(OkHttpClient.Builder okHttpClientBuilder)
{
try