Scoop API

scoop:scoop.http:0.1.0

src/cookie/pair.scoop

Selected production source · src/cookie/pair.scoop

1package scoop.http
3import scoop.bytes.ByteArrayBuilder
5/**
6 * A copied cookie name/value byte pair with cookie syntax validation.
7 */
8public class HttpCookiePair private constructor(
9 /**
10 * The copied cookie name or attribute-name bytes.
11 */
12 public val name: Array<Byte>,
13 /**
14 * The copied cookie value or optional attribute-value bytes.
15 */
16 public val value: Array<Byte>) {
17 public constructor(name: List<Byte>, value: List<Byte>) : this(checkedCookieBytes(name, true), checkedCookieBytes(value, false)) {}
18 public constructor(name: String, value: String)
19 : this(checkedCookieBytes(name.toByteArray(), true), checkedCookieBytes(value.toByteArray(), false)) {}
21 init {
22 if (name.size > 4096L || value.size > 4096L - name.size)
23 throw IllegalArgumentException(Some("Cookie name and value exceed 4096 bytes"))
24 }
26 /**
27 * Serializes this cookie component using its validated byte syntax.
28 */
29 public fun toByteArray(): Array<Byte> {
30 val output = ByteArrayBuilder()
31 if (name.size > 0L) { output.append(name); output.append(61.toByte()) }
32 output.append(value)
33 return output.toByteArray()
34 }
36 /**
37 * Internal parsing context for validated cookie components.
38 */
39 public companion object {
40 internal fun read(reader: HttpFieldReader): HttpCookiePair {
41 val offset = reader.position
42 val part = readCookiePart(reader)
43 val name = if (part._3) part._1 else Array<Byte>(0L) { _ -> 0.toByte() }
44 val value = if (part._3) part._2 else part._1
45 try { return HttpCookiePair(name, value) }
46 catch (_: IllegalArgumentException) { reader.failAt(offset, "Cookie name and value exceed 4096 bytes") }
47 }
48 }
51/**
52 * One Set-Cookie attribute name and optional value, retaining byte-oriented syntax.
53 */
54public class HttpCookieAttribute private constructor(
55 /**
56 * The copied cookie name or attribute-name bytes.
57 */
58 public val name: Array<Byte>,
59 /**
60 * The copied cookie value or optional attribute-value bytes.
61 */
62 public val value: Array<Byte>?) {
63 public constructor(name: List<Byte>, value: List<Byte>? = None)
64 : this(checkedCookieBytes(name, true), checkedCookieOptional(value)) {}
65 public constructor(name: String, value: List<Byte>? = None)
66 : this(checkedCookieBytes(name.toByteArray(), true), checkedCookieOptional(value)) {}
67 public constructor(name: String, value: String)
68 : this(checkedCookieBytes(name.toByteArray(), true), Some<Array<Byte>>(checkedCookieBytes(value.toByteArray(), false))) {}
70 /**
71 * Compares the attribute name using ASCII case folding.
72 */
73 public fun hasName(name: String): Boolean = httpAsciiEqual(this.name, name.toByteArray())
74 /**
75 * Serializes this cookie component using its validated byte syntax.
76 */
77 public fun toByteArray(): Array<Byte> {
78 val output = ByteArrayBuilder()
79 output.append(name)
80 when (value) {
81 case None -> {}
82 case Some(val bytes) -> { output.append(61.toByte()); output.append(bytes) }
83 }
84 return output.toByteArray()
85 }
87 /**
88 * Internal parsing context for validated cookie components.
89 */
90 public companion object {
91 internal fun read(reader: HttpFieldReader): HttpCookieAttribute {
92 val part = readCookiePart(reader)
93 val value: Array<Byte>? = if (part._3) Some(part._2) else None
94 return HttpCookieAttribute(part._1, value)
95 }
96 }