Scoop API

scoop:scoop.http.server:0.1.0

src/request.scoop

Selected production source · src/request.scoop

1package scoop.http.server
3import scoop.http.*
4import scoop.text.*
5import scoop.url.*
7/** The immutable protocol request head with lazily parsed application input. */
8public class HttpRequest internal constructor(
9 /** The original protocol head; fields retain order, repetition and byte values. */
10 public val head: HttpRequestHead,
11 /** The single-consumer request body. */
12 public val body: HttpRequestBody,
13 internal val expectations: RequestExpectations = RequestExpectations(head.fields)
14) {
15 private var parsed: RequestTarget? = None
16 /** The exact, case-sensitive HTTP method token. */
17 public val method: String = inputText(head.method, "method")
18 /** The original encoded path, excluding query; special targets retain their own shape. */
19 public val path: String get() = target().path
20 /** Ordered query access using URI, rather than form, plus semantics. */
21 public val query: RequestQuery get() = RequestQuery(target().query)
22 /** Byte-preserving access to the original request fields. */
23 public val headers: RequestHeaders = RequestHeaders(head.fields)
24 /** Lazily parsed request cookies, unaffected by response mutations. */
25 public val cookies: RequestCookies = RequestCookies(head.fields)
26 /** The optional SSE resume identifier; duplicates and malformed UTF-8 are rejected. */
27 public val lastEventId: String? get() = headers.getSingle("Last-Event-ID")
29 internal fun target(): RequestTarget {
30 if (!present(parsed)) parsed = Some(requestTarget(head.target, method))
31 return parsed!!
32 }
35/** Ordered query fields with strict single-value conversion helpers. */
36public class RequestQuery internal constructor(
37 /** The parsed query, preserving duplicate keys and absent versus empty values. */
38 public val parameters: UrlQuery
39) {
40 /** Decodes a flat named record with an explicit codec and unknown-field policy. */
41 public fun <T> decode(codec: ValueDecoder<T>, options: QueryDecodeOptions = QueryDecodeOptions()): T {
42 val record = QueryRecord(groupQuery(parameters), options)
43 try { return codec.decode(QueryDecoder(QueryNode.Record(record), "")) }
44 catch (error: DecodingException) { throw queryRejection(error, record.location(error.path)) }
45 }
47 /** Decodes a flat named record using the type's companion codec. */
48 public fun <T> decode(options: QueryDecodeOptions = QueryDecodeOptions()): T
49 where T.Companion : ValueDecoder<T> = decode(T.Companion, options)
51 /** Returns every matching value in arrival order. */
52 public fun getAll(name: String): List<String?> {
53 try { return parameters.getAll(name) }
54 catch (_: UrlParseException) { reject(400U, "query field must be UTF-8: " + name) }
55 }
57 /** Returns None only when the key is absent; duplicate or valueless fields are rejected. */
58 public fun optionalText(name: String): String? {
59 val values = getAll(name)
60 if (values.size == 0L) return None
61 if (values.size != 1L) reject(400U, "duplicate query field: " + name)
62 if (values[0L] == None) reject(400U, "missing query value: " + name)
63 return values[0L]
64 }
66 /** Requires exactly one text value. */
67 public fun requiredText(name: String): String = optionalText(name) ?: reject(400U, "missing query field: " + name)
68 /** Reads one optional signed decimal integer, rejecting invalid present input. */
69 public fun optionalLong(name: String): Long? = when (optionalText(name)) {
70 case None -> None
71 case Some(val value) -> Some(value.toLongOrNone() ?: reject(400U, "invalid query integer: " + name))
72 }
73 /** Reads one required signed decimal integer. */
74 public fun requiredLong(name: String): Long = optionalLong(name) ?: reject(400U, "missing query field: " + name)