Scoop API

scoop:scoop.http.client:0.1.0

src/cache/fields.scoop

Selected production source · src/cache/fields.scoop

1package scoop.http.client
3import scoop.bytes.ByteArrayBuilder
4import scoop.bytes.ByteOrder
5import scoop.collections.HashMap
6import scoop.collections.Map
7import scoop.crypto.DigestAlgorithm
8import scoop.crypto.digest
9import scoop.http.*
10import scoop.text.lowercaseAscii
11import scoop.text.toHexString
12import scoop.time.Instant
14internal val clientEmptyCacheFingerprint: String = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
16internal fun clientCacheFingerprints(fields: List<HttpField>): Map<String, String> {
17 val result = HashMap<String, String>()
18 for (field in fields) {
19 val name = String.fromUtf8(field.name).lowercaseAscii()
20 if (!result.containsKey(name)) result.put(name, clientCacheFieldFingerprint(fields, name))
21 }
22 return result
25internal fun clientCacheFieldFingerprint(fields: List<HttpField>, name: String): String {
26 val output = ByteArrayBuilder()
27 for (field in fields) if (field.hasName(name.toByteArray())) {
28 output.writeInt64(field.value.size, ByteOrder.BigEndian)
29 output.append(field.value)
30 }
31 return digest(DigestAlgorithm.Sha256, output.toByteArray()).toHexString()
34internal fun clientCacheField(fields: List<HttpField>, name: String): HttpField? {
35 var found: HttpField? = None
36 for (field in fields) if (field.hasName(name.toByteArray())) {
37 if (clientPresent(found)) return None
38 found = Some(field)
39 }
40 return found
43internal fun clientCacheHas(fields: List<HttpField>, name: String): Boolean {
44 for (field in fields) if (field.hasName(name.toByteArray())) return true
45 return false
48internal fun clientCacheDate(fields: List<HttpField>, name: String, reference: Instant): Instant? {
49 val field = clientCacheField(fields, name) ?: return None
50 return HttpDate.parseOrNone(field.value, reference, true)
53internal fun clientStoredCacheHead(head: HttpResponseHead): HttpResponseHead {
54 val connection = parseHttpTokenFields(head.fields, "connection")
55 val kept = ArrayList<HttpField>()
56 for (field in head.fields) {
57 val name = String.fromUtf8(field.name).lowercaseAscii()
58 if (clientCacheHopField(name)) continue
59 var excluded = false
60 for (option in connection) if (option == name) excluded = true
61 if (!excluded) kept.add(field)
62 }
63 return HttpResponseHead(head.version, head.status, clientCacheReason(head), kept, head.neverIndexStatus)
66private fun clientCacheHopField(name: String): Boolean =
67 name == "connection" || name == "keep-alive" || name == "proxy-authenticate" || name == "proxy-authorization" ||
68 name == "te" || name == "trailer" || name == "transfer-encoding" || name == "upgrade"
70internal fun clientCacheWithAge(head: HttpResponseHead, seconds: Long): HttpResponseHead {
71 val fields = ArrayList<HttpField>()
72 for (field in head.fields) if (!field.hasName("Age".toByteArray())) fields.add(field)
73 fields.add(HttpAge(seconds).toHttpField())
74 return HttpResponseHead(head.version, head.status, clientCacheReason(head), fields, head.neverIndexStatus)
77internal fun clientCacheWithDate(head: HttpResponseHead, receivedAt: Instant): HttpResponseHead {
78 if (clientCacheHas(head.fields, "Date")) return head
79 val fields = ArrayList<HttpField>()
80 for (field in head.fields) fields.add(field)
81 fields.add(HttpField("Date".toByteArray(), HttpDate.format(receivedAt).toByteArray()))
82 return HttpResponseHead(head.version, head.status, clientCacheReason(head), fields, head.neverIndexStatus)
85internal fun clientMergeCachedHead(stored: HttpResponseHead, validating: HttpResponseHead, receivedAt: Instant): HttpResponseHead {
86 val fresh = clientStoredCacheHead(clientCacheWithDate(validating, receivedAt))
87 val fields = ArrayList<HttpField>()
88 for (field in stored.fields) {
89 if (field.hasName("Age".toByteArray())) continue
90 var replaced = false
91 for (current in fresh.fields) if (current.hasName(field.name)) replaced = true
92 if (!replaced) fields.add(field)
93 }
94 for (field in fresh.fields) fields.add(field)
95 return HttpResponseHead(validating.version, stored.status, clientCacheReason(stored), fields, stored.neverIndexStatus)
98internal fun clientCacheReason(head: HttpResponseHead): List<Byte>? =
99 when (head.reason) { case Some(val value) -> Some<List<Byte>>(value); case None -> None }