Module: Tina4::CorsMiddleware
- Defined in:
- lib/tina4/cors.rb
Class Method Summary collapse
-
.apply_headers(response_headers, env = {}) ⇒ Object
Apply CORS headers to a response headers hash.
- .config ⇒ Object
-
.origin_allowed?(origin) ⇒ Boolean
Check if a given origin is allowed.
-
.preflight_response(env = {}) ⇒ Object
Handle OPTIONS preflight request, returns a Rack response array.
- .reset! ⇒ Object
Class Method Details
.apply_headers(response_headers, env = {}) ⇒ Object
Apply CORS headers to a response headers hash
31 32 33 34 35 36 37 38 39 |
# File 'lib/tina4/cors.rb', line 31 def apply_headers(response_headers, env = {}) origin = resolve_origin(env) response_headers["access-control-allow-origin"] = origin response_headers["access-control-allow-methods"] = config[:methods] response_headers["access-control-allow-headers"] = config[:headers] response_headers["access-control-max-age"] = config[:max_age] response_headers["access-control-allow-credentials"] = config[:credentials] if config[:credentials] == "true" response_headers end |
.config ⇒ Object
6 7 8 |
# File 'lib/tina4/cors.rb', line 6 def config @config ||= load_config end |
.origin_allowed?(origin) ⇒ Boolean
Check if a given origin is allowed
42 43 44 45 46 47 |
# File 'lib/tina4/cors.rb', line 42 def origin_allowed?(origin) return true if config[:origins] == "*" allowed = config[:origins].split(",").map(&:strip) allowed.include?(origin) end |
.preflight_response(env = {}) ⇒ Object
Handle OPTIONS preflight request, returns a Rack response array
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/tina4/cors.rb', line 15 def preflight_response(env = {}) origin = resolve_origin(env) [ 204, { "access-control-allow-origin" => origin, "access-control-allow-methods" => config[:methods], "access-control-allow-headers" => config[:headers], "access-control-max-age" => config[:max_age], "access-control-allow-credentials" => config[:credentials] }, [""] ] end |
.reset! ⇒ Object
10 11 12 |
# File 'lib/tina4/cors.rb', line 10 def reset! @config = nil end |