scoop:scoop.http.client:0.1.0
src/cache/call.scoop
Selected production source · src/cache/call.scoop
1
package scoop.http.client3
import scoop.http.*4
import scoop.time.Instant6
internal class ClientCallCache(val options: HttpCacheOptions, val request: HttpCacheRequest) {7
var snapshot: HttpCacheSnapshot? = None8
var writer: HttpCacheWriter? = None9
var sentAt: Instant = options.clock.now()10
var receivedAt: Instant = sentAt11
}13
internal suspend fun prepareClientCache(call: ClientCall): HttpResponseHead? {14
val request = call.request15
val control =16
try {17
Some(HttpCacheControl.parseFields(request.fields))18
}19
catch (_: HttpFieldParseException) {20
None21
}22
val only =23
request.options.cacheMode ==24
HttpCacheMode.OnlyIfCached ||25
(control?.contains("only-if-cached") ?: false)26
val refresh = call.sequence.cacheRefresh27
call.sequence.cacheRefresh = false28
val options = call.client.config.cache29
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
else40
None41
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.Reload55
else56
request.options.cacheMode57
val policy =58
try {59
Some(ClientCachePolicy(saved!!.record))60
} catch (_: HttpFieldParseException) {61
None62
}63
val usable =64
try {65
clientPresent(policy) &&66
policy!!.canServe(control!!, mode, cache.options.clock.now())67
}68
catch (_: HttpFieldParseException) {69
false70
}71
if (usable) {72
call.cacheStatus = HttpCacheStatus.Hit73
call.responseStream =74
Some<ClientResponseStream>(ClientCachedResponseStream(saved!!))75
return Some(76
clientCacheWithAge(77
saved!!.record.head,78
policy!!.age(cache.options.clock.now()).inWholeSeconds79
)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.method92
this.fields = fields93
this.body = request.body94
this.options = request.options95
}96
return None97
}98
}99
closeClientCacheSnapshot(call)100
}101
}102
return if (only)103
Some(clientCacheUnsatisfied(call))104
else105
None106
}108
private 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
0L117
)118
return false119
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 false131
return true132
}134
private fun clientCachePragmaNoCache(fields: List<HttpField>): Boolean {135
if (clientCacheHas(fields, "Cache-Control"))136
return false137
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 true142
}143
return false144
}146
private 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 None157
}159
private fun clientCacheUnsatisfied(call: ClientCall): HttpResponseHead {160
call.cacheStatus = HttpCacheStatus.Unsatisfied161
call.responseStream = Some<ClientResponseStream>(ClientEmptyCachedResponse())162
return HttpResponseHead(163
HttpVersion.Http11,164
504U,165
fields = [HttpField("Content-Length".toByteArray(), "0".toByteArray())]166
)167
}169
internal fun clientResponseFromCache(call: ClientCall): Boolean =170
call.cacheStatus != HttpCacheStatus.Network172
internal fun markClientCacheSent(call: ClientCall) {173
val cache = call.cache ?: return174
cache.sentAt = cache.options.clock.now()175
}177
internal fun markClientCacheReceived(call: ClientCall) {178
val cache = call.cache ?: return179
cache.receivedAt = cache.options.clock.now()180
}