Permalink
Browse files

* Added GET-masquarading for HEAD, so request.method will return :get…

… even for HEADs. This will help anyone relying on case request.method to automatically work with HEAD and map.resources will also allow HEADs to all GET actions. Rails automatically throws away the response content in a reply to HEAD, so you dont even need to worry about that. If you, for whatever reason, still need to distinguish between GET and HEAD in some edge case, you can use Request#head? and even Request.headers["REQUEST_METHOD"] for get the "real" answer. Closes #6694 [DHH]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5621 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent f15d52f commit efd0bdd9eaf66a361b01739522794ec0c9453d15 @dhh dhh committed Nov 23, 2006
Showing with 21 additions and 7 deletions.
  1. +2 −0 actionpack/CHANGELOG
  2. +9 −4 actionpack/lib/action_controller/request.rb
  3. +10 −3 actionpack/test/controller/request_test.rb
View
2 actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Added GET-masquarading for HEAD, so request.method will return :get even for HEADs. This will help anyone relying on case request.method to automatically work with HEAD and map.resources will also allow HEADs to all GET actions. Rails automatically throws away the response content in a reply to HEAD, so you don't even need to worry about that. If you, for whatever reason, still need to distinguish between GET and HEAD in some edge case, you can use Request#head? and even Request.headers["REQUEST_METHOD"] for get the "real" answer. Closes #6694 [DHH]
+
* Update Routing to complain when :controller is not specified by a route. Closes #6669. [Nicholas Seckar]
* Ensure render_to_string cleans up after itself when an exception is raised. #6658 [rsanheim]
View
13 actionpack/lib/action_controller/request.rb
@@ -13,14 +13,18 @@ def parameters
@parameters ||= request_parameters.update(query_parameters).update(path_parameters).with_indifferent_access
end
- # Returns the HTTP request method as a lowercase symbol (:get, for example)
+ # Returns the HTTP request method as a lowercase symbol (:get, for example). Note, HEAD is returned as :get
+ # since the two are supposedly to be functionaly equivilent for all purposes except that HEAD won't return a response
+ # body (which Rails also takes care of elsewhere).
def method
@request_method ||= (!parameters[:_method].blank? && @env['REQUEST_METHOD'] == 'POST') ?
parameters[:_method].to_s.downcase.to_sym :
@env['REQUEST_METHOD'].downcase.to_sym
+
+ @request_method == :head ? :get : @request_method
end
- # Is this a GET request? Equivalent to request.method == :get
+ # Is this a GET (or HEAD) request? Equivalent to request.method == :get
def get?
method == :get
end
@@ -40,9 +44,10 @@ def delete?
method == :delete
end
- # Is this a HEAD request? Equivalent to request.method == :head
+ # Is this a HEAD request? HEAD is mapped as :get for request.method, so here we ask the
+ # REQUEST_METHOD header directly. Thus, for head, both get? and head? will return true.
def head?
- method == :head
+ @env['REQUEST_METHOD'].downcase.to_sym == :head
end
# Determine whether the body of a HTTP call is URL-encoded (default)
View
13 actionpack/test/controller/request_test.rb
@@ -274,27 +274,34 @@ def test_reports_ssl_when_proxied_via_lighttpd
end
def test_symbolized_request_methods
- [:head, :get, :post, :put, :delete].each do |method|
+ [:get, :post, :put, :delete].each do |method|
set_request_method_to method
assert_equal method, @request.method
end
end
def test_allow_method_hacking_on_post
set_request_method_to :post
- [:head, :get, :put, :delete].each do |method|
+ [:get, :put, :delete].each do |method|
@request.instance_eval { @parameters = { :_method => method } ; @request_method = nil }
assert_equal method, @request.method
end
end
def test_restrict_method_hacking
@request.instance_eval { @parameters = { :_method => 'put' } }
- [:head, :get, :put, :delete].each do |method|
+ [:get, :put, :delete].each do |method|
set_request_method_to method
assert_equal method, @request.method
end
end
+
+ def test_head_masquarading_as_get
+ set_request_method_to :head
+ assert_equal :get, @request.method
+ assert @request.get?
+ assert @request.head?
+ end
protected
def set_request_method_to(method)

0 comments on commit efd0bdd

Please sign in to comment.