Scoop API

scoop:scoop.http:0.1.0

src/message/entity_tag_condition.scoop

Selected production source · src/message/entity_tag_condition.scoop

1package scoop.http
3import scoop.bytes.ByteArrayBuilder
5/**
6 * A conditional entity-tag list or wildcard for If-Match and If-None-Match.
7 */
8public class HttpEntityTagCondition private constructor(
9 /**
10 * Whether this condition matches any current representation.
11 */
12 public val isWildcard: Boolean,
13 /**
14 * The explicitly listed tags when the condition is not a wildcard.
15 */
16 public val tags: Array<HttpEntityTag>,
17 /**
18 * The original parsed field-value bytes, or None for a programmatically constructed value.
19 */
20 public val originalValue: Array<Byte>?
21) {
22 public constructor(tags: List<HttpEntityTag>) : this(false, tags.toArray(), None) {}
24 /**
25 * Serializes the structured value into HTTP field-value bytes.
26 */
27 public fun toByteArray(): Array<Byte> {
28 if (isWildcard) return "*".toByteArray()
29 val output = ByteArrayBuilder()
30 var first = true
31 for (tag in tags) {
32 if (!first) output.append(", ".toByteArray())
33 output.append(tag.toByteArray())
34 first = false
35 }
36 return output.toByteArray()
37 }
38 /**
39 * Builds an If-Match request field.
40 */
41 public fun toIfMatchField(): HttpField = HttpField("If-Match".toByteArray(), toByteArray())
42 /**
43 * Builds an If-None-Match request field.
44 */
45 public fun toIfNoneMatchField(): HttpField = HttpField("If-None-Match".toByteArray(), toByteArray())
47 /**
48 * Construction and parsing of this structured HTTP field.
49 */
50 public companion object {
51 /**
52 * Constructs the wildcard condition.
53 */
54 public fun wildcard(): HttpEntityTagCondition = HttpEntityTagCondition(true, [], None)
56 /**
57 * Parses the complete field value, throwing HttpFieldParseException for malformed syntax.
58 */
59 public fun parse(value: List<Byte>, fieldName: String = "If-None-Match"): HttpEntityTagCondition {
60 val reader = HttpFieldReader(fieldName, value)
61 reader.skipOws()
62 if (reader.peek() == 42) {
63 reader.position += 1L
64 reader.skipOws()
65 if (!reader.ended) reader.fail("wildcard cannot be combined with entity tags")
66 return HttpEntityTagCondition(true, [], Some(reader.bytes))
67 }
68 val tags = ArrayList<HttpEntityTag>()
69 while (!reader.ended) {
70 if (reader.peek() == 44) { reader.position += 1L; reader.skipOws(); continue }
71 tags.add(HttpEntityTag.read(reader))
72 reader.skipOws()
73 if (!reader.ended) { reader.expect(44); reader.skipOws() }
74 }
75 return HttpEntityTagCondition(false, tags.toArray(), Some(reader.bytes))
76 }
77 /**
78 * Parses the complete field value, throwing HttpFieldParseException for malformed syntax.
79 */
80 public fun parse(value: String, fieldName: String = "If-None-Match"): HttpEntityTagCondition =
81 parse(value.toByteArray(), fieldName)
82 /**
83 * Parses the complete value, returning None for HttpFieldParseException while propagating
84 * unrelated failures.
85 */
86 public fun parseOrNone(value: List<Byte>, fieldName: String = "If-None-Match"): HttpEntityTagCondition? {
87 try { return Some(parse(value, fieldName)) }
88 catch (_: HttpFieldParseException) { return None }
89 }
90 /**
91 * Parses the complete value, returning None for HttpFieldParseException while propagating
92 * unrelated failures.
93 */
94 public fun parseOrNone(value: String, fieldName: String = "If-None-Match"): HttpEntityTagCondition? =
95 parseOrNone(value.toByteArray(), fieldName)
96 }