]> git.openstreetmap.org Git - rails.git/blob - config/initializers/cors.rb
Merge remote-tracking branch 'upstream/pull/1858'
[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       headers["Cache-Control"] = "no-cache" if headers["Access-Control-Allow-Origin"]
11       [status, headers, body]
12     end
13   end
14 end
15
16 # Allow any and all cross-origin requests to the API. Allow any origin, and
17 # any headers. Non-browser requests do not have origin or header restrictions,
18 # so browser-requests should be similarly permitted. (Though the API does not
19 # require any custom headers, Ajax frameworks may automatically add headers
20 # such as X-Requested-By to requests.)
21 Rails.configuration.middleware.use OpenStreetMap::Cors do
22   allow do
23     origins "*"
24     resource "/oauth/*", :headers => :any, :methods => [:get, :post]
25     resource "/api/*", :headers => :any, :methods => [:get, :post, :put, :delete]
26     resource "/diary/rss", :headers => :any, :methods => [:get]
27     resource "/diary/*/rss", :headers => :any, :methods => [:get]
28     resource "/user/*/diary/rss", :headers => :any, :methods => [:get]
29   end
30 end