Scoop API

scoop:scoop.http:0.1.0

src/message/media_type.scoop

Selected production source · src/message/media_type.scoop

1package scoop.http
3import scoop.collections.emptyList
4import scoop.text.lowercaseAscii
6/**
7 * A normalized media type/subtype with unique named parameters and optional original field bytes.
8 */
9public class HttpMediaType private constructor(
10 /**
11 * The normalized top-level media type token.
12 */
13 public val type: String,
14 /**
15 * The normalized media subtype token.
16 */
17 public val subtype: String,
18 /**
19 * The parsed named parameters in their stored order.
20 */
21 public val parameters: Array<HttpParameter>,
22 /**
23 * The original parsed field-value bytes, or None for a programmatically constructed value.
24 */
25 public val originalValue: Array<Byte>?
26) {
27 public constructor(type: String, subtype: String, parameters: List<HttpParameter> = emptyList<HttpParameter>())
28 : this(httpNormalizedToken(type), httpNormalizedToken(subtype), uniqueHttpParameters(parameters), None) {}
30 /**
31 * Looks up a named parameter using ASCII case val folding, returning None when absent.
32 */
33 public fun parameter(name: String): HttpParameter? = findHttpParameter(parameters, name)
34 /**
35 * Serializes the structured value into HTTP field-value bytes.
36 */
37 public fun toByteArray(): Array<Byte> = encodeHttpParameters(type + "/" + subtype, parameters)
38 /**
39 * Builds the corresponding named HTTP field from the structured value.
40 */
41 public fun toHttpField(): HttpField = HttpField("Content-Type".toByteArray(), toByteArray())
43 /**
44 * Construction and parsing of this structured HTTP field.
45 */
46 public companion object {
47 /**
48 * Reads the Content-Type field, returning None when absent and rejecting duplicates.
49 */
50 public fun parseFields(fields: List<HttpField>): HttpMediaType? {
51 val values = fields.findAll("Content-Type".toByteArray())
52 if (values.size == 0L) return None
53 if (values.size > 1L) throw HttpFieldParseException("Content-Type", 0L, "Content-Type must occur once")
54 return Some(parse(values[0L].value))
55 }
57 /**
58 * Parses the complete field value, throwing HttpFieldParseException for malformed syntax.
59 */
60 public fun parse(value: List<Byte>): HttpMediaType {
61 val reader = HttpFieldReader("Content-Type", value)
62 reader.skipOws()
63 val type = reader.token().lowercaseAscii()
64 reader.expect(47)
65 val subtype = reader.token().lowercaseAscii()
66 val parameters = reader.parameters(allowEmpty = true)
67 return HttpMediaType(type, subtype, parameters, Some(reader.bytes))
68 }
69 /**
70 * Parses the complete field value, throwing HttpFieldParseException for malformed syntax.
71 */
72 public fun parse(value: String): HttpMediaType = parse(value.toByteArray())
73 /**
74 * Parses the complete value, returning None for HttpFieldParseException while propagating
75 * unrelated failures.
76 */
77 public fun parseOrNone(value: List<Byte>): HttpMediaType? {
78 try { return Some(parse(value)) }
79 catch (_: HttpFieldParseException) { return None }
80 }
81 /**
82 * Parses the complete value, returning None for HttpFieldParseException while propagating
83 * unrelated failures.
84 */
85 public fun parseOrNone(value: String): HttpMediaType? = parseOrNone(value.toByteArray())
86 }