Scoop API

scoop:scoop.http:0.1.0

src/message/age.scoop

Selected production source · src/message/age.scoop

1package scoop.http
3/**
4 * A nonnegative Age field value in seconds, retaining original input when parsed.
5 */
6public class HttpAge private constructor(
7 /**
8 * The age value in whole seconds.
9 */
10 public val seconds: Long,
11 /**
12 * The original parsed field-value bytes, or None for a programmatically constructed value.
13 */
14 public val originalValue: Array<Byte>?) : ToString {
15 public constructor(seconds: Long) : this(seconds, None) {
16 if (seconds < 0L) throw IllegalArgumentException(Some("negative HTTP Age"))
17 }
19 /**
20 * Formats the structured value as HTTP field text.
21 */
22 public override fun toString(): String = seconds.toString()
23 /**
24 * Serializes the structured value into HTTP field-value bytes.
25 */
26 public fun toByteArray(): Array<Byte> = toString().toByteArray()
27 /**
28 * Builds the corresponding named HTTP field from the structured value.
29 */
30 public fun toHttpField(): HttpField = HttpField("Age".toByteArray(), toByteArray())
32 /**
33 * Construction and parsing of this structured HTTP field.
34 */
35 public companion object {
36 /**
37 * Parses the first Age value, tolerating a following comma-separated suffix and saturating
38 * decimal overflow; malformed leading syntax throws HttpFieldParseException.
39 */
40 public fun parse(value: List<Byte>): HttpAge {
41 val reader = HttpFieldReader("Age", value)
42 reader.skipOws()
43 val seconds = reader.decimal(saturating = true)
44 reader.skipOws()
45 if (!reader.ended && reader.peek() != 44) reader.fail("invalid Age seconds")
46 return HttpAge(seconds, Some(reader.bytes))
47 }
48 /**
49 * Parses the first Age value, tolerating a following comma-separated suffix and saturating
50 * decimal overflow; malformed leading syntax throws HttpFieldParseException.
51 */
52 public fun parse(value: String): HttpAge = parse(value.toByteArray())
53 /**
54 * Parses the complete value, returning None for HttpFieldParseException while propagating
55 * unrelated failures.
56 */
57 public fun parseOrNone(value: List<Byte>): HttpAge? {
58 try { return Some(parse(value)) }
59 catch (_: HttpFieldParseException) { return None }
60 }
61 /**
62 * Parses the complete value, returning None for HttpFieldParseException while propagating
63 * unrelated failures.
64 */
65 public fun parseOrNone(value: String): HttpAge? = parseOrNone(value.toByteArray())
67 /**
68 * Reads the first Age field, returning None when absent; later Age fields are ignored.
69 */
70 public fun parseFields(fields: List<HttpField>): HttpAge? {
71 val selected = fields.findAll("age".toByteArray())
72 return if (selected.size == 0L) None else Some(parse(selected[0L].value))
73 }
74 /**
75 * Returns a parsed Age when present and valid, or None for absent/invalid input.
76 */
77 public fun parseFieldsOrNone(fields: List<HttpField>): HttpAge? {
78 try { return parseFields(fields) }
79 catch (_: HttpFieldParseException) { return None }
80 }
81 }