Scoop API

scoop:scoop.http.client:0.1.0

src/cookie/store.scoop

Selected production source · src/cookie/store.scoop

1package scoop.http.client
3import scoop.http.HttpCookie
4import scoop.http.HttpCookieRules
5import scoop.time.Instant
6import scoop.url.UrlHost
8internal class ClientStoredCookie(var cookie: HttpCookie, var lastAccess: Instant)
10internal 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)
14internal fun pruneClientCookies(entries: MutableList<ClientStoredCookie>, now: Instant) {
15 var index = entries.size
16 while (index > 0L) { index -= 1L; if (entries[index].cookie.isExpired(now)) entries.removeAt(index) }
19internal fun clientCookieOverlaysSecure(entries: List<ClientStoredCookie>, cookie: HttpCookie): Boolean {
20 for (entry in entries) {
21 val previous = entry.cookie
22 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 true
25 }
26 return false
29internal 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)) continue
33 if (cookie.isExpired(now)) entries.removeAt(index)
34 else { previous.cookie = cookie; previous.lastAccess = now }
35 return
36 }
37 if (!cookie.isExpired(now)) entries.add(ClientStoredCookie(cookie, now))
40internal fun trimClientCookies(entries: MutableList<ClientStoredCookie>, host: UrlHost?, limit: Long) {
41 while (true) {
42 var count = 0L
43 var oldest = -1L
44 for (index in 0L..<entries.size) {
45 val entry = entries[index]
46 if (clientPresent(host) && entry.cookie.host != host!!) continue
47 count += 1L
48 if (oldest < 0L || entry.lastAccess < entries[oldest].lastAccess) oldest = index
49 }
50 if (count <= limit) return
51 entries.removeAt(oldest)
52 }