scoop:scoop.http:0.1.0
src/cookie/set_cookie.scoop
Selected production source · src/cookie/set_cookie.scoop
1
package scoop.http3
import scoop.bytes.ByteArrayBuilder4
import scoop.collections.emptyList5
import scoop.Result6
import scoop.time.Instant7
import scoop.url.Url9
/**10
* One Set-Cookie field with its ordered attributes and interpreted cookie properties. Resolve11
* against the response URL before storing it.12
*/13
public class HttpSetCookie private constructor(14
/**15
* The cookie name/value pair.16
*/17
public val pair: HttpCookiePair,18
/**19
* Ordered original cookie attributes.20
*/21
public val attributes: Array<HttpCookieAttribute>,22
/**23
* The original parsed field-value bytes, or None for a programmatically constructed value.24
*/25
public val originalValue: Array<Byte>?26
) {27
public constructor(pair: HttpCookiePair, attributes: List<HttpCookieAttribute> = emptyList<HttpCookieAttribute>())28
: this(pair, attributes.toArray(), None) {}30
private val interpreted: CookieAttributes = interpretCookieAttributes(attributes)31
/**32
* The interpreted Expires date, when valid and present.33
*/34
public val expires: Instant? get() = interpreted.expires35
/**36
* The interpreted Max-Age in seconds, when present.37
*/38
public val maxAgeSeconds: Long? get() = interpreted.maxAge39
/**40
* The interpreted Domain attribute bytes, before resolution against an origin.41
*/42
public val domain: Array<Byte>? get() = interpreted.domain43
/**44
* The cookie path bytes.45
*/46
public val path: Array<Byte>? get() = interpreted.path47
/**48
* Whether transmission requires a secure scheme.49
*/50
public val secure: Boolean get() = interpreted.secure51
/**52
* Whether the cookie is marked inaccessible to non-HTTP scripting APIs.53
*/54
public val httpOnly: Boolean get() = interpreted.httpOnly55
/**56
* The interpreted SameSite policy.57
*/58
public val sameSite: HttpCookieSameSite get() = interpreted.sameSite60
/**61
* Calculates expiry, giving Max-Age precedence and applying the explicit maximum lifetime.62
*/63
public fun expiryFrom(now: Instant, maxLifetimeSeconds: Long = 34560000L): Instant? =64
cookieExpiry(expires, maxAgeSeconds, now, maxLifetimeSeconds)65
/**66
* Resolves and validates domain, public suffix, path, Secure, SameSite, and name-prefix rules67
* for the response URL.68
*/69
public fun resolve(url: Url, now: Instant, suffixes: PublicSuffixList, maxLifetimeSeconds: Long = 34560000L): Result<HttpCookie, HttpCookieRejection> =70
resolveHttpCookie(this, url, now, suffixes, maxLifetimeSeconds)72
/**73
* Serializes the structured value into HTTP field-value bytes.74
*/75
public fun toByteArray(): Array<Byte> {76
val output = ByteArrayBuilder()77
if (pair.name.size == 0L && pair.value.size > 0L) output.append(61.toByte())78
output.append(pair.toByteArray())79
for (attribute in attributes) { output.append("; ".toByteArray()); output.append(attribute.toByteArray()) }80
return output.toByteArray()81
}82
/**83
* Builds a separate Set-Cookie field with never-index enabled.84
*/85
public fun toHttpField(): HttpField = HttpField("Set-Cookie".toByteArray(), toByteArray(), neverIndex = true)87
/**88
* Construction and parsing of this structured HTTP field.89
*/90
public companion object {91
/**92
* Parses the complete field value, throwing HttpFieldParseException for malformed syntax.93
*/94
public fun parse(value: List<Byte>): HttpSetCookie {95
val reader = cookieFieldReader("Set-Cookie", value)96
val pair = HttpCookiePair.read(reader)97
val attributes = ArrayList<HttpCookieAttribute>()98
while (!reader.ended) {99
reader.expect(59)100
attributes.add(HttpCookieAttribute.read(reader))101
}102
return HttpSetCookie(pair, attributes.toArray(), Some(reader.bytes))103
}104
/**105
* Parses the complete field value, throwing HttpFieldParseException for malformed syntax.106
*/107
public fun parse(value: String): HttpSetCookie = parse(value.toByteArray())108
/**109
* Parses the complete value, returning None for HttpFieldParseException while propagating110
* unrelated failures.111
*/112
public fun parseOrNone(value: List<Byte>): HttpSetCookie? {113
try { return Some(parse(value)) }114
catch (_: HttpFieldParseException) { return None }115
}116
/**117
* Parses the complete value, returning None for HttpFieldParseException while propagating118
* unrelated failures.119
*/120
public fun parseOrNone(value: String): HttpSetCookie? = parseOrNone(value.toByteArray())121
}122
}