DEV: apply allow origin response header for CDN requests. (#11893)

Currently, it creates a CORS error while accessing those static files.
This commit is contained in:
Vinoth Kannan
2021-01-29 07:44:49 +05:30
committed by GitHub
parent 4af4d36175
commit a5923ad603
12 changed files with 62 additions and 6 deletions

View File

@ -17,6 +17,7 @@ end
module Discourse
DB_POST_MIGRATE_PATH ||= "db/post_migrate"
REQUESTED_HOSTNAME ||= "REQUESTED_HOSTNAME"
require 'sidekiq/exception_handler'
class SidekiqExceptionHandler
@ -917,6 +918,24 @@ module Discourse
def self.is_parallel_test?
ENV['RAILS_ENV'] == "test" && ENV['TEST_ENV_NUMBER']
end
CDN_REQUEST_METHODS ||= ["GET", "HEAD", "OPTIONS"]
def self.is_cdn_request?(env, request_method)
return unless CDN_REQUEST_METHODS.include?(request_method)
cdn_hostnames = GlobalSetting.cdn_hostnames
return if cdn_hostnames.blank?
requested_hostname = env[REQUESTED_HOSTNAME] || env[Rack::HTTP_HOST]
cdn_hostnames.include?(requested_hostname)
end
def self.apply_cdn_headers(headers)
headers['Access-Control-Allow-Origin'] = '*'
headers['Access-Control-Allow-Methods'] = CDN_REQUEST_METHODS.join(", ")
headers
end
end
# rubocop:enable Style/GlobalVars