Class: Rack::Cors::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/cors.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(public_resource, path, opts = {}) ⇒ Resource

Returns a new instance of Resource.



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# 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

Instance Attribute Details

#credentialsObject

Returns the value of attribute credentials.



266
267
268
# File 'lib/rack/cors.rb', line 266

def credentials
  @credentials
end

#exposeObject

Returns the value of attribute expose.



266
267
268
# File 'lib/rack/cors.rb', line 266

def expose
  @expose
end

#headersObject

Returns the value of attribute headers.



266
267
268
# File 'lib/rack/cors.rb', line 266

def headers
  @headers
end

#max_ageObject

Returns the value of attribute max_age.



266
267
268
# File 'lib/rack/cors.rb', line 266

def max_age
  @max_age
end

#methodsObject

Returns the value of attribute methods.



266
267
268
# File 'lib/rack/cors.rb', line 266

def methods
  @methods
end

#pathObject

Returns the value of attribute path.



266
267
268
# File 'lib/rack/cors.rb', line 266

def path
  @path
end

#patternObject

Returns the value of attribute pattern.



266
267
268
# File 'lib/rack/cors.rb', line 266

def pattern
  @pattern
end

Instance Method Details

#match?(path) ⇒ Boolean

Returns:

  • (Boolean)


286
287
288
# File 'lib/rack/cors.rb', line 286

def match?(path)
  pattern =~ path
end

#process_preflight(env) ⇒ Object



290
291
292
293
# 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) ⇒ Object



295
296
297
298
299
300
301
302
303
304
# 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