Allow OPTIONS requests when CORS is enabled

Allow OPTIONS requests when CORS is enabled

diff --git a/config/initializers/08-rack-cors.rb b/config/initializers/08-rack-cors.rb
index b59f22d..8ed2a38 100644
--- a/config/initializers/08-rack-cors.rb
+++ b/config/initializers/08-rack-cors.rb
@@ -8,7 +8,17 @@ if GlobalSetting.enable_cors
     end
 
     def call(env)
+      if env['REQUEST_METHOD'] == 'OPTIONS' and env['HTTP_ACCESS_CONTROL_REQUEST_METHOD']
+        return [200, apply_headers(env), []]
+      end
+
       status, headers, body = @app.call(env)
+      [status, apply_headers(env, headers), body]
+    end
+
+    def apply_headers(env, headers=nil)
+      headers ||= {}
+
       origin = nil
       cors_origins = @global_origins || []
       cors_origins += SiteSetting.cors_origins.split('|') if SiteSetting.cors_origins
@@ -22,7 +32,7 @@ if GlobalSetting.enable_cors
         headers['Access-Control-Allow-Credentials'] = "true"
       end
 
-      [status,headers,body]
+      headers
     end
   end
 

GitHub sha: 963b08f0

This commit has been mentioned on Discourse Meta. There might be relevant details there: