Skip to content

Commit 6c617e8

Browse files
luuilluluxinwei
and
luxinwei
authored
Fix: Staled cache response used when system time adjusted backward (#8077)
* 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 <[email protected]>
1 parent 60d2a54 commit 6c617e8

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

okhttp/src/jvmMain/kotlin/okhttp3/internal/cache/CacheStrategy.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,8 @@ class CacheStrategy internal constructor(
272272
apparentReceivedAge
273273
}
274274

275-
val responseDuration = receivedResponseMillis - sentRequestMillis
276-
val residentDuration = nowMillis - receivedResponseMillis
275+
val responseDuration = maxOf(0, receivedResponseMillis - sentRequestMillis)
276+
val residentDuration = maxOf(0, nowMillis - receivedResponseMillis)
277277
return receivedAge + responseDuration + residentDuration
278278
}
279279

0 commit comments

Comments
 (0)