Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 5621

Show
Ignore:
Timestamp:
11/23/06 23:24:47 (3 years ago)
Author:
david
Message:

* 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.headersREQUEST_METHOD? for get the "real" answer. Closes #6694 [DHH]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1-2-pre-release/actionpack/CHANGELOG

    r5610 r5621  
    11*1.13.0 RC1* (November 22nd, 2006) 
     2 
     3* 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] 
    24 
    35* Update Routing to complain when :controller is not specified by a route. Closes #6669. [Nicholas Seckar] 
  • branches/1-2-pre-release/actionpack/lib/action_controller/request.rb

    r5272 r5621  
    1414    end 
    1515 
    16     # Returns the HTTP request method as a lowercase symbol (:get, for example) 
     16    # Returns the HTTP request method as a lowercase symbol (:get, for example). Note, HEAD is returned as :get 
     17    # since the two are supposedly to be functionaly equivilent for all purposes except that HEAD won't return a response 
     18    # body (which Rails also takes care of elsewhere). 
    1719    def method 
    1820      @request_method ||= (!parameters[:_method].blank? && @env['REQUEST_METHOD'] == 'POST') ? 
    1921        parameters[:_method].to_s.downcase.to_sym : 
    2022        @env['REQUEST_METHOD'].downcase.to_sym 
    21     end 
    22  
    23     # Is this a GET request?  Equivalent to request.method == :get 
     23       
     24      @request_method == :head ? :get : @request_method 
     25    end 
     26 
     27    # Is this a GET (or HEAD) request?  Equivalent to request.method == :get 
    2428    def get? 
    2529      method == :get 
     
    4145    end 
    4246 
    43     # Is this a HEAD request?  Equivalent to request.method == :head 
     47    # Is this a HEAD request?  HEAD is mapped as :get for request.method, so here we ask the  
     48    # REQUEST_METHOD header directly. Thus, for head, both get? and head? will return true. 
    4449    def head? 
    45       method == :head 
     50      @env['REQUEST_METHOD'].downcase.to_sym == :head 
    4651    end 
    4752 
  • branches/1-2-pre-release/actionpack/test/controller/request_test.rb

    r5272 r5621  
    275275 
    276276  def test_symbolized_request_methods 
    277     [:head, :get, :post, :put, :delete].each do |method| 
     277    [:get, :post, :put, :delete].each do |method| 
    278278      set_request_method_to method 
    279279      assert_equal method, @request.method 
     
    283283  def test_allow_method_hacking_on_post 
    284284    set_request_method_to :post 
    285     [:head, :get, :put, :delete].each do |method| 
     285    [:get, :put, :delete].each do |method| 
    286286      @request.instance_eval { @parameters = { :_method => method } ; @request_method = nil } 
    287287      assert_equal method, @request.method 
     
    291291  def test_restrict_method_hacking 
    292292    @request.instance_eval { @parameters = { :_method => 'put' } } 
    293     [:head, :get, :put, :delete].each do |method| 
     293    [:get, :put, :delete].each do |method| 
    294294      set_request_method_to method 
    295295      assert_equal method, @request.method 
    296296    end 
     297  end 
     298   
     299  def test_head_masquarading_as_get 
     300    set_request_method_to :head 
     301    assert_equal :get, @request.method 
     302    assert @request.get? 
     303    assert @request.head? 
    297304  end 
    298305 
  • trunk/actionpack/CHANGELOG

    r5607 r5621  
    11*SVN* 
     2 
     3* 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] 
    24 
    35* Update Routing to complain when :controller is not specified by a route. Closes #6669. [Nicholas Seckar] 
  • trunk/actionpack/lib/action_controller/request.rb

    r5272 r5621  
    1414    end 
    1515 
    16     # Returns the HTTP request method as a lowercase symbol (:get, for example) 
     16    # Returns the HTTP request method as a lowercase symbol (:get, for example). Note, HEAD is returned as :get 
     17    # since the two are supposedly to be functionaly equivilent for all purposes except that HEAD won't return a response 
     18    # body (which Rails also takes care of elsewhere). 
    1719    def method 
    1820      @request_method ||= (!parameters[:_method].blank? && @env['REQUEST_METHOD'] == 'POST') ? 
    1921        parameters[:_method].to_s.downcase.to_sym : 
    2022        @env['REQUEST_METHOD'].downcase.to_sym 
    21     end 
    22  
    23     # Is this a GET request?  Equivalent to request.method == :get 
     23       
     24      @request_method == :head ? :get : @request_method 
     25    end 
     26 
     27    # Is this a GET (or HEAD) request?  Equivalent to request.method == :get 
    2428    def get? 
    2529      method == :get 
     
    4145    end 
    4246 
    43     # Is this a HEAD request?  Equivalent to request.method == :head 
     47    # Is this a HEAD request?  HEAD is mapped as :get for request.method, so here we ask the  
     48    # REQUEST_METHOD header directly. Thus, for head, both get? and head? will return true. 
    4449    def head? 
    45       method == :head 
     50      @env['REQUEST_METHOD'].downcase.to_sym == :head 
    4651    end 
    4752 
  • trunk/actionpack/test/controller/request_test.rb

    r5272 r5621  
    275275 
    276276  def test_symbolized_request_methods 
    277     [:head, :get, :post, :put, :delete].each do |method| 
     277    [:get, :post, :put, :delete].each do |method| 
    278278      set_request_method_to method 
    279279      assert_equal method, @request.method 
     
    283283  def test_allow_method_hacking_on_post 
    284284    set_request_method_to :post 
    285     [:head, :get, :put, :delete].each do |method| 
     285    [:get, :put, :delete].each do |method| 
    286286      @request.instance_eval { @parameters = { :_method => method } ; @request_method = nil } 
    287287      assert_equal method, @request.method 
     
    291291  def test_restrict_method_hacking 
    292292    @request.instance_eval { @parameters = { :_method => 'put' } } 
    293     [:head, :get, :put, :delete].each do |method| 
     293    [:get, :put, :delete].each do |method| 
    294294      set_request_method_to method 
    295295      assert_equal method, @request.method 
    296296    end 
     297  end 
     298   
     299  def test_head_masquarading_as_get 
     300    set_request_method_to :head 
     301    assert_equal :get, @request.method 
     302    assert @request.get? 
     303    assert @request.head? 
    297304  end 
    298305