diff options
author | Joshua Peek <josh@joshpeek.com> | 2010-01-15 12:35:18 -0600 |
---|---|---|
committer | Joshua Peek <josh@joshpeek.com> | 2010-01-15 12:38:50 -0600 |
commit | 3eaf525213ccef5c63c9e296fa643ad416a3f84c (patch) | |
tree | f7ab86f8622fc7ce1247377134195bf9eac2b2b0 /actionpack/lib | |
parent | 43a84a054f51f260e1d4a9d4be75783e497a3398 (diff) | |
download | rails-3eaf525213ccef5c63c9e296fa643ad416a3f84c.tar.gz rails-3eaf525213ccef5c63c9e296fa643ad416a3f84c.tar.bz2 rails-3eaf525213ccef5c63c9e296fa643ad416a3f84c.zip |
Make HEAD method masquerade as GET so requests are routed correctly
Diffstat (limited to 'actionpack/lib')
-rw-r--r-- | actionpack/lib/action_dispatch.rb | 1 | ||||
-rwxr-xr-x | actionpack/lib/action_dispatch/http/request.rb | 12 | ||||
-rw-r--r-- | actionpack/lib/action_dispatch/middleware/head.rb | 18 |
3 files changed, 26 insertions, 5 deletions
diff --git a/actionpack/lib/action_dispatch.rb b/actionpack/lib/action_dispatch.rb index ad6d54b6a2..f6d693d6f0 100644 --- a/actionpack/lib/action_dispatch.rb +++ b/actionpack/lib/action_dispatch.rb @@ -43,6 +43,7 @@ module ActionDispatch autoload_under 'middleware' do autoload :Callbacks autoload :Cascade + autoload :Head autoload :ParamsParser autoload :Rescue autoload :ShowExceptions diff --git a/actionpack/lib/action_dispatch/http/request.rb b/actionpack/lib/action_dispatch/http/request.rb index 6e8a5dcb8a..3d0aab8a06 100755 --- a/actionpack/lib/action_dispatch/http/request.rb +++ b/actionpack/lib/action_dispatch/http/request.rb @@ -35,7 +35,8 @@ module ActionDispatch # <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 @@ -43,7 +44,8 @@ module ActionDispatch # 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 @@ module ActionDispatch # 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>, diff --git a/actionpack/lib/action_dispatch/middleware/head.rb b/actionpack/lib/action_dispatch/middleware/head.rb new file mode 100644 index 0000000000..56e2d2f2a8 --- /dev/null +++ b/actionpack/lib/action_dispatch/middleware/head.rb @@ -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 |