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

Changeset 8235

Show
Ignore:
Timestamp:
11/29/07 02:08:51 (9 months ago)
Author:
rick
Message:

Raise UnknownHttpMethod exception for unknown HTTP methods. Closes #10303 [tarmo]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/CHANGELOG

    r8234 r8235  
    11*2.0.0 [RC2]* (November 28th, 2007) 
     2 
     3* Raise UnknownHttpMethod exception for unknown HTTP methods. Closes #10303 [tarmo] 
    24 
    35* Update to Prototype -r8232.  [sam] 
  • trunk/actionpack/lib/action_controller/base.rb

    r8215 r8235  
    8686  end 
    8787 
     88  class UnknownHttpMethod < ActionControllerError #:nodoc: 
     89  end 
    8890 
    8991  # Action Controllers are the core of a web request in Rails. They are made up of one or more actions that are executed 
  • trunk/actionpack/lib/action_controller/request.rb

    r8164 r8235  
    44 
    55module ActionController 
     6  # HTTP methods which are accepted by default.  
     7  ACCEPTED_HTTP_METHODS = Set.new(%w( get head put post delete )) 
     8 
    69  # CgiRequest and TestRequest provide concrete implementations. 
    710  class AbstractRequest 
     
    1316    attr_reader :env 
    1417 
     18    # The true HTTP request method as a lowercase symbol, such as :get. 
     19    # UnknownHttpMethod is raised for invalid methods not listed in ACCEPTED_HTTP_METHODS. 
     20    def request_method 
     21      @request_method ||= begin 
     22        method = ((@env['REQUEST_METHOD'] == 'POST' && !parameters[:_method].blank?) ? parameters[:_method].to_s : @env['REQUEST_METHOD']).downcase 
     23        if ACCEPTED_HTTP_METHODS.include?(method) 
     24          method.to_sym 
     25        else 
     26          raise UnknownHttpMethod, "#{method}, accepted HTTP methods are #{ACCEPTED_HTTP_METHODS.to_a.to_sentence}" 
     27        end 
     28      end 
     29    end 
     30 
    1531    # The HTTP request method as a lowercase symbol, such as :get. 
    1632    # Note, HEAD is returned as :get since the two are functionally 
    1733    # equivalent from the application's perspective. 
    1834    def method 
    19       @request_method ||= 
    20         if @env['REQUEST_METHOD'] == 'POST' && !parameters[:_method].blank? 
    21           parameters[:_method].to_s.downcase.to_sym 
    22         else 
    23           @env['REQUEST_METHOD'].downcase.to_sym 
    24         end 
    25  
    26       @request_method == :head ? :get : @request_method 
     35      request_method == :head ? :get : request_method 
    2736    end 
    2837 
     
    3443    # Is this a POST request?  Equivalent to request.method == :post 
    3544    def post? 
    36       method == :post 
     45      request_method == :post 
    3746    end 
    3847 
    3948    # Is this a PUT request?  Equivalent to request.method == :put 
    4049    def put? 
    41       method == :put 
     50      request_method == :put 
    4251    end 
    4352 
    4453    # Is this a DELETE request?  Equivalent to request.method == :delete 
    4554    def delete? 
    46       method == :delete 
     55      request_method == :delete 
    4756    end 
    4857 
     
    5059    # HTTP method directly. 
    5160    def head? 
    52       @env['REQUEST_METHOD'].downcase.to_sym == :head 
     61      request_method == :head 
    5362    end 
    5463 
  • trunk/actionpack/test/controller/request_test.rb

    r8164 r8235  
    307307  end 
    308308 
     309  def test_invalid_http_method_raises_exception 
     310    set_request_method_to :random_method 
     311    assert_raises(ActionController::UnknownHttpMethod) do 
     312      @request.method 
     313    end 
     314  end 
     315 
    309316  def test_allow_method_hacking_on_post 
    310317    set_request_method_to :post 
    311     [:get, :put, :delete].each do |method| 
     318    [:get, :head, :put, :post, :delete].each do |method| 
    312319      @request.instance_eval { @parameters = { :_method => method } ; @request_method = nil } 
    313       assert_equal method, @request.method 
     320      assert_equal(method == :head ? :get : method, @request.method) 
     321    end 
     322  end 
     323 
     324  def test_invalid_method_hacking_on_post_raises_exception 
     325    set_request_method_to :post 
     326    @request.instance_eval { @parameters = { :_method => :random_method } ; @request_method = nil } 
     327    assert_raises(ActionController::UnknownHttpMethod) do 
     328      @request.method 
    314329    end 
    315330  end