Class: Sinapse::Rack::CrossOriginResourceSharing

Inherits:
Object
  • Object
show all
Includes:
Goliath::Rack::AsyncMiddleware
Defined in:
lib/sinapse/cross_origin_resource_sharing.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ CrossOriginResourceSharing

Returns a new instance of CrossOriginResourceSharing.



6
7
8
9
10
11
12
# File 'lib/sinapse/cross_origin_resource_sharing.rb', line 6

def initialize(app, options = {})
  super(app)

  @origin  = options[:origin] || '*'
  @methods = options[:methods] || %w(GET POST)
  @max_age = options[:max_age]
end

Instance Method Details

#call(env) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/sinapse/cross_origin_resource_sharing.rb', line 14

def call(env)
  env['HTTP_ORIGIN'] ||= env['HTTP_X_ORIGIN']
  env['cors.headers'] = nil

  if env['HTTP_ORIGIN']
    if env['REQUEST_METHOD'] == 'OPTIONS' && env['HTTP_ACCESS_CONTROL_REQUEST_METHOD']
      return [200, preflight_headers(env), ''] if allowed?(env)
      return [400, {}, '']
    end

    if allowed_origin?(env['HTTP_ORIGIN']) && allowed_method?(env['REQUEST_METHOD'])
      env['cors.headers'] = response_headers(env)
    end
  end

  super(env)
end

#post_process(env, status, headers, body) ⇒ Object



32
33
34
35
# File 'lib/sinapse/cross_origin_resource_sharing.rb', line 32

def post_process(env, status, headers, body)
  augmented_headers = headers.merge(env['cors.headers']) if env['cors.headers']
  [status, augmented_headers || headers, body]
end