Scoop API

scoop:scoop.http:0.1.0

src/message/vary.scoop

Selected production source · src/message/vary.scoop

1package scoop.http
3import scoop.bytes.ByteArrayBuilder
5/**
6 * The request-field names affecting representation selection, or a wildcard that prevents ordinary
7 * cache matching.
8 */
9public class HttpVary private constructor(
10 /**
11 * The normalized request-field names controlling selection.
12 */
13 public val names: Array<String>,
14 /**
15 * The original field-value byte sequences, preserving field order.
16 */
17 public val originalValues: Array<Array<Byte>>) {
18 public constructor(names: List<String>)
19 : this(Array<String>(names.size) { index -> httpNormalizedToken(names[index]) }, []) {}
21 /**
22 * Whether Vary contains the wildcard.
23 */
24 public val isWildcard: Boolean get() = contains("*")
25 /**
26 * Tests whether the named request field is covered by this Vary value.
27 */
28 public fun contains(name: String): Boolean {
29 val normalized = httpNormalizedToken(name)
30 for (value in names) if (value == normalized) return true
31 return false
32 }
33 /**
34 * Serializes the structured value into HTTP field-value bytes.
35 */
36 public fun toByteArray(): Array<Byte> {
37 val output = ByteArrayBuilder()
38 var first = true
39 for (name in names) {
40 if (!first) output.append(", ".toByteArray())
41 output.append(name.toByteArray())
42 first = false
43 }
44 return output.toByteArray()
45 }
46 /**
47 * Builds the corresponding named HTTP field from the structured value.
48 */
49 public fun toHttpField(): HttpField = HttpField("Vary".toByteArray(), toByteArray())
51 /**
52 * Construction and parsing of this structured HTTP field.
53 */
54 public companion object {
55 /**
56 * Parses the complete field value, throwing HttpFieldParseException for malformed syntax.
57 */
58 public fun parse(value: List<Byte>): HttpVary {
59 val reader = HttpFieldReader("Vary", value)
60 return HttpVary(reader.tokenList(), [reader.bytes])
61 }
62 /**
63 * Parses the complete field value, throwing HttpFieldParseException for malformed syntax.
64 */
65 public fun parse(value: String): HttpVary = parse(value.toByteArray())
66 /**
67 * Parses the complete value, returning None for HttpFieldParseException while propagating
68 * unrelated failures.
69 */
70 public fun parseOrNone(value: List<Byte>): HttpVary? {
71 try { return Some(parse(value)) }
72 catch (_: HttpFieldParseException) { return None }
73 }
74 /**
75 * Parses the complete value, returning None for HttpFieldParseException while propagating
76 * unrelated failures.
77 */
78 public fun parseOrNone(value: String): HttpVary? = parseOrNone(value.toByteArray())
80 /**
81 * Combines Vary fields, retaining original values and wildcard semantics.
82 */
83 public fun parseFields(fields: List<HttpField>): HttpVary {
84 val names = ArrayList<String>()
85 val originals = ArrayList<Array<Byte>>()
86 for (field in fields.findAll("vary".toByteArray())) {
87 val parsed = parse(field.value)
88 for (name in parsed.names) names.add(name)
89 originals.add(parsed.originalValues[0L])
90 }
91 return HttpVary(names.toArray(), originals.toArray())
92 }
93 }