Scoop API

scoop:scoop.http.client:0.1.0

src/cache/policy.scoop

Selected production source · src/cache/policy.scoop

1package scoop.http.client
3import scoop.http.*
4import scoop.time.Duration
5import scoop.time.Instant
7internal class ClientCachePolicy(val record: HttpCacheRecord) {
8 val control: HttpCacheControl = HttpCacheControl.parseFields(record.head.fields)
9 private val date: Instant = record.responseDate
10 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 corrected
15 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 false
22 if (clientCacheHas(record.head.fields, "Date") && !clientPresent(record.parsedDate))
23 return false
24 var freshness = lifetime
25 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 true
29 if (control.contains("must-revalidate")) return false
30 val stale = request.single("max-stale") ?: return false
31 if (!clientPresent(stale.value)) return true
32 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.ZERO
40 return clientCacheElapsed(expires, date)
41 }
42 if (!clientCacheDefaultStatus(record.head.status)) return Duration.ZERO
43 val modified = clientCacheDate(record.head.fields, "Last-Modified", record.receivedAt) ?: return Duration.ZERO
44 val heuristic = clientCacheElapsed(date, modified) / 10L
45 val limit = Duration.days(1L)
46 return if (heuristic > limit) limit else heuristic
47 }
50private fun cacheDelta(control: HttpCacheControl, name: String): Duration? =
51 when (control.single(name)) { case Some(val value) -> Some(Duration.seconds(value.deltaSeconds())); case None -> None }
53internal 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) }
58internal fun clientCacheAdd(first: Duration, second: Duration): Duration {
59 try { return first + second }
60 catch (_: ArithmeticException) { return Duration.seconds(Long.MAX_VALUE, 999999999L) }
63internal 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 == 501U
67internal 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 false
70 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 false
73 return clientCacheDefaultStatus(head.status) || control.contains("max-age") || control.contains("public") ||
74 control.contains("private") || clientCacheHas(head.fields, "Expires")