2011-10-16 02:00:00 +08:00
|
|
|
if GlobalSetting.enable_cors
|
2014-07-23 15:03:52 +08:00
|
|
|
class Discourse::Cors
|
|
|
|
def initialize(app, options = nil)
|
|
|
|
@app = app
|
2011-10-16 02:00:00 +08:00
|
|
|
if GlobalSetting.enable_cors && GlobalSetting.cors_origin.present?
|
|
|
|
@global_origins = GlobalSetting.cors_origin.split(',').map(&:strip)
|
|
|
|
end
|
2014-07-23 15:03:52 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def call(env)
|
|
|
|
status, headers, body = @app.call(env)
|
|
|
|
origin = nil
|
2011-10-16 02:00:00 +08:00
|
|
|
cors_origins = @global_origins || []
|
|
|
|
cors_origins += SiteSetting.cors_origins.split('|') if SiteSetting.cors_origins
|
|
|
|
|
|
|
|
if cors_origins
|
|
|
|
if origin = env['HTTP_ORIGIN']
|
|
|
|
origin = nil unless cors_origins.include?(origin)
|
|
|
|
end
|
2014-07-23 15:03:52 +08:00
|
|
|
|
2011-10-16 02:00:00 +08:00
|
|
|
headers['Access-Control-Allow-Origin'] = origin || cors_origins[0]
|
|
|
|
headers['Access-Control-Allow-Credentials'] = "true"
|
2014-07-23 15:03:52 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
[status,headers,body]
|
2013-04-22 17:16:58 +08:00
|
|
|
end
|
|
|
|
end
|
2014-07-23 15:03:52 +08:00
|
|
|
|
2011-10-16 02:00:00 +08:00
|
|
|
Rails.configuration.middleware.use Discourse::Cors
|
2013-04-22 17:16:58 +08:00
|
|
|
end
|