ruby-changes:8375
From: xibbar <ko1@a...>
Date: Fri, 24 Oct 2008 16:26:13 +0900 (JST)
Subject: [ruby-changes:8375] Ruby:r19906 (trunk): * lib/cgi/core.rb (read_multipart): change field value as String
xibbar 2008-10-24 16:25:53 +0900 (Fri, 24 Oct 2008) New Revision: 19906 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=19906 Log: * lib/cgi/core.rb (read_multipart): change field value as String from StringIO of Tempfile when multipart parse without file field. add files method that can uploaded files. [ruby-dev:36547] * test/cgi/test_cgi_multipart.rb: fix the test for core.rb. Modified files: trunk/ChangeLog trunk/lib/cgi/core.rb trunk/test/cgi/test_cgi_multipart.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 19905) +++ ChangeLog (revision 19906) @@ -1,3 +1,11 @@ +Fri Oct 24 16:13:12 2008 Takeyuki FUJIOKA <xibbar@r...> + + * lib/cgi/core.rb (read_multipart): change field value as String + from StringIO of Tempfile when multipart parse without file field. + add files method that can uploaded files. [ruby-dev:36547] + + * test/cgi/test_cgi_multipart.rb: fix the test for core.rb. + Fri Oct 24 14:22:48 2008 Nobuyoshi Nakada <nobu@r...> * common.mk (ext): split out the target for extension library. Index: lib/cgi/core.rb =================================================================== --- lib/cgi/core.rb (revision 19905) +++ lib/cgi/core.rb (revision 19906) @@ -406,6 +406,9 @@ # values is an Array. attr_reader :params + # Get the uploaed files as a hash of name=>values pairs + attr_reader :files + # Set all the parameters. def params=(hash) @params.clear @@ -422,6 +425,7 @@ raise EOFError.new("bad content body") unless first_line == status ## parse and set params params = {} + @files = {} boundary_rexp = /--#{Regexp.quote(boundary)}(#{EOL}|--)/ boundary_size = "#{EOL}--#{boundary}#{EOL}".bytesize boundary_end = nil @@ -482,7 +486,25 @@ ## query parameter name /Content-Disposition:.* name=(?:"(.*?)"|([^;\r\n]*))/i.match(head) name = $1 || $2 || '' - (params[name] ||= []) << body + if body.original_filename.empty? + value=body.read.dup.force_encoding(@accept_charset) + (params[name] ||= []) << value + unless value.valid_encoding? + if @accept_charset_error_block + @accept_charset_error_block.call(name,value) + else + raise InvalidEncoding,"Accept-Charset encoding error" + end + end + class << params[name].last;self;end.class_eval do + define_method(:read){self} + define_method(:original_filename){""} + define_method(:content_type){""} + end + else + (params[name] ||= []) << body + @files[name]=body + end ## break loop break if buf.size == 0 break if content_length == -1 Index: test/cgi/test_cgi_multipart.rb =================================================================== --- test/cgi/test_cgi_multipart.rb (revision 19905) +++ test/cgi/test_cgi_multipart.rb (revision 19906) @@ -31,7 +31,8 @@ def initialize(boundary=nil) @boundary = boundary || create_boundary() - @buf = ''.force_encoding("ascii-8bit") + @buf = '' + @buf.force_encoding("ascii-8bit") if RUBY_VERSION>="1.9" end attr_reader :boundary @@ -43,7 +44,11 @@ buf << "Content-Disposition: form-data: name=\"#{name}\"#{s}\r\n" buf << "Content-Type: #{content_type}\r\n" if content_type buf << "\r\n" - buf << value + if RUBY_VERSION>="1.9" + buf << value.dup.force_encoding("ASCII-8BIT") + else + buf << value + end buf << "\r\n" return self end @@ -148,7 +153,18 @@ @data.each do |hash| name = hash[:name] expected = hash[:value] - expected_class = @expected_class || (hash[:value].length < threshold ? StringIO : Tempfile) + if RUBY_VERSION>="1.9" + if hash[:filename] #if file + expected_class = @expected_class || (hash[:value].length < threshold ? StringIO : Tempfile) + assert(cgi.files.keys.member?(hash[:name])) + else + expected_class = String + assert_equal(expected, cgi[name]) + assert_equal(false,cgi.files.keys.member?(hash[:name])) + end + else + expected_class = @expected_class || (hash[:value].length < threshold ? StringIO : Tempfile) + end assert_kind_of(expected_class, cgi[name]) assert_equal(expected, cgi[name].read()) assert_equal(hash[:filename] || '', cgi[name].original_filename) #if hash[:filename] @@ -173,7 +189,7 @@ @boundary = '----WebKitFormBoundaryAAfvAII+YL9102cX' @data = [ {:name=>'hidden1', :value=>'foobar'}, - {:name=>'text1', :value=>"\xE3\x81\x82\xE3\x81\x84\xE3\x81\x86\xE3\x81\x88\xE3\x81\x8A"}, + {:name=>'text1', :value=>"\xE3\x81\x82\xE3\x81\x84\xE3\x81\x86\xE3\x81\x88\xE3\x81\x8A".force_encoding("UTF-8")}, {:name=>'file1', :value=>_read('file1.html'), :filename=>'file1.html', :content_type=>'text/html'}, {:name=>'image1', :value=>_read('small.png'), @@ -188,7 +204,7 @@ @boundary = '----WebKitFormBoundaryAAfvAII+YL9102cX' @data = [ {:name=>'hidden1', :value=>'foobar'}, - {:name=>'text1', :value=>"\xE3\x81\x82\xE3\x81\x84\xE3\x81\x86\xE3\x81\x88\xE3\x81\x8A"}, + {:name=>'text1', :value=>"\xE3\x81\x82\xE3\x81\x84\xE3\x81\x86\xE3\x81\x88\xE3\x81\x8A".force_encoding("UTF-8")}, {:name=>'file1', :value=>_read('file1.html'), :filename=>'file1.html', :content_type=>'text/html'}, {:name=>'image1', :value=>_read('large.png'), @@ -279,7 +295,7 @@ @boundary = '(.|\n)*' @data = [ {:name=>'hidden1', :value=>'foobar'}, - {:name=>'text1', :value=>"\xE3\x81\x82\xE3\x81\x84\xE3\x81\x86\xE3\x81\x88\xE3\x81\x8A"}, + {:name=>'text1', :value=>"\xE3\x81\x82\xE3\x81\x84\xE3\x81\x86\xE3\x81\x88\xE3\x81\x8A".force_encoding("UTF-8")}, {:name=>'file1', :value=>_read('file1.html'), :filename=>'file1.html', :content_type=>'text/html'}, {:name=>'image1', :value=>_read('small.png'), -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/