Scoop API

scoop:scoop.http.server:0.1.0

src/path.scoop

Selected production source · src/path.scoop

1package scoop.http.server
3import scoop.bytes.ByteArrayBuilder
4import scoop.http.HttpRequestTarget
5import scoop.text.*
6import scoop.url.*
8internal class PathSegment(
9 val encoded: String,
10 val decoded: Array<Byte>,
11 val tail: Array<PathSegment>? = None
14internal enum TargetKind {
15 Path, Star, Connect
18internal class RequestTarget(
19 val kind: TargetKind,
20 val path: String,
21 val segments: Array<PathSegment>,
22 val query: UrlQuery
25internal fun requestTarget(target: HttpRequestTarget, method: String): RequestTarget {
26 if (method == "CONNECT")
27 return RequestTarget(TargetKind.Connect, "", [], UrlQuery())
28 val encoded =
29 when (target) {
30 case HttpRequestTarget.Http1(val value) -> inputText(value, "request target")
31 case HttpRequestTarget.Http2(_, _, val value) -> inputText(value, "request path")
32 case HttpRequestTarget.Connect(_) -> return RequestTarget(
33 TargetKind.Connect,
34 "",
35 [],
36 UrlQuery()
37 )
38 }
39 if (encoded == "*")
40 return RequestTarget(TargetKind.Star, "*", [], UrlQuery())
41 if (!encoded.startsWith('/') || encoded.indexOf('#') >= 0L)
42 reject(400U, "expected an origin-form request target")
43 val queryAt = encoded.indexOf('?')
44 val path =
45 if (queryAt < 0L)
46 encoded
47 else
48 encoded.slice(0L, queryAt)
49 try {
50 val query =
51 if (queryAt < 0L)
52 UrlQuery()
53 else
54 UrlQuery.parse(encoded.slice(queryAt + 1L, encoded.charLength))
55 return RequestTarget(TargetKind.Path, path, decodePath(path), query)
56 } catch (_: UrlParseException) {
57 reject(400U, "invalid request target encoding")
58 }
61internal fun decodePath(path: String): Array<PathSegment> {
62 val parts = path.slice(1L, path.charLength).split('/')
63 return Array<PathSegment>(parts.size) {
64 index ->
65 val part = parts[index]
66 PathSegment(part, PercentEncoding.decode(part))
67 }
70/** Captured route parameters with explicit raw-byte and strict UTF-8 access. */
71public class PathParameters internal constructor(
72 private val names: Array<String>,
73 private val values: Array<PathSegment>
74) {
75 private fun find(name: String): PathSegment {
76 var index = 0L
77 while (index < names.size) {
78 if (names[index] == name)
79 return values[index]
80 index += 1L
81 }
82 throw IllegalStateException(Some("route has no parameter named " + name))
83 }
85 /** Returns the original percent-encoded parameter bytes, preserving encoded separators. */
86 public fun rawBytes(name: String): Array<Byte> = find(name).encoded.toByteArray()
87 /** Returns a snapshot of the once-decoded parameter bytes. */
88 public fun bytes(name: String): Array<Byte> = find(name).decoded
89 /** Decodes one captured parameter as strict UTF-8. */
90 public fun text(name: String): String = inputText(bytes(name), "path parameter " + name)
91 /** Reads a required signed decimal integer or rejects the request with 400. */
92 public fun long(name: String): Long =
93 text(name).toLongOrNone() ?: reject(400U, "invalid path integer " + name)
95 internal fun tailSegments(name: String): Array<PathSegment> =
96 find(name).tail ?:
97 throw IllegalStateException(Some("static files require a terminal wildcard parameter"))
100internal fun pathTail(parts: Array<PathSegment>, start: Long): PathSegment {
101 val encoded = StringBuilder()
102 val decoded = ByteArrayBuilder()
103 var index = start
104 while (index < parts.size) {
105 if (index != start) {
106 encoded.append("/");
107 decoded.append(47.toByte())
108 }
109 encoded.append(parts[index].encoded)
110 decoded.append(parts[index].decoded)
111 index += 1L
112 }
113 return PathSegment(
114 encoded.toString(),
115 decoded.toByteArray(),
116 Some(Array<PathSegment>(parts.size - start) { offset -> parts[start + offset] })
117 )