Class: Rack::Cors::Resource

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Resource.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/rhoconnect/middleware/cors.rb', line 108

def initialize(public_resource, path, opts={})
  self.path        = path
  self.methods     = ensure_enum(opts[:methods]) || [:get]
  self.credentials = opts[:credentials] || true
  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 = case opts[:expose]
  when nil then nil
  else
    [opts[:expose]].flatten
  end
end

Instance Attribute Details

#credentialsObject

Returns the value of attribute credentials.



106
107
108
# File 'lib/rhoconnect/middleware/cors.rb', line 106

def credentials
  @credentials
end

#exposeObject

Returns the value of attribute expose.



106
107
108
# File 'lib/rhoconnect/middleware/cors.rb', line 106

def expose
  @expose
end

#headersObject

Returns the value of attribute headers.



106
107
108
# File 'lib/rhoconnect/middleware/cors.rb', line 106

def headers
  @headers
end

#max_ageObject

Returns the value of attribute max_age.



106
107
108
# File 'lib/rhoconnect/middleware/cors.rb', line 106

def max_age
  @max_age
end

#methodsObject

Returns the value of attribute methods.



106
107
108
# File 'lib/rhoconnect/middleware/cors.rb', line 106

def methods
  @methods
end

#pathObject

Returns the value of attribute path.



106
107
108
# File 'lib/rhoconnect/middleware/cors.rb', line 106

def path
  @path
end

#patternObject

Returns the value of attribute pattern.



106
107
108
# File 'lib/rhoconnect/middleware/cors.rb', line 106

def pattern
  @pattern
end

Instance Method Details

#match?(path) ⇒ Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/rhoconnect/middleware/cors.rb', line 130

def match?(path)
  pattern =~ path
end

#process_preflight(env) ⇒ Object



134
135
136
137
# File 'lib/rhoconnect/middleware/cors.rb', line 134

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



139
140
141
142
143
144
145
146
147
# File 'lib/rhoconnect/middleware/cors.rb', line 139

def to_headers(env)
  x_origin = env['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']
  h = { 'Access-Control-Allow-Origin' => public_resource? ? '*' : env['HTTP_ORIGIN'],
    '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