Class: Rack::Cors

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

Defined Under Namespace

Classes: Resource, Resources

Instance Method Summary collapse

Constructor Details

#initialize(app, opts = {}) {|_self| ... } ⇒ Cors

Returns a new instance of Cors.

Yields:

  • (_self)

Yield Parameters:

  • _self (Rack::Cors)

    the object that the method was called on



6
7
8
9
10
# File 'lib/rhoconnect/middleware/cors.rb', line 6

def initialize(app, opts={})
  @app = app
  @logger = opts[:logger]
  yield self if block_given?
end

Instance Method Details

#allow {|resources| ... } ⇒ Object

Yields:

  • (resources)


12
13
14
15
# File 'lib/rhoconnect/middleware/cors.rb', line 12

def allow
  all_resources << (resources = Resources.new)
  yield resources
end

#call(env) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rhoconnect/middleware/cors.rb', line 17

def call(env)
  if env['HTTP_ORIGIN'] == 'null'
    env['HTTP_ORIGIN'] = 'file://'
  end
  if env['HTTP_X_ORIGIN'] and not env['HTTP_ORIGIN']
    log 'ORIGIN header is empty, X-ORIGIN workaround header applied.'
    env['HTTP_ORIGIN'] = env['HTTP_X_ORIGIN']
  end
  cors_headers = nil
  if env['HTTP_ORIGIN']
    if env['REQUEST_METHOD'] == 'OPTIONS'
      if headers = process_preflight(env)
        return [200, headers, ['']]
      end
    else
      cors_headers = process_cors(env)
    end
  end
  status, headers, body = @app.call env
  headers = headers.merge(cors_headers) if cors_headers
  [status, headers, body]
end