scoop:scoop.http.client:0.1.0
src/cookie/store.scoop
Selected production source · src/cookie/store.scoop
1
package scoop.http.client3
import scoop.http.HttpCookie4
import scoop.http.HttpCookieRules5
import scoop.time.Instant6
import scoop.url.UrlHost8
internal class ClientStoredCookie(var cookie: HttpCookie, var lastAccess: Instant)10
internal fun sameClientCookieKey(first: HttpCookie, second: HttpCookie): Boolean =11
first.host == second.host && first.hostOnly == second.hostOnly &&12
clientBytesEqual(first.pair.name, second.pair.name) && clientBytesEqual(first.path, second.path)14
internal fun pruneClientCookies(entries: MutableList<ClientStoredCookie>, now: Instant) {15
var index = entries.size16
while (index > 0L) { index -= 1L; if (entries[index].cookie.isExpired(now)) entries.removeAt(index) }17
}19
internal fun clientCookieOverlaysSecure(entries: List<ClientStoredCookie>, cookie: HttpCookie): Boolean {20
for (entry in entries) {21
val previous = entry.cookie22
if (previous.secure && clientBytesEqual(previous.pair.name, cookie.pair.name) &&23
(HttpCookieRules.domainMatches(previous.host, cookie.host) || HttpCookieRules.domainMatches(cookie.host, previous.host)) &&24
HttpCookieRules.pathMatches(cookie.path, previous.path)) return true25
}26
return false27
}29
internal fun storeClientCookie(entries: MutableList<ClientStoredCookie>, cookie: HttpCookie, now: Instant) {30
for (index in 0L..<entries.size) {31
val previous = entries[index]32
if (!sameClientCookieKey(previous.cookie, cookie)) continue33
if (cookie.isExpired(now)) entries.removeAt(index)34
else { previous.cookie = cookie; previous.lastAccess = now }35
return36
}37
if (!cookie.isExpired(now)) entries.add(ClientStoredCookie(cookie, now))38
}40
internal fun trimClientCookies(entries: MutableList<ClientStoredCookie>, host: UrlHost?, limit: Long) {41
while (true) {42
var count = 0L43
var oldest = -1L44
for (index in 0L..<entries.size) {45
val entry = entries[index]46
if (clientPresent(host) && entry.cookie.host != host!!) continue47
count += 1L48
if (oldest < 0L || entry.lastAccess < entries[oldest].lastAccess) oldest = index49
}50
if (count <= limit) return51
entries.removeAt(oldest)52
}53
}