Scoop API

scoop:scoop.http:0.1.0

src/cookie/cookie.scoop

Selected production source · src/cookie/cookie.scoop

1package scoop.http
3import scoop.Result
4import scoop.flatMap
5import scoop.time.Instant
6import scoop.url.*
8/**
9 * Why a parsed Set-Cookie cannot be accepted for its response URL.
10 */
11public enum HttpCookieRejection {
12 /**
13 * Both cookie name and value are empty.
14 */
15 EmptyPair,
16 /**
17 * The Domain attribute is not an acceptable host.
18 */
19 InvalidDomain,
20 /**
21 * The response host does not domain-match the cookie domain.
22 */
23 DomainMismatch,
24 /**
25 * The Domain attribute attempts to cover an unacceptable public suffix.
26 */
27 PublicSuffix,
28 /**
29 * A Secure cookie arrived over a nonsecure scheme.
30 */
31 InsecureTransport,
32 /**
33 * SameSite=None was supplied without Secure.
34 */
35 SameSiteWithoutSecure,
36 /**
37 * The __Secure- or __Host- cookie prefix requirements were not met.
38 */
39 InvalidPrefix }
41/**
42 * A resolved cookie with host/path scope, expiry, and flags. URL matching and SameSite decisions
43 * are separate operations.
44 */
45public class HttpCookie internal constructor(
46 /**
47 * The cookie name/value pair.
48 */
49 public val pair: HttpCookiePair,
50 /**
51 * The resolved host or domain scope.
52 */
53 public val host: UrlHost,
54 /**
55 * Whether only the exact host matches, rather than its subdomains.
56 */
57 public val hostOnly: Boolean,
58 /**
59 * The cookie path bytes.
60 */
61 public val path: Array<Byte>,
62 /**
63 * The resolved absolute expiry, or None for a session cookie.
64 */
65 public val expiresAt: Instant?,
66 /**
67 * Whether transmission requires a secure scheme.
68 */
69 public val secure: Boolean,
70 /**
71 * Whether the cookie is marked inaccessible to non-HTTP scripting APIs.
72 */
73 public val httpOnly: Boolean,
74 /**
75 * The interpreted SameSite policy.
76 */
77 public val sameSite: HttpCookieSameSite
78) {
79 /**
80 * Tests expiry against the supplied current instant; session cookies do not expire by this
81 * check.
82 */
83 public fun isExpired(now: Instant): Boolean = when (expiresAt) { case None -> false; case Some(val date) -> date <= now }
84 /**
85 * Checks expiry, secure scheme, host/domain, and path for the URL; apply SameSite policy
86 * separately.
87 */
88 public fun matches(url: Url, now: Instant): Boolean {
89 if (isExpired(now)) return false
90 val origin = url.httpOrigin()
91 if (secure && !secureCookieScheme(origin.scheme)) return false
92 if (hostOnly) { if (origin.host != host) return false }
93 else if (!HttpCookieRules.domainMatches(origin.host, host)) return false
94 val encodedPath = if (url.encodedPath.charLength == 0L) "/" else url.encodedPath
95 return HttpCookieRules.pathMatches(encodedPath.toByteArray(), path)
96 }
97 /**
98 * Evaluates SameSite using explicit same-site, top-level-navigation, and safe-method context.
99 */
100 public fun allowsSameSite(isSameSiteRequest: Boolean, isTopLevelNavigation: Boolean, isSafeMethod: Boolean): Boolean = when (sameSite) {
101 case HttpCookieSameSite.None -> true
102 case HttpCookieSameSite.Strict -> isSameSiteRequest
103 case HttpCookieSameSite.Lax -> isSameSiteRequest || (isTopLevelNavigation && isSafeMethod)
104 case HttpCookieSameSite.Default -> isSameSiteRequest || (isTopLevelNavigation && isSafeMethod)
105 }
106 /**
107 * Tests whether this cookie's SameSite policy permits setting it in the supplied request
108 * context.
109 */
110 public fun allowsSetting(isSameSiteRequest: Boolean, isTopLevelNavigation: Boolean): Boolean =
111 sameSite == HttpCookieSameSite.None || isSameSiteRequest || isTopLevelNavigation
114internal fun resolveHttpCookie(value: HttpSetCookie, url: Url, now: Instant, suffixes: PublicSuffixList, maximum: Long): Result<HttpCookie, HttpCookieRejection> {
115 val expiry = value.expiryFrom(now, maximum)
116 val origin = url.httpOrigin()
117 if (value.pair.name.size == 0L && value.pair.value.size == 0L) return Result.Failure(HttpCookieRejection.EmptyPair)
118 return resolveCookieDomain(origin, value.domain, suffixes).flatMap { (host, hostOnly) ->
119 finishHttpCookie(value, url.encodedPath, origin.scheme, expiry, host, hostOnly)
120 }
123private fun finishHttpCookie(value: HttpSetCookie, encodedPath: String, scheme: String, expiry: Instant?, host: UrlHost, hostOnly: Boolean): Result<HttpCookie, HttpCookieRejection> {
124 val path = when (value.path) {
125 case Some(val bytes) -> if (bytes.size > 0L && bytes[0L] == 47.toByte()) bytes else HttpCookieRules.defaultPath(encodedPath)
126 case None -> HttpCookieRules.defaultPath(encodedPath)
127 }
128 if (value.secure && !secureCookieScheme(scheme)) return Result.Failure(HttpCookieRejection.InsecureTransport)
129 if (value.sameSite == HttpCookieSameSite.None && !value.secure) return Result.Failure(HttpCookieRejection.SameSiteWithoutSecure)
130 val securePrefix = cookiePrefix(value.pair.name, "__secure-")
131 val hostPrefix = cookiePrefix(value.pair.name, "__host-")
132 val hasPath = when (value.path) { case None -> false; case Some(_) -> true }
133 if (((securePrefix || hostPrefix) && !value.secure) ||
134 (hostPrefix && (!hostOnly || !hasPath || !httpBytesEqual(path, "/".toByteArray()))) ||
135 (value.pair.name.size == 0L && (cookiePrefix(value.pair.value, "__secure-") || cookiePrefix(value.pair.value, "__host-"))))
136 return Result.Failure(HttpCookieRejection.InvalidPrefix)
137 return Result.Success(HttpCookie(value.pair, host, hostOnly, path, expiry, value.secure, value.httpOnly, value.sameSite))