scoop:scoop.http.client:0.1.0
src/cache/policy.scoop
Selected production source · src/cache/policy.scoop
1
package scoop.http.client3
import scoop.http.*4
import scoop.time.Duration5
import scoop.time.Instant7
internal class ClientCachePolicy(val record: HttpCacheRecord) {8
val control: HttpCacheControl = HttpCacheControl.parseFields(record.head.fields)9
private val date: Instant = record.responseDate10
private val delay: Duration = clientCacheElapsed(record.receivedAt, record.sentAt)11
private val apparent: Duration = clientCacheElapsed(record.receivedAt, date)12
private val wireAge: Duration = Duration.seconds(HttpAge.parseFields(record.head.fields)?.seconds ?: 0L)13
private val corrected: Duration = clientCacheAdd(wireAge, delay)14
private val initialAge: Duration = if (apparent > corrected) apparent else corrected15
val lifetime: Duration get() = cacheLifetime()17
fun age(now: Instant): Duration = clientCacheAdd(initialAge, clientCacheElapsed(now, record.receivedAt))19
fun canServe(request: HttpCacheControl, mode: HttpCacheMode, now: Instant): Boolean {20
if (mode == HttpCacheMode.Reload || control.contains("no-store") || control.contains("no-cache") ||21
request.contains("no-cache") || HttpVary.parseFields(record.head.fields).isWildcard) return false22
if (clientCacheHas(record.head.fields, "Date") && !clientPresent(record.parsedDate))23
return false24
var freshness = lifetime25
val maximum = cacheDelta(request, "max-age")26
if (clientPresent(maximum) && maximum!! < freshness) freshness = maximum!!27
val current = clientCacheAdd(age(now), cacheDelta(request, "min-fresh") ?: Duration.ZERO)28
if (current < freshness) return true29
if (control.contains("must-revalidate")) return false30
val stale = request.single("max-stale") ?: return false31
if (!clientPresent(stale.value)) return true32
return current <= clientCacheAdd(freshness, Duration.seconds(stale.deltaSeconds()))33
}35
private fun cacheLifetime(): Duration {36
val maximum = cacheDelta(control, "max-age")37
if (clientPresent(maximum)) return maximum!!38
if (clientCacheHas(record.head.fields, "Expires")) {39
val expires = clientCacheDate(record.head.fields, "Expires", record.receivedAt) ?: return Duration.ZERO40
return clientCacheElapsed(expires, date)41
}42
if (!clientCacheDefaultStatus(record.head.status)) return Duration.ZERO43
val modified = clientCacheDate(record.head.fields, "Last-Modified", record.receivedAt) ?: return Duration.ZERO44
val heuristic = clientCacheElapsed(date, modified) / 10L45
val limit = Duration.days(1L)46
return if (heuristic > limit) limit else heuristic47
}48
}50
private fun cacheDelta(control: HttpCacheControl, name: String): Duration? =51
when (control.single(name)) { case Some(val value) -> Some(Duration.seconds(value.deltaSeconds())); case None -> None }53
internal fun clientCacheElapsed(later: Instant, earlier: Instant): Duration {54
try { return if (later > earlier) later - earlier else Duration.ZERO }55
catch (_: ArithmeticException) { return Duration.seconds(Long.MAX_VALUE, 999999999L) }56
}58
internal fun clientCacheAdd(first: Duration, second: Duration): Duration {59
try { return first + second }60
catch (_: ArithmeticException) { return Duration.seconds(Long.MAX_VALUE, 999999999L) }61
}63
internal fun clientCacheDefaultStatus(status: UInt): Boolean =64
status == 200U || status == 203U || status == 204U || status == 300U || status == 301U || status == 308U ||65
status == 404U || status == 405U || status == 410U || status == 414U || status == 501U67
internal fun clientMayStoreCache(request: HttpRequest, head: HttpResponseHead): Boolean {68
if (request.method != "GET" || request.contentLength != Some(0L) || head.status < 200U || head.status > 599U || head.status == 206U ||69
head.status == 304U || clientCacheHas(request.fields, "Range") || clientCacheHas(head.fields, "Content-Range")) return false70
val requested = HttpCacheControl.parseFields(request.fields)71
val control = HttpCacheControl.parseFields(head.fields)72
if (requested.contains("no-store") || control.contains("no-store") || HttpVary.parseFields(head.fields).isWildcard) return false73
return clientCacheDefaultStatus(head.status) || control.contains("max-age") || control.contains("public") ||74
control.contains("private") || clientCacheHas(head.fields, "Expires")75
}