Scoop API

scoop:scoop.url:0.1.0

src/query.scoop

Selected production source · src/query.scoop

1package scoop.url
3import scoop.text.*
4import scoop.collections.emptyList
6/**
7 * An independent byte snapshot of one query name and optional value. No value and an empty value
8 * remain distinct.
9 */
10public class UrlQueryParameter public constructor(name: List<Byte>, value: List<Byte>?) {
11 /**
12 * The copied raw bytes of the parameter name.
13 */
14 public val nameBytes: Array<Byte> = name.toArray()
15 /**
16 * The copied raw value bytes, or None for a parameter with no equals/value component.
17 */
18 public val valueBytes: Array<Byte>? = when (value) { case None -> None; case Some(val bytes) -> Some(bytes.toArray()) }
19 /**
20 * Decodes the parameter name as strict UTF-8.
21 */
22 public val name: String get() = urlUtf8(nameBytes, UrlComponent.Query)
23 /**
24 * Decodes a present value as strict UTF-8, retaining None for an absent value.
25 */
26 public val value: String? get() = when (valueBytes) { case None -> None; case Some(val bytes) -> Some(urlUtf8(bytes, UrlComponent.Query)) }
29/**
30 * An immutable ordered query parameter collection preserving duplicates, absent values, and empty values.
31 * Text access requires valid UTF-8.
32 */
33public class UrlQuery public constructor(entries: List<UrlQueryParameter> = emptyList<UrlQueryParameter>()) : ToString {
34 /**
35 * An independent ordered snapshot of the query entries.
36 */
37 public val entries: Array<UrlQueryParameter> = entries.toArray()
39 /**
40 * Returns a new query with a UTF-8 name and optional value appended, preserving earlier duplicates.
41 */
42 public fun add(name: String, value: String? = None): UrlQuery =
43 addBytes(name.toByteArray(), if (value == None) None else Some<List<Byte>>(value!!.toByteArray()))
45 /**
46 * Returns a new query with arbitrary name/value bytes appended, without requiring UTF-8.
47 */
48 public fun addBytes(name: List<Byte>, value: List<Byte>? = None): UrlQuery {
49 val next = ArrayList<UrlQueryParameter>()
50 for (entry in entries) next.add(entry)
51 next.add(UrlQueryParameter(name, value))
52 return UrlQuery(next)
53 }
55 /**
56 * Returns a new query without matching UTF-8 names, preserving other entries' order.
57 */
58 public fun remove(name: String): UrlQuery {
59 val bytes = name.toByteArray()
60 val next = ArrayList<UrlQueryParameter>()
61 for (entry in entries) { if (!urlBytesEqual(entry.nameBytes, bytes)) next.add(entry) }
62 return UrlQuery(next)
63 }
65 /**
66 * Returns a new query with matching names removed and the replacement appended at the end.
67 */
68 public fun set(name: String, value: String? = None): UrlQuery = remove(name).add(name, value)
70 /**
71 * Returns all matching values in order, preserving absent and empty values.
72 */
73 public fun getAll(name: String): List<String?> {
74 val bytes = name.toByteArray()
75 val result = ArrayList<String?>()
76 for (entry in entries) { if (urlBytesEqual(entry.nameBytes, bytes)) result.add(entry.value) }
77 return result.toArray()
78 }
80 /**
81 * Serializes with ordinary URI query escaping, where plus is not a space abbreviation.
82 */
83 public override fun toString(): String = render(false)
85 internal fun render(form: Boolean): String {
86 val output = StringBuilder()
87 var first = true
88 for (entry in entries) {
89 if (!first) output.append("&")
90 first = false
91 output.append(encodeUrlBytes(entry.nameBytes, form, ""))
92 if (hasUrlValue(entry.valueBytes)) output.append("=").append(encodeUrlBytes(entry.valueBytes!!, form, ""))
93 }
94 return output.toString()
95 }
97 /**
98 * Construction from an encoded query component.
99 */
100 public companion object {
101 /**
102 * Parses an encoded query while preserving duplicate keys, ordering, absent values, and
103 * empty values.
104 */
105 public fun parse(encodedQuery: String): UrlQuery = parseQuery(encodedQuery, false)
106 }
109/**
110 * The application/x-www-form-urlencoded codec, with plus representing a space.
111 */
112public object FormUrlEncoded {
113 /**
114 * Encodes parameters using form escaping and plus for spaces.
115 */
116 public fun encode(parameters: UrlQuery): String = parameters.render(true)
117 /**
118 * Parses form data, decoding plus to space and retaining ordered duplicate entries.
119 */
120 public fun parse(text: String): UrlQuery = parseQuery(text, true)
123private fun parseQuery(text: String, form: Boolean): UrlQuery {
124 val items = ArrayList<UrlQueryParameter>()
125 if (text.charLength == 0L) return UrlQuery(items)
126 for (part in text.split('&')) {
127 val equal = part.indexOf('=')
128 val name = if (equal < 0L) part else part.slice(0L, equal)
129 val value: List<Byte>? = if (equal < 0L) None else Some<List<Byte>>(decodeUrlBytes(part.slice(equal + 1L, part.charLength), form))
130 items.add(UrlQueryParameter(decodeUrlBytes(name, form), value))
131 }
132 return UrlQuery(items)