Class: Manifold::Middleware
- Inherits:
-
Object
- Object
- Manifold::Middleware
- Defined in:
- lib/manifold.rb
Instance Method Summary collapse
- #call(env) ⇒ Object
- #cors_headers(env) ⇒ Object
-
#initialize(app) ⇒ Middleware
constructor
A new instance of Middleware.
- #preflight?(env) ⇒ Boolean
Constructor Details
#initialize(app) ⇒ Middleware
Returns a new instance of Middleware.
18 19 20 |
# File 'lib/manifold.rb', line 18 def initialize(app) @app = app end |
Instance Method Details
#call(env) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/manifold.rb', line 22 def call(env) if preflight?(env) env['HTTP_ORIGIN'] = 'file://' if env['HTTP_ORIGIN'] == 'null' env['HTTP_ORIGIN'] ||= env['HTTP_X_ORIGIN'] headers = cors_headers(env) headers['Content-Type'] = 'text/plain' [200, headers, []] else status, headers, body = @app.call env headers['Access-Control-Allow-Origin'] = "*" headers['Access-Control-Expose-Headers'] = Manifold.config.expose.join(', ') if Manifold.config.expose [status, headers, body] end end |
#cors_headers(env) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/manifold.rb', line 42 def cors_headers(env) headers = {} headers['Access-Control-Allow-Origin'] = "*" headers['Access-Control-Allow-Methods'] = env['HTTP_ACCESS_CONTROL_REQUEST_METHOD'] headers['Access-Control-Allow-Headers'] = [ env['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'], Manifold.config.accept, 'Authorization' ].compact.join(', ') headers['Access-Control-Allow-Credentials'] = "true" headers['Access-Control-Max-Age'] = "1728000" headers end |
#preflight?(env) ⇒ Boolean
62 63 64 65 66 |
# File 'lib/manifold.rb', line 62 def preflight?(env) env['REQUEST_METHOD'] == "OPTIONS" && env['HTTP_ACCESS_CONTROL_REQUEST_METHOD'] && env['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'] end |