class Rack::Cors::Resource

Attributes

credentials[RW]
expose[RW]
headers[RW]
max_age[RW]
methods[RW]
path[RW]
pattern[RW]

Public Class Methods

new(public_resource, path, opts={}) click to toggle source
# File lib/rack/cors.rb, line 268
def initialize(public_resource, path, opts={})
  self.path        = path
  self.methods     = prepare_and_validate_methods_option(opts[:methods]) || [:get]
  self.credentials = opts[:credentials].nil? ? true : opts[:credentials]
  self.max_age     = opts[:max_age] || 1728000
  self.pattern     = compile(path)
  @public_resource = public_resource

  self.headers = case opts[:headers]
  when :any then :any
  when nil then nil
  else
    [opts[:headers]].flatten.collect{|h| h.downcase}
  end

  self.expose = opts[:expose] ? [opts[:expose]].flatten : nil
end

Public Instance Methods

match?(path) click to toggle source
# File lib/rack/cors.rb, line 286
def match?(path)
  pattern =~ path
end
process_preflight(env) click to toggle source
# File lib/rack/cors.rb, line 290
def process_preflight(env)
  return nil if invalid_method_request?(env) || invalid_headers_request?(env)
  {'Content-Type' => 'text/plain'}.merge(to_preflight_headers(env))
end
to_headers(env) click to toggle source
# File lib/rack/cors.rb, line 295
def to_headers(env)
  x_origin = env['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']
  h = {
    'Access-Control-Allow-Origin'     => origin_for_response_header(env[ORIGIN_HEADER_KEY]),
    'Access-Control-Allow-Methods'    => methods.collect{|m| m.to_s.upcase}.join(', '),
    'Access-Control-Expose-Headers'   => expose.nil? ? '' : expose.join(', '),
    'Access-Control-Max-Age'          => max_age.to_s }
  h['Access-Control-Allow-Credentials'] = 'true' if credentials
  h
end

Protected Instance Methods

allow_headers?(request_headers) click to toggle source
# File lib/rack/cors.rb, line 334
def allow_headers?(request_headers)
  return false if headers.nil?
  headers == :any || begin
    request_headers = request_headers.split(/,\s*/) if request_headers.kind_of?(String)
    request_headers.all?{|h| headers.include?(h.downcase)}
  end
end
compile(path) click to toggle source
# File lib/rack/cors.rb, line 352
def compile(path)
  if path.respond_to? :to_str
    special_chars = %w{. + ( )}
    pattern =
      path.to_str.gsub(/((:\w+)|[\*#{special_chars.join}])/) do |match|
        case match
        when "*"
          "(.*?)"
        when *special_chars
          Regexp.escape(match)
        else
          "([^/?&#]+)"
        end
      end
    /^#{pattern}$/
  elsif path.respond_to? :match
    path
  else
    raise TypeError, path
  end
end
ensure_enum(v) click to toggle source
# File lib/rack/cors.rb, line 347
def ensure_enum(v)
  return nil if v.nil?
  [v].flatten
end
invalid_headers_request?(env) click to toggle source
# File lib/rack/cors.rb, line 329
def invalid_headers_request?(env)
  request_headers = env['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']
  request_headers && !allow_headers?(request_headers)
end
invalid_method_request?(env) click to toggle source
# File lib/rack/cors.rb, line 324
def invalid_method_request?(env)
  request_method = env['HTTP_ACCESS_CONTROL_REQUEST_METHOD']
  request_method.nil? || !methods.include?(request_method.downcase.to_sym)
end
origin_for_response_header(origin) click to toggle source
# File lib/rack/cors.rb, line 311
def origin_for_response_header(origin)
  return '*' if public_resource? && !credentials
  origin
end
prepare_and_validate_methods_option(setting) click to toggle source
# File lib/rack/cors.rb, line 342
def prepare_and_validate_methods_option setting
  raise ArgumentError.new("rack-cors methods must be an single HTTP verb, or an array of verbs") if setting && setting == :any
  ensure_enum setting
end
public_resource?() click to toggle source
# File lib/rack/cors.rb, line 307
def public_resource?
  @public_resource
end
to_preflight_headers(env) click to toggle source
# File lib/rack/cors.rb, line 316
def to_preflight_headers(env)
  h = to_headers(env)
  if env['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']
    h.merge!('Access-Control-Allow-Headers' => env['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'])
  end
  h
end