@@ -16,49 +16,39 @@ abstract class Route {
1616
1717 abstract fun getHandler (): Handler <RoutingContext >
1818
19- open val allowedSchemes = setOf (" http" , " https" )
20- open val allowedHosts = setOf (" localhost" , " 127.0.0.1" , " 0.0.0.0" )
21-
22- open val allowedHeaders = setOf (
23- " x-requested-with" ,
24- " Access-Control-Allow-Origin" ,
25- " origin" ,
26- " Content-Type" ,
27- " accept" ,
28- " X-PINGARUNER" ,
29- " x-csrf-token"
30- )
31-
32- open val allowedMethods = setOf<HttpMethod >(
33- HttpMethod .GET ,
34- HttpMethod .POST ,
35- HttpMethod .OPTIONS ,
36- HttpMethod .DELETE ,
37- HttpMethod .PATCH ,
38- HttpMethod .PUT
39- )
40-
41- open fun corsHandler (): Handler <RoutingContext >? = Handler { ctx ->
19+ open val allowedSchemes: Set <String > = emptySet()
20+ open val allowedHosts: Set <String > = emptySet()
21+
22+ open val allowedHeaders: Set <String > = emptySet()
23+
24+ open val allowedMethods: Set <HttpMethod > = emptySet()
25+
26+ open fun corsHandler (
27+ hosts : Set <String > = allowedHosts,
28+ schemes : Set <String > = allowedSchemes,
29+ headers : Set <String > = allowedHeaders,
30+ methods : Set <HttpMethod > = allowedMethods
31+ ): Handler <RoutingContext >? = Handler { ctx ->
4232 val origin = ctx.request().getHeader(" Origin" )
4333 if (origin != null ) {
4434 try {
4535 val uri = URI (origin)
4636 // Check the scheme and host
47- if (uri.scheme in allowedSchemes && uri.host in allowedHosts ) {
37+ if (uri.scheme in schemes && uri.host in hosts ) {
4838 // If the origin is allowed, add it to the response header
49- ctx.response().putHeader(" Access-Control-Allow-Origin" , " * " )
39+ ctx.response().putHeader(" Access-Control-Allow-Origin" , origin )
5040 }
5141 } catch (_: Exception ) {
5242 // If the URI cannot be parsed, do not add any header.
5343 }
5444 }
5545
5646 // Add the allowed methods to the header:
57- val methodsAsString = allowedMethods .joinToString(" ," ) { it.name() }
47+ val methodsAsString = methods .joinToString(" ," ) { it.name() }
5848 ctx.response().putHeader(" Access-Control-Allow-Methods" , methodsAsString)
5949
6050 // Add the allowed headers to the header:
61- val headersAsString = allowedHeaders .joinToString(" ," )
51+ val headersAsString = headers .joinToString(" ," )
6252 ctx.response().putHeader(" Access-Control-Allow-Headers" , headersAsString)
6353
6454 // If it's a Preflight (OPTIONS) request, end the response immediately:
@@ -69,6 +59,8 @@ abstract class Route {
6959 }
7060 }
7161
62+ open fun corsHandler (): Handler <RoutingContext >? = corsHandler()
63+
7264 open fun bodyHandler (): Handler <RoutingContext >? = BodyHandler .create()
7365
7466 open fun getValidationHandler (schemaRepository : SchemaRepository ): ValidationHandler ? =
0 commit comments