Changeset 6786
- Timestamp:
- 05/19/07 21:34:36 (2 years ago)
- Files:
-
- trunk/actionpack/lib/action_controller/request.rb (modified) (5 diffs)
- trunk/actionpack/test/controller/cgi_test.rb (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/actionpack/lib/action_controller/request.rb
r6765 r6786 64 64 end 65 65 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. 72 67 # 73 68 # For backward compatibility, the post format is extracted from the … … 75 70 def content_type 76 71 @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) 92 74 end 93 75 … … 309 291 end 310 292 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 311 315 312 316 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 = {}) 314 322 content_length = content_length.to_i 315 323 return {} if content_length.zero? 316 324 317 content_type, boundary = extract_multipart_boundary(content_type .to_s)325 content_type, boundary = extract_multipart_boundary(content_type_with_parameters.to_s) 318 326 return {} if content_type.blank? 319 327 … … 344 352 raise 345 353 { "body" => body, 346 "content_type" => content_type ,354 "content_type" => content_type_with_parameters, 347 355 "content_length" => content_length, 348 356 "exception" => "#{e.message} (#{e.class})", … … 445 453 MULTIPART_BOUNDARY = %r|\Amultipart/form-data.*boundary=\"?([^\";,]+)\"?|n 446 454 447 def extract_multipart_boundary(content_type )448 if content_type =~ MULTIPART_BOUNDARY455 def extract_multipart_boundary(content_type_with_parameters) 456 if content_type_with_parameters =~ MULTIPART_BOUNDARY 449 457 ['multipart/form-data', $1.dup] 450 458 else 451 content_type459 extract_content_type_without_parameters(content_type_with_parameters) 452 460 end 453 461 end trunk/actionpack/test/controller/cgi_test.rb
r6764 r6786 2 2 require 'action_controller/cgi_process' 3 3 4 class CgiRequestTest < Test::Unit::TestCase4 class BaseCgiTest < Test::Unit::TestCase 5 5 def setup 6 6 @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"} … … 11 11 end 12 12 13 def default_test; end 14 end 15 16 17 class CgiRequestTest < BaseCgiTest 13 18 def test_proxy_request 14 19 assert_equal 'glu.ttono.us', @request.host_with_port … … 53 58 end 54 59 end 60 61 62 class 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 70 end