Scoop API

scoop:scoop.http:0.1.0

src/message/cache_directive.scoop

Selected production source · src/message/cache_directive.scoop

1package scoop.http
3/**
4 * A normalized directive name and optional raw argument. Numeric and field-list interpretation
5 * validate their own grammar.
6 */
7public class HttpCacheDirective internal constructor(
8 /**
9 * The ASCII-lowercase directive token.
10 */
11 public val name: String,
12 /**
13 * The optional argument bytes after quoted-string decoding.
14 */
15 public val value: Array<Byte>?,
16 /**
17 * Whether the parsed argument used quoted-string syntax.
18 */
19 public val wasQuoted: Boolean,
20 internal val nameOffset: Long, private val valueOffset: Long, private val sourceValue: Array<Byte>?
21) {
22 public constructor(name: String, value: List<Byte>? = None) : this(httpNormalizedToken(name),
23 when (value) { case Some(val bytes) -> Some(checkedHttpParameter(bytes)); case None -> None }, false, 0L, 0L, None) {}
24 public constructor(name: String, value: List<Byte>) : this(name, Some<List<Byte>>(value)) {}
26 /**
27 * Parses the required nonnegative decimal argument, saturating numeric overflow.
28 */
29 public fun deltaSeconds(): Long {
30 val reader = HttpFieldReader("Cache-Control", requiredValue())
31 try {
32 val result = reader.decimal(saturating = true)
33 if (!reader.ended) reader.fail("invalid delta-seconds")
34 return result
35 } catch (error: HttpFieldParseException) { failAt(error.byteOffset, error.detail) }
36 }
38 /**
39 * Parses the required argument as a list of HTTP field-name tokens.
40 */
41 public fun fieldNames(): Array<String> {
42 val bytes = requiredValue()
43 try { return HttpFieldReader("Cache-Control", bytes).tokenList() }
44 catch (error: HttpFieldParseException) { failAt(error.byteOffset, error.detail) }
45 }
47 private fun requiredValue(): Array<Byte> = when (value) {
48 case Some(val bytes) -> bytes
49 case None -> failAt(0L, "cache directive requires an argument")
50 }
52 internal fun failAt(offset: Long, detail: String): Nothing {
53 var sourceOffset = valueOffset + offset
54 when (sourceValue) {
55 case Some(val source) -> if (wasQuoted) {
56 sourceOffset = valueOffset + 1L
57 var decoded = 0L
58 while (decoded < offset) {
59 if (source[sourceOffset] == 92.toByte()) sourceOffset += 1L
60 sourceOffset += 1L
61 decoded += 1L
62 }
63 if (source[sourceOffset] == 92.toByte()) sourceOffset += 1L
64 }
65 case None -> {}
66 }
67 throw HttpFieldParseException("Cache-Control", sourceOffset, name + ": " + detail)
68 }