1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
# frozen_string_literal: true
require_relative 'resources/cors_misconfiguration_error'
module Rack
class Cors
class Resources
attr_reader :resources
def initialize
@origins = []
@resources = []
@public_resources = false
end
def origins(*args, &blk)
@origins = args.flatten.reject { |s| s == '' }.map do |n|
case n
when Proc, Regexp, %r{^[a-z][a-z0-9.+-]*://}
n
when '*'
@public_resources = true
n
else
Regexp.compile("^[a-z][a-z0-9.+-]*:\\\/\\\/#{Regexp.quote(n)}$")
end
end.flatten
@origins.push(blk) if blk
end
def resource(path, opts = {})
@resources << Resource.new(public_resources?, path, opts)
end
def public_resources?
@public_resources
end
def allow_origin?(source, env = {})
return true if public_resources?
!!@origins.detect do |origin|
if origin.is_a?(Proc)
origin.call(source, env)
elsif origin.is_a?(Regexp)
source =~ origin
else
source == origin
end
end
end
def match_resource(path, env)
@resources.detect { |r| r.match?(path, env) }
end
def resource_for_path(path)
@resources.detect { |r| r.matches_path?(path) }
end
end
end
end
|