Scoop API

scoop:scoop.http.client:0.1.0

src/cache/call.scoop

Selected production source · src/cache/call.scoop

1package scoop.http.client
3import scoop.http.*
4import scoop.time.Instant
6internal class ClientCallCache(val options: HttpCacheOptions, val request: HttpCacheRequest) {
7 var snapshot: HttpCacheSnapshot? = None
8 var writer: HttpCacheWriter? = None
9 var sentAt: Instant = options.clock.now()
10 var receivedAt: Instant = sentAt
13internal suspend fun prepareClientCache(call: ClientCall): HttpResponseHead? {
14 val request = call.request
15 val control =
16 try {
17 Some(HttpCacheControl.parseFields(request.fields))
18 }
19 catch (_: HttpFieldParseException) {
20 None
21 }
22 val only =
23 request.options.cacheMode ==
24 HttpCacheMode.OnlyIfCached ||
25 (control?.contains("only-if-cached") ?: false)
26 val refresh = call.sequence.cacheRefresh
27 call.sequence.cacheRefresh = false
28 val options = call.client.config.cache
29 if (
30 request.options.cacheMode ==
31 HttpCacheMode.Bypass ||
32 !clientPresent(options) ||
33 !clientPresent(control) ||
34 control!!.contains("no-store") ||
35 !clientCacheEligible(request)
36 )
37 return if (only)
38 Some(clientCacheUnsatisfied(call))
39 else
40 None
41 val cache =
42 ClientCallCache(
43 options!!,
44 HttpCacheRequest(clientCacheKey(call), clientRequestSnapshotFields(call))
45 )
46 call.cache = Some(cache)
47 if (!refresh) {
48 val saved = cache.options.storage.lookup(cache.request)
49 call.lock.withLock { cache.snapshot = saved }
50 requireClientCall(call)
51 if (clientPresent(saved)) {
52 val mode =
53 if (clientCachePragmaNoCache(request.fields))
54 HttpCacheMode.Reload
55 else
56 request.options.cacheMode
57 val policy =
58 try {
59 Some(ClientCachePolicy(saved!!.record))
60 } catch (_: HttpFieldParseException) {
61 None
62 }
63 val usable =
64 try {
65 clientPresent(policy) &&
66 policy!!.canServe(control!!, mode, cache.options.clock.now())
67 }
68 catch (_: HttpFieldParseException) {
69 false
70 }
71 if (usable) {
72 call.cacheStatus = HttpCacheStatus.Hit
73 call.responseStream =
74 Some<ClientResponseStream>(ClientCachedResponseStream(saved!!))
75 return Some(
76 clientCacheWithAge(
77 saved!!.record.head,
78 policy!!.age(cache.options.clock.now()).inWholeSeconds
79 )
80 )
81 }
82 if (!only) {
83 val condition = clientCacheCondition(saved!!.record)
84 if (clientPresent(condition)) {
85 val fields = ArrayList<HttpField>()
86 for (field in request.suppliedFields)
87 fields.add(field)
88 fields.add(condition!!)
89 call.request =
90 httpRequest(request.url) {
91 this.method = request.method
92 this.fields = fields
93 this.body = request.body
94 this.options = request.options
95 }
96 return None
97 }
98 }
99 closeClientCacheSnapshot(call)
100 }
101 }
102 return if (only)
103 Some(clientCacheUnsatisfied(call))
104 else
105 None
108private fun clientCacheEligible(request: HttpRequest): Boolean {
109 if (
110 request.method !=
111 "GET" ||
112 request.options.duplex ||
113 request.contentLength !=
114 Some(0L) ||
115 request.body.trailerNames.size !=
116 0L
117 )
118 return false
119 for (
120 name in [
121 "Range",
122 "If-Match",
123 "If-None-Match",
124 "If-Modified-Since",
125 "If-Unmodified-Since",
126 "If-Range"
127 ]
128 )
129 if (clientCacheHas(request.fields, name))
130 return false
131 return true
134private fun clientCachePragmaNoCache(fields: List<HttpField>): Boolean {
135 if (clientCacheHas(fields, "Cache-Control"))
136 return false
137 for (field in fields)
138 if (field.hasName("Pragma".toByteArray())) {
139 val directives = HttpCacheControl.parseOrNone(field.value)
140 if (directives?.contains("no-cache") ?: false)
141 return true
142 }
143 return false
146private fun clientCacheCondition(record: HttpCacheRecord): HttpField? {
147 val etag = clientCacheField(record.head.fields, "ETag")
148 if (clientPresent(etag) && clientPresent(HttpEntityTag.parseOrNone(etag!!.value)))
149 return Some(HttpField("If-None-Match".toByteArray(), etag!!.value))
150 val modified = clientCacheField(record.head.fields, "Last-Modified")
151 if (
152 clientPresent(modified) &&
153 clientPresent(HttpDate.parseOrNone(modified!!.value, record.receivedAt, true))
154 )
155 return Some(HttpField("If-Modified-Since".toByteArray(), modified!!.value))
156 return None
159private fun clientCacheUnsatisfied(call: ClientCall): HttpResponseHead {
160 call.cacheStatus = HttpCacheStatus.Unsatisfied
161 call.responseStream = Some<ClientResponseStream>(ClientEmptyCachedResponse())
162 return HttpResponseHead(
163 HttpVersion.Http11,
164 504U,
165 fields = [HttpField("Content-Length".toByteArray(), "0".toByteArray())]
166 )
169internal fun clientResponseFromCache(call: ClientCall): Boolean =
170 call.cacheStatus != HttpCacheStatus.Network
172internal fun markClientCacheSent(call: ClientCall) {
173 val cache = call.cache ?: return
174 cache.sentAt = cache.options.clock.now()
177internal fun markClientCacheReceived(call: ClientCall) {
178 val cache = call.cache ?: return
179 cache.receivedAt = cache.options.clock.now()