Permalink
Browse files

Make HEAD method masquerade as GET so requests are routed correctly

  • Loading branch information...
1 parent 43a84a0 commit 3eaf525213ccef5c63c9e296fa643ad416a3f84c @josh josh committed Jan 15, 2010
@@ -43,6 +43,7 @@ module ActionDispatch
autoload_under 'middleware' do
autoload :Callbacks
autoload :Cascade
+ autoload :Head
autoload :ParamsParser
autoload :Rescue
autoload :ShowExceptions
@@ -35,15 +35,17 @@ def key?(key)
# <tt>:get</tt>. If the request \method is not listed in the HTTP_METHODS
# constant above, an UnknownHttpMethod exception is raised.
def request_method
- HTTP_METHOD_LOOKUP[super] || raise(ActionController::UnknownHttpMethod, "#{super}, accepted HTTP methods are #{HTTP_METHODS.to_sentence(:locale => :en)}")
+ method = env["rack.methodoverride.original_method"] || env["REQUEST_METHOD"]
+ HTTP_METHOD_LOOKUP[method] || raise(ActionController::UnknownHttpMethod, "#{method}, accepted HTTP methods are #{HTTP_METHODS.to_sentence(:locale => :en)}")
end
# Returns the HTTP request \method used for action processing as a
# lowercase symbol, such as <tt>:post</tt>. (Unlike #request_method, this
# method returns <tt>:get</tt> for a HEAD request because the two are
# functionally equivalent from the application's perspective.)
def method
- request_method == :head ? :get : request_method
+ method = env["REQUEST_METHOD"]
+ HTTP_METHOD_LOOKUP[method] || raise(ActionController::UnknownHttpMethod, "#{method}, accepted HTTP methods are #{HTTP_METHODS.to_sentence(:locale => :en)}")
end
# Is this a GET (or HEAD) request? Equivalent to <tt>request.method == :get</tt>.
@@ -53,17 +55,17 @@ def get?
# Is this a POST request? Equivalent to <tt>request.method == :post</tt>.
def post?
- request_method == :post
+ method == :post
end
# Is this a PUT request? Equivalent to <tt>request.method == :put</tt>.
def put?
- request_method == :put
+ method == :put
end
# Is this a DELETE request? Equivalent to <tt>request.method == :delete</tt>.
def delete?
- request_method == :delete
+ method == :delete
end
# Is this a HEAD request? Since <tt>request.method</tt> sees HEAD as <tt>:get</tt>,
@@ -0,0 +1,18 @@
+module ActionDispatch
+ class Head
+ def initialize(app)
+ @app = app
+ end
+
+ def call(env)
+ if env["REQUEST_METHOD"] == "HEAD"
+ env["REQUEST_METHOD"] = "GET"
+ env["rack.methodoverride.original_method"] = "HEAD"
+ status, headers, body = @app.call(env)
+ [status, headers, []]
+ else
+ @app.call(env)
+ end
+ end
+ end
+end
@@ -91,7 +91,7 @@ def self.build_app(routes = nil)
middleware.use "ActionDispatch::ShowExceptions"
middleware.use "ActionDispatch::Callbacks"
middleware.use "ActionDispatch::ParamsParser"
- middleware.use "Rack::Head"
+ middleware.use "ActionDispatch::Head"
}.build(routes || ActionController::Routing::Routes)
end
@@ -254,6 +254,10 @@ def post
render :text => "Created", :status => 201
end
+ def method
+ render :text => "method: #{request.method}"
+ end
+
def cookie_monster
cookies["cookie_1"] = nil
cookies["cookie_3"] = "chocolate"
@@ -379,6 +383,14 @@ def test_head
head '/post'
assert_equal 201, status
assert_equal "", body
+
+ get '/get/method'
+ assert_equal 200, status
+ assert_equal "method: get", body
+
+ head '/get/method'
+ assert_equal 200, status
+ assert_equal "", body
end
end
@@ -391,6 +403,7 @@ def with_test_route_set
with_routing do |set|
set.draw do |map|
match ':action', :to => ::IntegrationProcessTest::IntegrationController
+ get 'get/:action', :to => ::IntegrationProcessTest::IntegrationController
end
yield
end
@@ -319,7 +319,7 @@ def teardown
end
test "allow method hacking on post" do
- [:get, :head, :options, :put, :post, :delete].each do |method|
+ [:get, :options, :put, :post, :delete].each do |method|
request = stub_request "REQUEST_METHOD" => method.to_s.upcase
assert_equal(method == :head ? :get : method, request.method)
end
@@ -341,7 +341,7 @@ def teardown
end
test "head masquerading as get" do
- request = stub_request 'REQUEST_METHOD' => 'HEAD'
+ request = stub_request 'REQUEST_METHOD' => 'GET', "rack.methodoverride.original_method" => "HEAD"
assert_equal :get, request.method
assert request.get?
assert request.head?
@@ -18,7 +18,7 @@ def self.default_middleware_stack
middleware.use(lambda { ActionController::Base.session_store }, lambda { ActionController::Base.session_options })
middleware.use('ActionDispatch::ParamsParser')
middleware.use('::Rack::MethodOverride')
- middleware.use('::Rack::Head')
+ middleware.use('::ActionDispatch::Head')
end
end
@@ -23,7 +23,7 @@ def setup
"ActionDispatch::Cascade",
"ActionDispatch::ParamsParser",
"Rack::MethodOverride",
- "Rack::Head",
+ "ActionDispatch::Head",
"ActiveRecord::ConnectionAdapters::ConnectionManagement",
"ActiveRecord::QueryCache"
], middleware

0 comments on commit 3eaf525

Please sign in to comment.