Class: RrxApi::Engine

Inherits:
Rails::Engine
  • Object
show all
Defined in:
lib/rrx_api/engine.rb

Constant Summary collapse

CORS_LOCALHOST_PATTERN =
/\Ahttp:\/\/localhost(?::\d{4})?\z/.freeze

Class Method Summary collapse

Class Method Details

.cors_origin_allowed?(source, origins) ⇒ Boolean

Checks whether source matches any of the configured CORS origins. Each entry in cors_origins may be:

Returns:

  • (Boolean)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rrx_api/engine.rb', line 24

def self.cors_origin_allowed?(source, origins)
  origins.any? do |origin|
    case origin
    when Regexp then origin.match?(source)
    when String
      if origin.start_with?('*.')
        # Wildcard subdomain: *.example.com matches any scheme+subdomain of example.com
        suffix = origin[1..]  # => ".example.com"
        source.end_with?(suffix)
      else
        source == origin
      end
    end
  end
end