Move request parameter parsing from CGI to AbstractRequest.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6742 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Jeremy Kemper
2007-05-15 22:10:03 +00:00
parent 6d3dc90c33
commit f73e1c2ec0
5 changed files with 55 additions and 49 deletions

View File

@@ -62,20 +62,6 @@ module ActionController
parser.result
end
def parse_formatted_request_parameters(mime_type, body)
case strategy = ActionController::Base.param_parsers[mime_type]
when Proc
strategy.call(body)
when :xml_simple, :xml_node
body.blank? ? {} : Hash.from_xml(body).with_indifferent_access
when :yaml
YAML.load(body)
end
rescue Exception => e # YAML, XML or Ruby code block errors
{ "exception" => "#{e.message} (#{e.class})", "backtrace" => e.backtrace,
"body" => body, "format" => mime_type }
end
private
def get_typed_value(value)
case value

View File

@@ -76,7 +76,7 @@ module ActionController #:nodoc:
def request_parameters
@request_parameters ||=
if ActionController::Base.param_parsers.has_key?(content_type)
CGI.parse_formatted_request_parameters(content_type, body.read)
self.class.parse_formatted_request_parameters(content_type, body.read)
else
CGI.parse_request_parameters(@cgi.params)
end

View File

@@ -295,5 +295,22 @@ module ActionController
def reset_session #:nodoc:
end
def self.parse_formatted_request_parameters(mime_type, body)
case strategy = ActionController::Base.param_parsers[mime_type]
when Proc
strategy.call(body)
when :xml_simple, :xml_node
body.blank? ? {} : Hash.from_xml(body).with_indifferent_access
when :yaml
YAML.load(body)
else
{}
end
rescue Exception => e # YAML, XML or Ruby code block errors
{ "exception" => "#{e.message} (#{e.class})", "backtrace" => e.backtrace,
"body" => body, "format" => mime_type }
end
end
end

View File

@@ -274,35 +274,6 @@ class CGITest < Test::Unit::TestCase
end
class XmlCGITest < Test::Unit::TestCase
def test_single_file
raw_post_data =
"<person><name>David</name><avatar type='file' name='me.jpg' content_type='image/jpg'>#{Base64.encode64('ABC')}</avatar></person>"
person = CGI.parse_formatted_request_parameters(Mime::XML, raw_post_data)
assert_equal "image/jpg", person['person']['avatar'].content_type
assert_equal "me.jpg", person['person']['avatar'].original_filename
assert_equal "ABC", person['person']['avatar'].read
end
def test_multiple_files
raw_post_data =
"<person><name>David</name><avatars>" +
"<avatar type='file' name='me.jpg' content_type='image/jpg'>#{Base64.encode64('ABC')}</avatar>" +
"<avatar type='file' name='you.gif' content_type='image/gif'>#{Base64.encode64('DEF')}</avatar>" +
"</avatars></person>"
person = CGI.parse_formatted_request_parameters(Mime::XML, raw_post_data)
assert_equal "image/jpg", person['person']['avatars']['avatar'].first.content_type
assert_equal "me.jpg", person['person']['avatars']['avatar'].first.original_filename
assert_equal "ABC", person['person']['avatars']['avatar'].first.read
assert_equal "image/gif", person['person']['avatars']['avatar'].last.content_type
assert_equal "you.gif", person['person']['avatars']['avatar'].last.original_filename
assert_equal "DEF", person['person']['avatars']['avatar'].last.read
end
end
class MultipartCGITest < Test::Unit::TestCase
FIXTURE_PATH = File.dirname(__FILE__) + '/../fixtures/multipart'
@@ -379,15 +350,11 @@ class MultipartCGITest < Test::Unit::TestCase
private
def process(name)
old_stdin = $stdin
File.open(File.join(FIXTURE_PATH, name), 'rb') do |file|
ENV['CONTENT_LENGTH'] = file.stat.size.to_s
$stdin = file
@cgi = CGI.new
@cgi = CGI.new('query', file)
CGI.parse_request_parameters @cgi.params
end
ensure
$stdin = old_stdin
end
end

View File

@@ -355,3 +355,39 @@ class RequestTest < Test::Unit::TestCase
@request.instance_eval { @request_method = nil }
end
end
class RequestParameterParsingTest < Test::Unit::TestCase
def test_xml_with_single_file
body = "<person><name>David</name><avatar type='file' name='me.jpg' content_type='image/jpg'>#{Base64.encode64('ABC')}</avatar></person>"
person = ActionController::AbstractRequest.parse_formatted_request_parameters(Mime::XML, body)
assert_equal "image/jpg", person['person']['avatar'].content_type
assert_equal "me.jpg", person['person']['avatar'].original_filename
assert_equal "ABC", person['person']['avatar'].read
end
def test_xml_with_multiple_files
body = <<-end_body
<person>
<name>David</name>
<avatars>
<avatar type='file' name='me.jpg' content_type='image/jpg'>#{Base64.encode64('ABC')}</avatar>
<avatar type='file' name='you.gif' content_type='image/gif'>#{Base64.encode64('DEF')}</avatar>
</avatars>
</person>
end_body
person = ActionController::AbstractRequest.parse_formatted_request_parameters(Mime::XML, body)
assert_equal "image/jpg", person['person']['avatars']['avatar'].first.content_type
assert_equal "me.jpg", person['person']['avatars']['avatar'].first.original_filename
assert_equal "ABC", person['person']['avatars']['avatar'].first.read
assert_equal "image/gif", person['person']['avatars']['avatar'].last.content_type
assert_equal "you.gif", person['person']['avatars']['avatar'].last.original_filename
assert_equal "DEF", person['person']['avatars']['avatar'].last.read
end
end