You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2020/07/31 15:36:56 UTC

[GitHub] [incubator-apisix] membphis commented on a change in pull request #1963: feat: add option for cors

membphis commented on a change in pull request #1963:
URL: https://github.com/apache/incubator-apisix/pull/1963#discussion_r463681866



##########
File path: apisix/plugins/cors.lua
##########
@@ -146,10 +146,16 @@ function _M.rewrite(conf, ctx)
 
     ctx.cors_allow_origins = allow_origins
     set_cors_headers(conf, ctx)
+end
 
+function _M.rewrite(conf, ctx)
     if ctx.var.request_method == "OPTIONS" then
         return 200
     end
 end
 
+function _M.header_filter(conf, ctx)

Review comment:
       two blank lines between different functions.

##########
File path: apisix/plugins/cors.lua
##########
@@ -114,17 +114,17 @@ local function set_cors_headers(conf, ctx)
         allow_methods = "GET,POST,PUT,DELETE,PATCH,HEAD,OPTIONS,CONNECT,TRACE"
     end
 
-    ngx.header["Access-Control-Allow-Origin"] = ctx.cors_allow_origins
-    ngx.header["Access-Control-Allow-Methods"] = allow_methods
-    ngx.header["Access-Control-Allow-Headers"] = conf.allow_headers
-    ngx.header["Access-Control-Max-Age"] = conf.max_age
+    core.response.set_header("Access-Control-Allow-Origin", ctx.cors_allow_origins)
+    core.response.set_header("Access-Control-Allow-Methods", allow_methods)
+    core.response.set_header("Access-Control-Allow-Headers", conf.allow_headers)
+    core.response.set_header("Access-Control-Max-Age", conf.max_age)
+    core.response.set_header("Access-Control-Expose-Headers", conf.expose_headers)
     if conf.allow_credential then
-        ngx.header["Access-Control-Allow-Credentials"] = true
+        core.response.set_header("Access-Control-Allow-Credentials", true)
     end
-    ngx.header["Access-Control-Expose-Headers"] = conf.expose_headers
 end
 
-function _M.rewrite(conf, ctx)
+local function set_cors(conf, ctx)

Review comment:
       https://github.com/apache/incubator-apisix/blob/8d2e758e1e60413f57338c7c54e5e5c405f229a5/apisix/plugins/cors.lua#L128-L148
   
   we can copy them into `header_filter` and remove function `set_cors`




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org