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

Changeset 6786

Show
Ignore:
Timestamp:
05/19/07 21:34:36 (2 years ago)
Author:
bitsweat
Message:

Fix incorrent content type lookup in request parsing. Closes #8407.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/lib/action_controller/request.rb

    r6765 r6786  
    6464    end 
    6565 
    66     def content_type_with_parameters 
    67       @content_type_with_parameters ||= env['CONTENT_TYPE'].to_s 
    68     end 
    69  
    70     # Determine whether the body of a HTTP call is URL-encoded (default) 
    71     # or matches one of the registered param_parsers.  
     66    # The MIME type of the HTTP request, such as Mime::XML. 
    7267    # 
    7368    # For backward compatibility, the post format is extracted from the 
     
    7570    def content_type 
    7671      @content_type ||= 
    77         begin 
    78           # Receive header sans any charset information. 
    79           content_type = content_type_with_parameters.sub(/\s*\;.*$/, '').strip.downcase 
    80  
    81           if x_post_format = @env['HTTP_X_POST_DATA_FORMAT'] 
    82             case x_post_format.to_s.downcase 
    83             when 'yaml' 
    84               content_type = Mime::YAML.to_s 
    85             when 'xml' 
    86               content_type = Mime::XML.to_s 
    87             end 
    88           end 
    89  
    90           Mime::Type.lookup(content_type) 
    91         end 
     72        content_type_from_legacy_post_data_format_header || 
     73        Mime::Type.lookup(content_type_without_parameters) 
    9274    end 
    9375 
     
    309291    end 
    310292 
     293    protected 
     294      # The raw content type string. Use when you need parameters such as 
     295      # charset or boundary which aren't included in the content_type MIME type. 
     296      def content_type_with_parameters 
     297        env['CONTENT_TYPE'].to_s 
     298      end 
     299 
     300      # The raw content type string with its parameters stripped off. 
     301      def content_type_without_parameters 
     302        @content_type_without_parameters ||= self.class.extract_content_type_without_parameters(content_type_with_parameters) 
     303      end 
     304 
     305    private 
     306      def content_type_from_legacy_post_data_format_header 
     307        if x_post_format = @env['HTTP_X_POST_DATA_FORMAT'] 
     308          case x_post_format.to_s.downcase 
     309            when 'yaml';  Mime::YAML 
     310            when 'xml';   Mime::XML 
     311          end 
     312        end 
     313      end 
     314 
    311315 
    312316    class << self 
    313       def parse_formatted_request_parameters(body, content_type, content_length, env = {}) 
     317      def extract_content_type_without_parameters(content_type_with_parameters) 
     318        $1.strip.downcase if content_type_with_parameters =~ /^([^,\;]*)/ 
     319      end 
     320 
     321      def parse_formatted_request_parameters(body, content_type_with_parameters, content_length, env = {}) 
    314322        content_length = content_length.to_i 
    315323        return {} if content_length.zero? 
    316324 
    317         content_type, boundary = extract_multipart_boundary(content_type.to_s) 
     325        content_type, boundary = extract_multipart_boundary(content_type_with_parameters.to_s) 
    318326        return {} if content_type.blank? 
    319327 
     
    344352        raise 
    345353        { "body" => body, 
    346           "content_type" => content_type
     354          "content_type" => content_type_with_parameters
    347355          "content_length" => content_length, 
    348356          "exception" => "#{e.message} (#{e.class})", 
     
    445453        MULTIPART_BOUNDARY = %r|\Amultipart/form-data.*boundary=\"?([^\";,]+)\"?|n 
    446454 
    447         def extract_multipart_boundary(content_type
    448           if content_type =~ MULTIPART_BOUNDARY 
     455        def extract_multipart_boundary(content_type_with_parameters
     456          if content_type_with_parameters =~ MULTIPART_BOUNDARY 
    449457            ['multipart/form-data', $1.dup] 
    450458          else 
    451             content_type 
     459            extract_content_type_without_parameters(content_type_with_parameters) 
    452460          end 
    453461        end 
  • trunk/actionpack/test/controller/cgi_test.rb

    r6764 r6786  
    22require 'action_controller/cgi_process' 
    33 
    4 class CgiRequestTest < Test::Unit::TestCase 
     4class BaseCgiTest < Test::Unit::TestCase 
    55  def setup 
    66    @request_hash = {"HTTP_MAX_FORWARDS"=>"10", "SERVER_NAME"=>"glu.ttono.us:8007", "FCGI_ROLE"=>"RESPONDER", "HTTP_X_FORWARDED_HOST"=>"glu.ttono.us", "HTTP_ACCEPT_ENCODING"=>"gzip, deflate", "HTTP_USER_AGENT"=>"Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/312.5.1 (KHTML, like Gecko) Safari/312.3.1", "PATH_INFO"=>"", "HTTP_ACCEPT_LANGUAGE"=>"en", "HTTP_HOST"=>"glu.ttono.us:8007", "SERVER_PROTOCOL"=>"HTTP/1.1", "REDIRECT_URI"=>"/dispatch.fcgi", "SCRIPT_NAME"=>"/dispatch.fcgi", "SERVER_ADDR"=>"207.7.108.53", "REMOTE_ADDR"=>"207.7.108.53", "SERVER_SOFTWARE"=>"lighttpd/1.4.5", "HTTP_COOKIE"=>"_session_id=c84ace84796670c052c6ceb2451fb0f2; is_admin=yes", "HTTP_X_FORWARDED_SERVER"=>"glu.ttono.us", "REQUEST_URI"=>"/admin", "DOCUMENT_ROOT"=>"/home/kevinc/sites/typo/public", "SERVER_PORT"=>"8007", "QUERY_STRING"=>"", "REMOTE_PORT"=>"63137", "GATEWAY_INTERFACE"=>"CGI/1.1", "HTTP_X_FORWARDED_FOR"=>"65.88.180.234", "HTTP_ACCEPT"=>"*/*", "SCRIPT_FILENAME"=>"/home/kevinc/sites/typo/public/dispatch.fcgi", "REDIRECT_STATUS"=>"200", "REQUEST_METHOD"=>"GET"} 
     
    1111  end 
    1212 
     13  def default_test; end 
     14end 
     15 
     16 
     17class CgiRequestTest < BaseCgiTest 
    1318  def test_proxy_request 
    1419    assert_equal 'glu.ttono.us', @request.host_with_port 
     
    5358  end 
    5459end 
     60 
     61 
     62class CgiRequestParamsParsingTest < BaseCgiTest 
     63  def test_doesnt_break_when_content_type_has_charset 
     64    data = 'flamenco=love' 
     65    @request.env['CONTENT_LENGTH'] = data.length 
     66    @request.env['CONTENT_TYPE'] = 'application/x-www-form-urlencoded; charset=utf-8' 
     67    @request.env['RAW_POST_DATA'] = data 
     68    assert_equal({"flamenco"=> "love"}, @request.request_parameters) 
     69  end 
     70end