@@ -30,7 +30,7 @@ const SIMPLE_EXPOSE_HEADERS = [
30
30
* sure not to set request specific instance properties.
31
31
*/
32
32
export class Cors {
33
- private isEnabled : ( request : HttpContextContract [ 'request' ] ) => boolean
33
+ private isEnabled : ( request : HttpContextContract [ 'request' ] , ctx : HttpContextContract ) => boolean
34
34
35
35
constructor ( private options : CorsConfig ) {
36
36
this . normalizeOptions ( )
@@ -72,7 +72,7 @@ export class Cors {
72
72
*
73
73
* Origin match is always case sensitive
74
74
*/
75
- private computeResponseOrigin ( origin : string ) : string | null {
75
+ private computeResponseOrigin ( origin : string , ctx : HttpContextContract ) : string | null {
76
76
let allowedOrigins = this . options . origin
77
77
78
78
/**
@@ -81,7 +81,7 @@ export class Cors {
81
81
* new config value.
82
82
*/
83
83
if ( typeof allowedOrigins === 'function' ) {
84
- allowedOrigins = allowedOrigins ( origin )
84
+ allowedOrigins = allowedOrigins ( origin , ctx )
85
85
}
86
86
87
87
/**
@@ -144,14 +144,14 @@ export class Cors {
144
144
* The array items are casted to lowercase for case insensitive
145
145
* match.
146
146
*/
147
- private computedAllowedHeaders ( headers : string [ ] ) : string [ ] {
147
+ private computedAllowedHeaders ( headers : string [ ] , ctx : HttpContextContract ) : string [ ] {
148
148
let allowedHeaders = this . options . headers
149
149
150
150
/**
151
151
* Compute allowed headers by calling the config function.
152
152
*/
153
153
if ( typeof allowedHeaders === 'function' ) {
154
- allowedHeaders = allowedHeaders ( headers )
154
+ allowedHeaders = allowedHeaders ( headers , ctx )
155
155
}
156
156
157
157
/**
@@ -241,16 +241,16 @@ export class Cors {
241
241
* Handle HTTP request for CORS. This method is binded as a before hook
242
242
* to the HTTP server.
243
243
*/
244
- public async handle ( { request , response } : HttpContextContract ) {
244
+ public async handle ( ctx : HttpContextContract ) {
245
245
/**
246
246
* Return early when CORS is not enabled for the current request
247
247
*/
248
- if ( ! this . isEnabled ( request ) ) {
248
+ if ( ! this . isEnabled ( ctx . request , ctx ) ) {
249
249
return
250
250
}
251
251
252
- const origin = request . header ( 'origin' )
253
- const isOptions = request . method ( ) === 'OPTIONS'
252
+ const origin = ctx . request . header ( 'origin' )
253
+ const isOptions = ctx . request . method ( ) === 'OPTIONS'
254
254
255
255
/**
256
256
* If their is no Origin header present, then let the user-agent handle
@@ -260,7 +260,7 @@ export class Cors {
260
260
return
261
261
}
262
262
263
- const allowedOrigin = this . computeResponseOrigin ( origin )
263
+ const allowedOrigin = this . computeResponseOrigin ( origin , ctx )
264
264
265
265
/**
266
266
* If origin is not allowed, then we don't set any of the cors headers
@@ -270,7 +270,7 @@ export class Cors {
270
270
* Also end the OPTIONS request right away
271
271
*/
272
272
if ( isOptions ) {
273
- this . endPreFlight ( response )
273
+ this . endPreFlight ( ctx . response )
274
274
}
275
275
276
276
return
@@ -280,24 +280,24 @@ export class Cors {
280
280
* Set required headers for non options request.
281
281
*/
282
282
if ( ! isOptions ) {
283
- this . setOrigin ( response , allowedOrigin )
284
- this . setCredentials ( response )
285
- this . setExposedHeaders ( response )
283
+ this . setOrigin ( ctx . response , allowedOrigin )
284
+ this . setCredentials ( ctx . response )
285
+ this . setExposedHeaders ( ctx . response )
286
286
return
287
287
}
288
288
289
289
/**
290
290
* Everything below is for pre-flight (aka OPTIONS) request
291
291
*/
292
- const requestMethod = request . header ( 'Access-Control-Request-Method' )
292
+ const requestMethod = ctx . request . header ( 'Access-Control-Request-Method' )
293
293
294
294
/**
295
295
* End the request, when `Access-Control-Request-Method` is missing or isn't
296
296
* part of allowed methods.
297
297
* https://www.w3.org/TR/cors/#http-access-control-request-method
298
298
*/
299
299
if ( ! requestMethod || this . options . methods . indexOf ( requestMethod ) === - 1 ) {
300
- this . endPreFlight ( response )
300
+ this . endPreFlight ( ctx . response )
301
301
return
302
302
}
303
303
@@ -306,7 +306,7 @@ export class Cors {
306
306
* we subsitute that with an empty list.
307
307
* https://www.w3.org/TR/cors/#http-access-control-request-headers
308
308
*/
309
- let requestHeaders : unknown = request . header ( 'Access-Control-Request-Headers' )
309
+ let requestHeaders : unknown = ctx . request . header ( 'Access-Control-Request-Headers' )
310
310
if ( requestHeaders && requestHeaders !== '' ) {
311
311
requestHeaders = ( requestHeaders as string ) . split ( ',' )
312
312
} else {
@@ -316,7 +316,7 @@ export class Cors {
316
316
/**
317
317
* Computing allowed headers array from the user config
318
318
*/
319
- const allowedHeaders = this . computedAllowedHeaders ( requestHeaders as string [ ] )
319
+ const allowedHeaders = this . computedAllowedHeaders ( requestHeaders as string [ ] , ctx )
320
320
321
321
/**
322
322
* Finding if all request `Access-Control-Request-Headers` falls under the
@@ -339,16 +339,16 @@ export class Cors {
339
339
* https://www.w3.org/TR/cors/#http-access-control-request-headers
340
340
*/
341
341
if ( headersMatches === false ) {
342
- this . endPreFlight ( response )
342
+ this . endPreFlight ( ctx . response )
343
343
return
344
344
}
345
345
346
- this . setOrigin ( response , allowedOrigin )
347
- this . setCredentials ( response )
348
- this . setExposedHeaders ( response )
349
- this . setAllowMethods ( response )
350
- this . setAllowHeaders ( response , allowedHeaders )
351
- this . setMaxAge ( response )
352
- this . endPreFlight ( response )
346
+ this . setOrigin ( ctx . response , allowedOrigin )
347
+ this . setCredentials ( ctx . response )
348
+ this . setExposedHeaders ( ctx . response )
349
+ this . setAllowMethods ( ctx . response )
350
+ this . setAllowHeaders ( ctx . response , allowedHeaders )
351
+ this . setMaxAge ( ctx . response )
352
+ this . endPreFlight ( ctx . response )
353
353
}
354
354
}
0 commit comments