Scoop API

scoop:scoop.http:0.1.0

src/message/cache_control.scoop

Selected production source · src/message/cache_control.scoop

1package scoop.http
3import scoop.bytes.ByteArrayBuilder
4import scoop.text.lowercaseAscii
6/**
7 * Ordered Cache-Control directives, including duplicates and original field values. Interpretation
8 * is explicit; single rejects duplicated directives.
9 */
10public class HttpCacheControl private constructor(
11 /**
12 * All directives in field order, retaining duplicates.
13 */
14 public val directives: Array<HttpCacheDirective>,
15 /**
16 * The original field-value byte sequences, preserving field order.
17 */
18 public val originalValues: Array<Array<Byte>>
19) {
20 public constructor(directives: List<HttpCacheDirective>) : this(directives.toArray(), []) {}
22 /**
23 * Returns every directive with the normalized name in original order.
24 */
25 public fun findAll(name: String): Array<HttpCacheDirective> {
26 val normalized = httpNormalizedToken(name)
27 val result = ArrayList<HttpCacheDirective>()
28 for (directive in directives) if (directive.name == normalized) result.add(directive)
29 return result.toArray()
30 }
31 /**
32 * Tests whether a named directive exists.
33 */
34 public fun contains(name: String): Boolean {
35 val normalized = httpNormalizedToken(name)
36 for (directive in directives) if (directive.name == normalized) return true
37 return false
38 }
39 /**
40 * Returns the sole named directive or None; duplicates throw HttpFieldParseException.
41 */
42 public fun single(name: String): HttpCacheDirective? {
43 val found = findAll(name)
44 if (found.size > 1L) throw HttpFieldParseException("Cache-Control", found[1L].nameOffset, "duplicate cache directive " + name)
45 return if (found.size == 0L) None else Some(found[0L])
46 }
48 /**
49 * Serializes the structured value into HTTP field-value bytes.
50 */
51 public fun toByteArray(): Array<Byte> {
52 val output = ByteArrayBuilder()
53 var first = true
54 for (directive in directives) {
55 if (!first) output.append(", ".toByteArray())
56 appendCacheDirective(output, directive)
57 first = false
58 }
59 return output.toByteArray()
60 }
61 /**
62 * Builds the corresponding named HTTP field from the structured value.
63 */
64 public fun toHttpField(): HttpField = HttpField("Cache-Control".toByteArray(), toByteArray())
66 /**
67 * Construction and parsing of this structured HTTP field.
68 */
69 public companion object {
70 /**
71 * Parses the complete field value, throwing HttpFieldParseException for malformed syntax.
72 */
73 public fun parse(value: List<Byte>): HttpCacheControl {
74 val reader = HttpFieldReader("Cache-Control", value)
75 val result = ArrayList<HttpCacheDirective>()
76 reader.skipOws()
77 while (!reader.ended) {
78 if (reader.peek() == 44) { reader.position += 1L; reader.skipOws(); continue }
79 val start = reader.position
80 val name = reader.token().lowercaseAscii()
81 var valueOffset = reader.position
82 var value: Array<Byte>? = None
83 var quoted = false
84 if (reader.peek() == 61) {
85 reader.position += 1L
86 valueOffset = reader.position
87 val parsed = reader.parameterValue()
88 value = Some(parsed._1)
89 quoted = parsed._2
90 }
91 result.add(HttpCacheDirective(name, value, quoted, start, valueOffset, Some(reader.bytes)))
92 reader.skipOws()
93 if (!reader.ended) { reader.expect(44); reader.skipOws() }
94 }
95 return HttpCacheControl(result.toArray(), [reader.bytes])
96 }
97 /**
98 * Parses the complete field value, throwing HttpFieldParseException for malformed syntax.
99 */
100 public fun parse(value: String): HttpCacheControl = parse(value.toByteArray())
101 /**
102 * Parses the complete value, returning None for HttpFieldParseException while propagating
103 * unrelated failures.
104 */
105 public fun parseOrNone(value: List<Byte>): HttpCacheControl? {
106 try { return Some(parse(value)) }
107 catch (_: HttpFieldParseException) { return None }
108 }
109 /**
110 * Parses the complete value, returning None for HttpFieldParseException while propagating
111 * unrelated failures.
112 */
113 public fun parseOrNone(value: String): HttpCacheControl? = parseOrNone(value.toByteArray())
115 /**
116 * Combines all Cache-Control fields in order, retaining their original values.
117 */
118 public fun parseFields(fields: List<HttpField>): HttpCacheControl {
119 val directives = ArrayList<HttpCacheDirective>()
120 val originals = ArrayList<Array<Byte>>()
121 for (field in fields.findAll("cache-control".toByteArray())) {
122 val parsed = parse(field.value)
123 for (directive in parsed.directives) directives.add(directive)
124 originals.add(parsed.originalValues[0L])
125 }
126 return HttpCacheControl(directives.toArray(), originals.toArray())
127 }
128 }
131private fun cacheSecondsDirective(name: String): Boolean =
132 name == "max-age" || name == "s-maxage" || name == "min-fresh" || name == "max-stale" ||
133 name == "stale-while-revalidate" || name == "stale-if-error"
135private fun cacheFlagDirective(name: String): Boolean =
136 name == "no-store" || name == "no-transform" || name == "only-if-cached" || name == "must-revalidate" ||
137 name == "must-understand" || name == "proxy-revalidate" || name == "public" || name == "immutable"
139private fun appendCacheDirective(output: ByteArrayBuilder, directive: HttpCacheDirective) {
140 output.append(directive.name.toByteArray())
141 when (directive.value) {
142 case None -> if (cacheSecondsDirective(directive.name) && directive.name != "max-stale") directive.deltaSeconds()
143 case Some(val bytes) -> {
144 if (cacheFlagDirective(directive.name)) directive.failAt(0L, "cache directive does not allow an argument")
145 output.append(61.toByte())
146 if (cacheSecondsDirective(directive.name)) {
147 directive.deltaSeconds()
148 output.append(bytes)
149 } else {
150 val fields = directive.name == "no-cache" || directive.name == "private"
151 if (fields) directive.fieldNames()
152 appendHttpParameterValue(output, bytes, fields || directive.wasQuoted)
153 }
154 }
155 }