Scoop API

scoop:scoop.http.server:0.1.0

src/route_pattern.scoop

Selected production source · src/route_pattern.scoop

1package scoop.http.server
3import scoop.text.*
4import scoop.url.*
6internal enum RoutePart { Literal(Array<Byte>), Parameter, Tail }
7internal class RoutePattern(val path: String, val parts: Array<RoutePart>, val names: Array<String>)
9internal fun routePattern(path: String, group: Boolean = false): RoutePattern {
10 if (!path.startsWith("/") || path.indexOf('?') >= 0L || path.indexOf('#') >= 0L)
11 throw IllegalArgumentException(Some("route must be an absolute path without query or fragment"))
12 val parts = ArrayList<RoutePart>()
13 val names = ArrayList<String>()
14 val segments = path.slice(1L, path.charLength).split('/')
15 var index = 0L
16 for (segment in segments) {
17 if (segment.startsWith("{")) {
18 if (!segment.endsWith("}")) throw IllegalArgumentException(Some("unterminated route parameter"))
19 val tail = segment.startsWith("{*")
20 val name = segment.slice(if (tail) 2L else 1L, segment.charLength - 1L)
21 if (name.charLength == 0L || name.indexOf('{') >= 0L || name.indexOf('}') >= 0L || name.indexOf('*') >= 0L)
22 throw IllegalArgumentException(Some("invalid route parameter name"))
23 for (existing in names) if (existing == name) throw IllegalArgumentException(Some("duplicate route parameter name"))
24 if (tail && (group || index != segments.size - 1L)) throw IllegalArgumentException(Some("wildcard must end an endpoint path"))
25 names.add(name)
26 parts.add(if (tail) RoutePart.Tail else RoutePart.Parameter)
27 } else {
28 if (segment.indexOf('{') >= 0L || segment.indexOf('}') >= 0L)
29 throw IllegalArgumentException(Some("route parameters occupy a whole segment"))
30 try { parts.add(RoutePart.Literal(PercentEncoding.decode(segment))) }
31 catch (_: UrlParseException) { throw IllegalArgumentException(Some("invalid route percent encoding")) }
32 }
33 index += 1L
34 }
35 return RoutePattern(path, parts.toArray(), names.toArray())
38internal fun joinedRoute(prefix: String, path: String): String {
39 if (!path.startsWith("/")) throw IllegalArgumentException(Some("route suffix must start with slash"))
40 return prefix + path
43/** The stable metadata of a selected route resource. */
44public class HttpRouteInfo internal constructor(
45 /** The original configured path template. */
46 public val template: String,
47 /** The configured method selected for the endpoint, including GET for implicit HEAD. */
48 public val method: String