Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Rack::Cors misses the OPTIONS request #93

Closed
apraditya opened this issue Sep 17, 2015 · 6 comments
Closed

Rack::Cors misses the OPTIONS request #93

apraditya opened this issue Sep 17, 2015 · 6 comments

Comments

@apraditya
Copy link

I have rack-cors 0.4.0 installed with this config on applicaiton.rb

config.middleware.insert_before 0, 'Rack::Cors', debug: true, logger: (-> { Rails.logger }) do
  allow do
    origins '*'

    resource '/api/*',
      headers: :any,
      methods: [:get, :post, :delete, :put, :options, :head],
      max_age: 0
  end

and I keep getting OPTIONS requests handled by the Rails routes directly, instead of Rack::Cors:

2015-09-17T03:06:14.925486+00:00 app[web.1]: Started OPTIONS "/api/v2/posts/1136" for 103.252.202.20 at 2015-09-17 03:06:14 +0000
..... SKIPPED ....
2015-09-17T03:06:14.930356+00:00 app[web.1]:
2015-09-17T03:06:14.930360+00:00 app[web.1]: ActionController::RoutingError (No route matches [OPTIONS] "/api/v2/posts/1136"):
2015-09-17T03:13:33.005456+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
2015-09-17T03:13:33.005457+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/rollbar-2.1.2/lib/rollbar/middleware/rails/show_exceptions.rb:22:in `call_with_rollbar'
2015-09-17T03:13:33.005459+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
2015-09-17T03:13:33.005460+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/railties-4.2.3/lib/rails/rack/logger.rb:38:in `call_app'
2015-09-17T03:13:33.005461+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/railties-4.2.3/lib/rails/rack/logger.rb:20:in `block in call'
2015-09-17T03:13:33.005463+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/tagged_logging.rb:68:in `block in tagged'
2015-09-17T03:13:33.005464+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/tagged_logging.rb:26:in `tagged'
2015-09-17T03:13:33.005465+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/tagged_logging.rb:68:in `tagged'
2015-09-17T03:13:33.005466+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/railties-4.2.3/lib/rails/rack/logger.rb:20:in `call'
2015-09-17T03:13:33.005467+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/request_store-1.2.0/lib/request_store/middleware.rb:8:in `call'
2015-09-17T03:13:33.005468+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/middleware/request_id.rb:21:in `call'
2015-09-17T03:13:33.005470+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/rack-1.6.4/lib/rack/runtime.rb:18:in `call'
2015-09-17T03:13:33.005471+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
2015-09-17T03:13:33.005472+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/middleware/static.rb:116:in `call'
2015-09-17T03:13:33.005473+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/rack-cors-0.4.0/lib/rack/cors.rb:80:in `call'
2015-09-17T03:13:33.005475+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/railties-4.2.3/lib/rails/engine.rb:518:in `call'
2015-09-17T03:13:33.005476+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/railties-4.2.3/lib/rails/application.rb:165:in `call'
2015-09-17T03:13:33.005477+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/unicorn-4.9.0/lib/unicorn/http_server.rb:580:in `process_client'
2015-09-17T03:13:33.005478+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/unicorn-4.9.0/lib/unicorn/http_server.rb:674:in `worker_loop'
2015-09-17T03:13:33.005479+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/unicorn-4.9.0/lib/unicorn/http_server.rb:529:in `spawn_missing_workers'
2015-09-17T03:13:33.005480+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/unicorn-4.9.0/lib/unicorn/http_server.rb:140:in `start'
2015-09-17T03:13:33.005481+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/unicorn-4.9.0/bin/unicorn:126:in `<top (required)>'
2015-09-17T03:13:33.005483+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/bin/unicorn:23:in `load'
2015-09-17T03:13:33.005484+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/bin/unicorn:23:in `<main>'

It's as if Rack::Cors misses the request. This only happens on Production. Here's the middleware on Production environment:

use Rack::Cors
use ActionDispatch::Static
use #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x007f55b9ce4288>
use Rack::Runtime
use ActionDispatch::RequestId
use RequestStore::Middleware
use Rails::Rack::Logger
use ActionDispatch::ShowExceptions
use ActionDispatch::DebugExceptions
use Rollbar::Middleware::Rails::RollbarMiddleware
use ActionDispatch::RemoteIp
use ActionDispatch::Callbacks
use ActiveRecord::ConnectionAdapters::ConnectionManagement
use ActiveRecord::QueryCache
use ActionDispatch::ParamsParser
use Rack::Head
use Rack::ConditionalGet
use Rack::ETag
use Warden::Manager
use Apipie::Extractor::Recorder::Middleware
use Apipie::StaticDispatcher
run ClientsApi::Application.routes

Did I miss something? Any idea how to debug this? Thanks!

@apraditya apraditya changed the title Rack::Cors misses the OPTIONS request on one resource only Rack::Cors misses the OPTIONS request Sep 17, 2015
@alecho
Copy link

alecho commented Oct 2, 2015

Just curious, what does your request look like?

@fguillen
Copy link

Same here I keep getting a No route matches [OPTIONS] Exception. I have been searching for solution and looks like I have to add a special route in my router.rb to accept OPTIONS verb. This is confusing and looks like it should be unnecessary after install the gem.

I don't know actually how to add this OPTIONS route due I don't know to which Controller to associate it.

This is how my request looks like:

$ curl -I -X OPTIONS http://mydomain.com/api/index
HTTP/1.1 404 Not Found
Content-Type: text/html; charset=utf-8
Content-Length: 32594
X-Request-Id: 3a9902eb-e3c2-4b43-bb24-12472a71818d
X-Runtime: 0.554076
Vary: Origin
X-Rack-CORS: preflight-hit; no-origin
Date: Sat, 21 Nov 2015 20:05:52 GMT
Connection: keep-alive

@raykin
Copy link

raykin commented Jan 18, 2016

@fguillen you need specify the HTTP_ORIGIN or HTTP_X_ORIGIN in request header, it can be any value if you set

allow_origin: '*'

the output of your request give you the error on this line

X-Rack-CORS: preflight-hit; no-origin

@fguillen
Copy link

You can forget my issue, it was not caused by a problem in the rack-cors gem or configuration.

It was a problem in my routes actually the problem was that I hadn't configure the proper route in my Rails routes.rb, not the OPTIONS one but the normal GET for the index.. :/ , I'm a bit embarrassed :).. In my defense I'll say that the error was not clear.

@cyu
Copy link
Owner

cyu commented Jul 15, 2017

Fixed with #106. Closing

@cyu cyu closed this as completed Jul 15, 2017
@e0da
Copy link

e0da commented Feb 9, 2018

@fguillen I wish you'd included the critical change to your routes, but we in the future appreciate that you solved your problem. https://xkcd.com/979/

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants