From 6c617e88c15a6ae3fe45553bd50a215a5e4400d7 Mon Sep 17 00:00:00 2001 From: luuillu <xygood@qq.com> Date: Mon, 4 Dec 2023 05:49:24 +0800 Subject: [PATCH] Fix: Staled cache response used when system time adjusted backward (#8077) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix: Staled cache response used when system time adjusted backward。 OkHttp would incorrectly use a cached response for a second request if the server's first response did not contain caching headers, if the system time was adjusted backward between the two requests。 * clamp each of the durations to be non-negative instead of doing that to the overall result --------- Co-authored-by: luxinwei <luxinwei@itiger.com> --- .../jvmMain/kotlin/okhttp3/internal/cache/CacheStrategy.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/okhttp/src/jvmMain/kotlin/okhttp3/internal/cache/CacheStrategy.kt b/okhttp/src/jvmMain/kotlin/okhttp3/internal/cache/CacheStrategy.kt index af92345a0a54..917de871b6d9 100644 --- a/okhttp/src/jvmMain/kotlin/okhttp3/internal/cache/CacheStrategy.kt +++ b/okhttp/src/jvmMain/kotlin/okhttp3/internal/cache/CacheStrategy.kt @@ -272,8 +272,8 @@ class CacheStrategy internal constructor( apparentReceivedAge } - val responseDuration = receivedResponseMillis - sentRequestMillis - val residentDuration = nowMillis - receivedResponseMillis + val responseDuration = maxOf(0, receivedResponseMillis - sentRequestMillis) + val residentDuration = maxOf(0, nowMillis - receivedResponseMillis) return receivedAge + responseDuration + residentDuration }