scoop:scoop.http.server:0.1.0
src/query/input.scoop
Selected production source · src/query/input.scoop
1
package scoop.http.server3
import scoop.collections.LinkedHashMap4
import scoop.url.UrlQuery6
/** Controls fields that the query record codec does not access. */7
public enum QueryUnknownFields {8
/** Reject unaccessed fields when the keyed container ends. */9
Reject,10
/** Permit the codec to ignore additional fields. */11
Ignore12
}14
/** Options for decoding a flat query record. */15
public class QueryDecodeOptions public constructor(16
/** Unknown fields are rejected by default. */17
public val unknownFields: QueryUnknownFields = QueryUnknownFields.Reject18
)20
/** Reports a codec shape that cannot be represented by a flat query record. */21
public class QueryCodecException public constructor(22
/** The JSON Pointer path at which the unsupported shape was requested. */23
public val path: String, message: String24
) : Exception(Some(message))26
internal class QueryItem(private val bytes: Array<Byte>?, val position: Long) {27
private var decoded: String? = None28
fun text(path: String): String {29
if (!present(bytes)) throw DecodingException(path, "missing query value")30
if (!present(decoded)) decoded = Some(String.fromUtf8OrNone(bytes!!)31
?: throw DecodingException(path, "query value must be UTF-8"))32
return decoded!!33
}34
}36
internal class QueryField(val path: String) {37
val items: ArrayList<QueryItem> = ArrayList<QueryItem>()38
var accessed: Boolean = false39
}41
internal fun groupQuery(parameters: UrlQuery): LinkedHashMap<String, QueryField> {42
val fields = LinkedHashMap<String, QueryField>()43
for (position in 0L until parameters.entries.size) {44
val entry = parameters.entries[position]45
val name = String.fromUtf8OrNone(entry.nameBytes) ?: throw queryRejection(46
DecodingException("", "query name must be UTF-8"), "query item " + position.toString())47
val existing = fields[name]48
val field = if (present(existing)) existing!! else QueryField(queryPath(name))49
if (!present(existing)) fields.put(name, field)50
field.items.add(QueryItem(entry.valueBytes, position))51
}52
return fields53
}55
internal fun queryPath(name: String): String {56
val result = StringBuilder().append("/")57
for (character in name) {58
when (character) {59
case '~' -> result.append("~0")60
case '/' -> result.append("~1")61
else -> result.append(character)62
}63
}64
return result.toString()65
}67
internal fun queryRejection(error: DecodingException, location: String): HttpRejection {68
val source = if (location.charLength == 0L) "" else " (" + location + ")"69
return HttpRejection(text("invalid query " + error.path + source + ": " +70
(error.message ?: "decoding failed"), 400U), Some<Throwable>(error))71
}