]> git.openstreetmap.org Git - rails.git/blob - config/initializers/cors.rb
Monkey patch the port number in the rack request
[rails.git] / config / initializers / cors.rb
1 require "rack/cors"
2
3 # Mark CORS responses as uncacheable as we don't want a browser to
4 # try and reuse a response that had a different origin, even with
5 # revalidation, as the origin check will fail.
6 module OpenStreetMap
7   class Cors < Rack::Cors
8     def call(env)
9       status, headers, body = super env
10       if headers["Access-Control-Allow-Origin"]
11         headers["Cache-Control"] = "no-cache"
12       end
13       [status, headers, body]
14     end
15   end
16 end
17
18 # Allow any and all cross-origin requests to the API. Allow any origin, and
19 # any headers. Non-browser requests do not have origin or header restrictions,
20 # so browser-requests should be similarly permitted. (Though the API does not
21 # require any custom headers, Ajax frameworks may automatically add headers
22 # such as X-Requested-By to requests.)
23 Rails.configuration.middleware.use OpenStreetMap::Cors do
24   allow do
25     origins "*"
26     resource "/oauth/*", :headers => :any, :methods => [:get, :post]
27     resource "/api/*", :headers => :any, :methods => [:get, :post, :put, :delete]
28   end
29 end