-
Notifications
You must be signed in to change notification settings - Fork 1k
/
RoutingBuilder.kt
395 lines (355 loc) · 12.3 KB
/
RoutingBuilder.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/
@file:Suppress("unused")
package io.ktor.server.routing
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.request.*
import io.ktor.util.*
import io.ktor.util.pipeline.*
import kotlin.jvm.*
/**
* Builds a route to match the specified [path].
* @see [Application.routing]
*/
@KtorDsl
public fun Route.route(path: String, build: Route.() -> Unit): Route = createRouteFromPath(path).apply(build)
/**
* Builds a route to match the specified HTTP [method] and [path].
* @see [Application.routing]
*/
@KtorDsl
public fun Route.route(path: String, method: HttpMethod, build: Route.() -> Unit): Route {
val selector = HttpMethodRouteSelector(method)
return createRouteFromPath(path).createChild(selector).apply(build)
}
/**
* Builds a route to match the specified HTTP [method].
* @see [Application.routing]
*/
@KtorDsl
public fun Route.method(method: HttpMethod, body: Route.() -> Unit): Route {
val selector = HttpMethodRouteSelector(method)
return createChild(selector).apply(body)
}
/**
* Builds a route to match a parameter with the specified [name] and [value].
* @see [Application.routing]
*/
@KtorDsl
public fun Route.param(name: String, value: String, build: Route.() -> Unit): Route {
val selector = ConstantParameterRouteSelector(name, value)
return createChild(selector).apply(build)
}
/**
* Builds a route to match a parameter with the specified [name] and captures its value.
* @see [Application.routing]
*/
@KtorDsl
public fun Route.param(name: String, build: Route.() -> Unit): Route {
val selector = ParameterRouteSelector(name)
return createChild(selector).apply(build)
}
/**
* Builds a route to capture an optional parameter with specified [name], if it exists.
* @see [Application.routing]
*/
@KtorDsl
public fun Route.optionalParam(name: String, build: Route.() -> Unit): Route {
val selector = OptionalParameterRouteSelector(name)
return createChild(selector).apply(build)
}
/**
* Builds a route to match a header with the specified [name] and [value].
* @see [Application.routing]
*/
@KtorDsl
public fun Route.header(name: String, value: String, build: Route.() -> Unit): Route {
val selector = HttpHeaderRouteSelector(name, value)
return createChild(selector).apply(build)
}
/**
* Builds a route to match requests with the [HttpHeaders.Accept] header matching the specified [contentType].
* @see [Application.routing]
*/
@KtorDsl
public fun Route.accept(contentType: ContentType, build: Route.() -> Unit): Route {
val selector = HttpAcceptRouteSelector(contentType)
return createChild(selector).apply(build)
}
/**
* Builds a route to match requests with the [HttpHeaders.ContentType] header matching the specified [contentType].
* @see [Application.routing]
*/
@KtorDsl
public fun Route.contentType(contentType: ContentType, build: Route.() -> Unit): Route {
val selector = ContentTypeHeaderRouteSelector(contentType)
return createChild(selector).apply(build)
}
/**
* Builds a route to match `GET` requests with the specified [path].
* @see [Application.routing]
*/
@KtorDsl
public fun Route.get(path: String, body: PipelineInterceptor<Unit, ApplicationCall>): Route {
return route(path, HttpMethod.Get) { handle(body) }
}
/**
* Builds a route to match `GET` requests.
* @see [Application.routing]
*/
@KtorDsl
public fun Route.get(body: PipelineInterceptor<Unit, ApplicationCall>): Route {
return method(HttpMethod.Get) { handle(body) }
}
/**
* Builds a route to match `POST` requests with the specified [path].
* @see [Application.routing]
*/
@KtorDsl
public fun Route.post(path: String, body: PipelineInterceptor<Unit, ApplicationCall>): Route {
return route(path, HttpMethod.Post) { handle(body) }
}
/**
* Builds a route to match `POST` requests receiving a request body as content of the [R] type.
* @see [Application.routing]
*/
@KtorDsl
@JvmName("postTyped")
public inline fun <reified R : Any> Route.post(
crossinline body: suspend PipelineContext<Unit, ApplicationCall>.(R) -> Unit
): Route = post {
body(call.receive())
}
/**
* Builds a route to match `POST` requests with the specified [path] receiving a request body as content of the [R] type.
* @see [Application.routing]
*/
@KtorDsl
@JvmName("postTypedPath")
public inline fun <reified R : Any> Route.post(
path: String,
crossinline body: suspend PipelineContext<Unit, ApplicationCall>.(R) -> Unit
): Route = post(path) {
body(call.receive())
}
/**
* Builds a route to match `POST` requests.
* @see [Application.routing]
*/
@KtorDsl
public fun Route.post(body: PipelineInterceptor<Unit, ApplicationCall>): Route {
return method(HttpMethod.Post) { handle(body) }
}
/**
* Builds a route to match `HEAD` requests with the specified [path].
* @see [Application.routing]
*/
@KtorDsl
public fun Route.head(path: String, body: PipelineInterceptor<Unit, ApplicationCall>): Route {
return route(path, HttpMethod.Head) { handle(body) }
}
/**
* Builds a route to match `HEAD` requests.
* @see [Application.routing]
*/
@KtorDsl
public fun Route.head(body: PipelineInterceptor<Unit, ApplicationCall>): Route {
return method(HttpMethod.Head) { handle(body) }
}
/**
* Builds a route to match `PUT` requests with the specified [path].
* @see [Application.routing]
*/
@KtorDsl
public fun Route.put(path: String, body: PipelineInterceptor<Unit, ApplicationCall>): Route {
return route(path, HttpMethod.Put) { handle(body) }
}
/**
* Builds a route to match `PUT` requests.
* @see [Application.routing]
*/
@KtorDsl
public fun Route.put(body: PipelineInterceptor<Unit, ApplicationCall>): Route {
return method(HttpMethod.Put) { handle(body) }
}
/**
* Builds a route to match `PUT` requests receiving a request body as content of the [R] type.
* @see [Application.routing]
*/
@KtorDsl
@JvmName("putTyped")
public inline fun <reified R : Any> Route.put(
crossinline body: suspend PipelineContext<Unit, ApplicationCall>.(R) -> Unit
): Route = put {
body(call.receive())
}
/**
* Builds a route to match `PUT` requests with the specified [path] receiving a request body as content of the [R] type.
* @see [Application.routing]
*/
@KtorDsl
@JvmName("putTypedPath")
public inline fun <reified R : Any> Route.put(
path: String,
crossinline body: suspend PipelineContext<Unit, ApplicationCall>.(R) -> Unit
): Route = put(path) {
body(call.receive())
}
/**
* Builds a route to match `PATCH` requests with the specified [path].
* @see [Application.routing]
*/
@KtorDsl
public fun Route.patch(path: String, body: PipelineInterceptor<Unit, ApplicationCall>): Route {
return route(path, HttpMethod.Patch) { handle(body) }
}
/**
* Builds a route to match `PATCH` requests.
* @see [Application.routing]
*/
@KtorDsl
public fun Route.patch(body: PipelineInterceptor<Unit, ApplicationCall>): Route {
return method(HttpMethod.Patch) { handle(body) }
}
/**
* Builds a route to match `PATCH` requests receiving a request body as content of the [R] type.
* @see [Application.routing]
*/
@KtorDsl
@JvmName("patchTyped")
public inline fun <reified R : Any> Route.patch(
crossinline body: suspend PipelineContext<Unit, ApplicationCall>.(R) -> Unit
): Route = patch {
body(call.receive())
}
/**
* Builds a route to match `PATCH` requests with the specified [path] receiving a request body as content of the [R] type.
* @see [Application.routing]
*/
@KtorDsl
@JvmName("patchTypedPath")
public inline fun <reified R : Any> Route.patch(
path: String,
crossinline body: suspend PipelineContext<Unit, ApplicationCall>.(R) -> Unit
): Route = patch(path) {
body(call.receive())
}
/**
* Builds a route to match `DELETE` requests with the specified [path].
* @see [Application.routing]
*/
@KtorDsl
public fun Route.delete(path: String, body: PipelineInterceptor<Unit, ApplicationCall>): Route {
return route(path, HttpMethod.Delete) { handle(body) }
}
/**
* Builds a route to match `DELETE` requests.
* @see [Application.routing]
*/
@KtorDsl
public fun Route.delete(body: PipelineInterceptor<Unit, ApplicationCall>): Route {
return method(HttpMethod.Delete) { handle(body) }
}
/**
* Builds a route to match `OPTIONS` requests with the specified [path].
* @see [Application.routing]
*/
@KtorDsl
public fun Route.options(path: String, body: PipelineInterceptor<Unit, ApplicationCall>): Route {
return route(path, HttpMethod.Options) { handle(body) }
}
/**
* Builds a route to match `OPTIONS` requests.
* @see [Application.routing]
*/
@KtorDsl
public fun Route.options(body: PipelineInterceptor<Unit, ApplicationCall>): Route {
return method(HttpMethod.Options) { handle(body) }
}
/**
* Creates a routing entry for specified path.
*/
public fun Route.createRouteFromPath(path: String): Route {
val parts = RoutingPath.parse(path).parts
var current: Route = this
for (index in parts.indices) {
val (value, kind) = parts[index]
val selector = when (kind) {
RoutingPathSegmentKind.Parameter -> PathSegmentSelectorBuilder.parseParameter(value)
RoutingPathSegmentKind.Constant -> PathSegmentSelectorBuilder.parseConstant(value)
}
// there may already be entry with same selector, so join them
current = current.createChild(selector)
}
if (path.endsWith("/")) {
current = current.createChild(TrailingSlashRouteSelector)
}
return current
}
/**
* A helper object for building instances of [RouteSelector] from path segments.
*/
public object PathSegmentSelectorBuilder {
/**
* Builds a [RouteSelector] to match a path segment parameter with a prefix/suffix and name.
*/
public fun parseParameter(value: String): RouteSelector {
val prefixIndex = value.indexOf('{')
val suffixIndex = value.lastIndexOf('}')
val prefix = if (prefixIndex == 0) null else value.substring(0, prefixIndex)
val suffix = if (suffixIndex == value.length - 1) null else value.substring(suffixIndex + 1)
val signature = value.substring(prefixIndex + 1, suffixIndex)
return when {
signature.endsWith("?") -> PathSegmentOptionalParameterRouteSelector(signature.dropLast(1), prefix, suffix)
signature.endsWith("...") -> {
if (suffix != null && suffix.isNotEmpty()) {
throw IllegalArgumentException("Suffix after tailcard is not supported")
}
PathSegmentTailcardRouteSelector(signature.dropLast(3), prefix ?: "")
}
else -> PathSegmentParameterRouteSelector(signature, prefix, suffix)
}
}
/**
* Builds a [RouteSelector] to match a path segment parameter with a prefix/suffix, name, and trailing slash, if any.
*/
@Deprecated(
"hasTrailingSlash is not used anymore. This is going to be removed",
level = DeprecationLevel.ERROR,
replaceWith = ReplaceWith("parseParameter(value)")
)
@Suppress("UNUSED_PARAMETER")
public fun parseParameter(value: String, hasTrailingSlash: Boolean): RouteSelector = parseParameter(value)
/**
* Builds a [RouteSelector] to match a constant or wildcard segment parameter.
*/
public fun parseConstant(value: String): RouteSelector = when (value) {
"*" -> PathSegmentWildcardRouteSelector
else -> PathSegmentConstantRouteSelector(value)
}
/**
* Builds a [RouteSelector] to match a constant or wildcard segment parameter and trailing slash, if any.
*/
@Deprecated(
"hasTrailingSlash is not used anymore. This is going to be removed",
level = DeprecationLevel.ERROR,
replaceWith = ReplaceWith("parseConstant(value)")
)
@Suppress("UNUSED_PARAMETER")
public fun parseConstant(value: String, hasTrailingSlash: Boolean): RouteSelector = parseConstant(value)
/**
* Parses a name out of segment specification.
*/
public fun parseName(value: String): String {
val prefix = value.substringBefore('{', "")
val suffix = value.substringAfterLast('}', "")
val signature = value.substring(prefix.length + 1, value.length - suffix.length - 1)
return when {
signature.endsWith("?") -> signature.dropLast(1)
signature.endsWith("...") -> signature.dropLast(3)
else -> signature
}
}
}