-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace CORS handling logic with NJS scripting (avoid using Nginx 'if…
…' directives)
- Loading branch information
1 parent
31b7074
commit c291656
Showing
5 changed files
with
288 additions
and
379 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
function handle_cors(req) { | ||
const originsRegex = new RegExp(`${req.variables.cors_origins_regex}$`, 'i'); | ||
|
||
if (originsRegex.test(req.headersIn['Origin'])) { | ||
const allowedOrigins = req.variables.cors_allowed_origins.split(','); | ||
|
||
req.headersOut['Access-Control-Allow-Origin'] = allowedOrigins.length === 1 && allowedOrigins[0] === '*' ? '*' : req.headersIn['Origin']; | ||
req.headersOut['Access-Control-Allow-Methods'] = req.variables.cors_allow_methods; | ||
req.headersOut['Access-Control-Allow-Headers'] = req.variables.cors_allow_headers; | ||
req.headersOut['Access-Control-Max-Age'] = req.variables.cors_max_age; | ||
if (req.variables.cors_allow_credentials) req.headersOut['Access-Control-Allow-Credentials'] = req.variables.cors_allow_credentials; | ||
if (req.variables.cors_expose_headers) req.headersOut['Access-Control-Expose-Headers'] = req.variables.cors_expose_headers; | ||
|
||
if (req.method === 'OPTIONS') { | ||
req.headersOut['Content-Type'] = 'text/plain charset=UTF-8'; | ||
req.headersOut['Content-Length'] = '0'; | ||
} | ||
} | ||
} | ||
|
||
export default {handle_cors}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.