Rails + Rack Middleware
Rails just released Metal!
Giving us a great framework to build custom Rack processors alongside our application.
Check out this great article over at Soylent Foo.
Recently we had a problem where Microsoft Office was somehow issuing OPTIONS request to our site.
This was blowing up our RESTful controllers that weren’t invited to the OPTIONS party.
We played with Apache rewrites based on REQUEST_METHOD,
but ultimately, we wasted a lot of time fighting against Rails and routing exceptions, blahblahblah.
But now Rails is running off Rack we can deal with it the correct way.
class OptionsCatcher < Rails::Rack::Metal
def call(env)
if env["REQUEST_METHOD"] =~ /OPTIONS/i
[403, {"Content-Type" => "text/html"}, "OPTIONS requests are forbidden"]
else
super
end
end
end
B00M!
Now this is technically just middleware, and should be declared with a
config.middleware.use OptionsCatcher
and in lib/options_catcher.rb
class OptionsMiddleware
def initialize(app)
@app = app
end
def call(env)
if env["REQUEST_METHOD"] =~ /OPTIONS/i
[403, {"Content-Type" => "text/html"}, "OPTIONS requests are forbidden"]
else
@app.call(env)
end
end
end
but doing it as a Metal makes sense, and is easier than remembering the structure of middleware each time
But there seems to be a problem at the moment with this.
Will try and get a proper Metal working shortly.