Class: Rack::Cors::Resource
- Inherits:
-
Object
- Object
- Rack::Cors::Resource
- Defined in:
- lib/rhoconnect/middleware/cors.rb
Instance Attribute Summary collapse
-
#credentials ⇒ Object
Returns the value of attribute credentials.
-
#expose ⇒ Object
Returns the value of attribute expose.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#max_age ⇒ Object
Returns the value of attribute max_age.
-
#methods ⇒ Object
Returns the value of attribute methods.
-
#path ⇒ Object
Returns the value of attribute path.
-
#pattern ⇒ Object
Returns the value of attribute pattern.
Instance Method Summary collapse
-
#initialize(public_resource, path, opts = {}) ⇒ Resource
constructor
A new instance of Resource.
- #match?(path) ⇒ Boolean
- #process_preflight(env) ⇒ Object
- #to_headers(env) ⇒ Object
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
#credentials ⇒ Object
Returns the value of attribute credentials.
106 107 108 |
# File 'lib/rhoconnect/middleware/cors.rb', line 106 def credentials @credentials end |
#expose ⇒ Object
Returns the value of attribute expose.
106 107 108 |
# File 'lib/rhoconnect/middleware/cors.rb', line 106 def expose @expose end |
#headers ⇒ Object
Returns the value of attribute headers.
106 107 108 |
# File 'lib/rhoconnect/middleware/cors.rb', line 106 def headers @headers end |
#max_age ⇒ Object
Returns the value of attribute max_age.
106 107 108 |
# File 'lib/rhoconnect/middleware/cors.rb', line 106 def max_age @max_age end |
#methods ⇒ Object
Returns the value of attribute methods.
106 107 108 |
# File 'lib/rhoconnect/middleware/cors.rb', line 106 def methods @methods end |
#path ⇒ Object
Returns the value of attribute path.
106 107 108 |
# File 'lib/rhoconnect/middleware/cors.rb', line 106 def path @path end |
#pattern ⇒ Object
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
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 |