scoop:scoop.http.client:0.1.0
src/cache/fields.scoop
Selected production source · src/cache/fields.scoop
1
package scoop.http.client3
import scoop.bytes.ByteArrayBuilder4
import scoop.bytes.ByteOrder5
import scoop.collections.HashMap6
import scoop.collections.Map7
import scoop.crypto.DigestAlgorithm8
import scoop.crypto.digest9
import scoop.http.*10
import scoop.text.lowercaseAscii11
import scoop.text.toHexString12
import scoop.time.Instant14
internal val clientEmptyCacheFingerprint: String = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"16
internal 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 result23
}25
internal 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()32
}34
internal fun clientCacheField(fields: List<HttpField>, name: String): HttpField? {35
var found: HttpField? = None36
for (field in fields) if (field.hasName(name.toByteArray())) {37
if (clientPresent(found)) return None38
found = Some(field)39
}40
return found41
}43
internal fun clientCacheHas(fields: List<HttpField>, name: String): Boolean {44
for (field in fields) if (field.hasName(name.toByteArray())) return true45
return false46
}48
internal fun clientCacheDate(fields: List<HttpField>, name: String, reference: Instant): Instant? {49
val field = clientCacheField(fields, name) ?: return None50
return HttpDate.parseOrNone(field.value, reference, true)51
}53
internal 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)) continue59
var excluded = false60
for (option in connection) if (option == name) excluded = true61
if (!excluded) kept.add(field)62
}63
return HttpResponseHead(head.version, head.status, clientCacheReason(head), kept, head.neverIndexStatus)64
}66
private 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"70
internal 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)75
}77
internal fun clientCacheWithDate(head: HttpResponseHead, receivedAt: Instant): HttpResponseHead {78
if (clientCacheHas(head.fields, "Date")) return head79
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)83
}85
internal 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())) continue90
var replaced = false91
for (current in fresh.fields) if (current.hasName(field.name)) replaced = true92
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)96
}98
internal fun clientCacheReason(head: HttpResponseHead): List<Byte>? =99
when (head.reason) { case Some(val value) -> Some<List<Byte>>(value); case None -> None }