scoop:scoop.http:0.1.0
src/cookie/attributes.scoop
Selected production source · src/cookie/attributes.scoop
1
package scoop.http3
import scoop.time.Instant5
/**6
* SameSite policy associated with a cookie.7
*/8
public enum HttpCookieSameSite {9
/**10
* Uses the library's Lax behavior when no recognized SameSite value is supplied.11
*/12
Default,13
/**14
* Allows only same-site requests.15
*/16
Strict,17
/**18
* Allows same-site requests or safe top-level navigations.19
*/20
Lax,21
/**22
* Allows cross-site use, requiring Secure when the cookie is resolved.23
*/24
None }26
internal class CookieAttributes(27
val expires: Instant?, val maxAge: Long?, val domain: Array<Byte>?, val path: Array<Byte>?,28
val secure: Boolean, val httpOnly: Boolean, val sameSite: HttpCookieSameSite29
)31
internal fun interpretCookieAttributes(attributes: List<HttpCookieAttribute>): CookieAttributes {32
var expires: Instant? = None33
var maxAge: Long? = None34
var domain: Array<Byte>? = None35
var path: Array<Byte>? = None36
var secure = false37
var httpOnly = false38
var sameSite = HttpCookieSameSite.Default39
for (attribute in attributes) {40
val value: Array<Byte> = when (attribute.value) { case None -> []; case Some(val bytes) -> bytes }41
if (value.size > 1024L) continue42
if (attribute.hasName("expires")) {43
when (HttpCookieDate.parseOrNone(value)) { case None -> {}; case Some(val date) -> expires = Some(date) }44
} else if (attribute.hasName("max-age")) {45
when (cookieMaxAge(value)) { case None -> {}; case Some(val seconds) -> maxAge = Some(seconds) }46
} else if (attribute.hasName("domain")) domain = Some(cookieDomainBytes(value))47
else if (attribute.hasName("path")) path = Some(value)48
else if (attribute.hasName("secure")) secure = true49
else if (attribute.hasName("httponly")) httpOnly = true50
else if (attribute.hasName("samesite")) {51
sameSite = if (httpAsciiEqual(value, "strict".toByteArray())) HttpCookieSameSite.Strict52
else if (httpAsciiEqual(value, "lax".toByteArray())) HttpCookieSameSite.Lax53
else if (httpAsciiEqual(value, "none".toByteArray())) HttpCookieSameSite.None else HttpCookieSameSite.Default54
}55
}56
return CookieAttributes(expires, maxAge, domain, path, secure, httpOnly, sameSite)57
}59
private fun cookieDomainBytes(value: Array<Byte>): Array<Byte> {60
val start = if (value.size > 0L && value[0L] == 46.toByte()) 1L else 0L61
return Array<Byte>(value.size - start) { index ->62
val c = value[start + index].toUByte().toInt()63
(if (c >= 65 && c <= 90) c + 32 else c).toByte()64
}65
}67
private fun cookieMaxAge(value: List<Byte>): Long? {68
if (value.size == 0L) return None69
val negative = value[0L] == 45.toByte()70
var index = if (negative) 1L else 0L71
if (index == value.size) return None72
var amount = 0L73
var overflow = false74
while (index < value.size) {75
val digit = value[index].toUByte().toLong() - 48L76
if (digit < 0L || digit > 9L) return None77
if (!overflow) {78
if (amount > (Long.MAX_VALUE - digit) / 10L) overflow = true79
else amount = amount * 10L + digit80
}81
index += 1L82
}83
if (overflow) return Some(if (negative) Long.MIN_VALUE else Long.MAX_VALUE)84
return Some(if (negative) -amount else amount)85
}