public
Fork of rails/rails
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/jeremy/rails.git
Search Repo:
Ruby 1.9 compat: ensure binary encoding for post body parsing
jeremy (author)
Mon May 19 16:01:42 -0700 2008
commit  b43309328a4a4c1f6fda6db40a4a86a3c8d643fc
tree    bb5ace11b3167d0b34155ede7d8523aac5d35f57
parent  b5c8433a6f7f869bfcd2001f8c3c4660716e873b
...
16
17
18
 
19
20
21
...
16
17
18
19
20
21
22
0
@@ -16,6 +16,7 @@
0
 
0
       def initialize_with_stdinput(type = nil, stdinput = $stdin)
0
         @stdinput = stdinput
0
+ @stdinput.set_encoding(Encoding::BINARY) if @stdinput.respond_to?(:set_encoding)
0
         initialize_without_stdinput(type || 'query')
0
       end
0
     end
...
65
66
67
 
68
69
70
...
65
66
67
68
69
70
71
0
@@ -65,6 +65,7 @@
0
     # variable is already set, wrap it in a StringIO.
0
     def body
0
       if raw_post = env['RAW_POST_DATA']
0
+ raw_post.force_encoding(Encoding::BINARY) if raw_post.respond_to?(:force_encoding)
0
         StringIO.new(raw_post)
0
       else
0
         @cgi.stdinput
...
228
229
230
 
 
231
232
233
...
382
383
384
 
 
385
386
387
...
228
229
230
231
232
233
234
235
...
384
385
386
387
388
389
390
391
0
@@ -228,6 +228,8 @@
0
 
0
             super
0
 
0
+ stdinput.set_encoding(Encoding::BINARY) if stdinput.respond_to?(:set_encoding)
0
+ stdinput.force_encoding(Encoding::BINARY) if stdinput.respond_to?(:force_encoding)
0
             @stdinput = stdinput.is_a?(IO) ? stdinput : StringIO.new(stdinput || '')
0
           end
0
         end
0
@@ -382,6 +384,8 @@
0
           multipart_requestify(params).map do |key, value|
0
             if value.respond_to?(:original_filename)
0
               File.open(value.path) do |f|
0
+ f.set_encoding(Encoding::BINARY) if f.respond_to?(:set_encoding)
0
+
0
                 <<-EOF
0
 --#{boundary}\r
0
 Content-Disposition: form-data; name="#{key}"; filename="#{CGI.escape(value.original_filename)}"\r
...
466
467
468
469
470
 
 
471
472
473
...
519
520
521
522
 
523
524
525
526
...
529
530
531
 
 
 
 
 
 
532
533
 
534
535
536
...
541
542
543
544
 
545
546
547
548
549
550
551
...
563
564
565
566
 
567
568
569
 
570
571
572
573
574
575
 
576
577
578
579
580
581
 
582
583
 
584
585
586
...
607
608
609
610
 
611
612
613
...
466
467
468
 
 
469
470
471
472
473
...
519
520
521
 
522
523
524
525
526
...
529
530
531
532
533
534
535
536
537
538
 
539
540
541
542
...
547
548
549
 
550
551
552
553
554
555
556
557
...
569
570
571
 
572
573
574
 
575
576
577
578
579
580
 
581
582
583
584
585
586
 
587
588
 
589
590
591
592
...
613
614
615
 
616
617
618
619
0
@@ -466,8 +466,8 @@
0
         parser.result
0
       end
0
 
0
- def parse_multipart_form_parameters(body, boundary, content_length, env)
0
- parse_request_parameters(read_multipart(body, boundary, content_length, env))
0
+ def parse_multipart_form_parameters(body, boundary, body_size, env)
0
+ parse_request_parameters(read_multipart(body, boundary, body_size, env))
0
       end
0
 
0
       def extract_multipart_boundary(content_type_with_parameters)
0
@@ -519,7 +519,7 @@
0
 
0
         EOL = "\015\012"
0
 
0
- def read_multipart(body, boundary, content_length, env)
0
+ def read_multipart(body, boundary, body_size, env)
0
           params = Hash.new([])
0
           boundary = "--" + boundary
0
           quoted_boundary = Regexp.quote(boundary)
0
0
@@ -529,8 +529,14 @@
0
 
0
           # start multipart/form-data
0
           body.binmode if defined? body.binmode
0
+ case body
0
+ when File
0
+ body.set_encoding(Encoding::BINARY) if body.respond_to?(:set_encoding)
0
+ when StringIO
0
+ body.string.force_encoding(Encoding::BINARY) if body.string.respond_to?(:force_encoding)
0
+ end
0
           boundary_size = boundary.size + EOL.size
0
- content_length -= boundary_size
0
+ body_size -= boundary_size
0
           status = body.read(boundary_size)
0
           if nil == status
0
             raise EOFError, "no content body"
0
@@ -541,7 +547,7 @@
0
           loop do
0
             head = nil
0
             content =
0
- if 10240 < content_length
0
+ if 10240 < body_size
0
                 UploadedTempfile.new("CGI")
0
               else
0
                 UploadedStringIO.new
0
0
0
0
0
@@ -563,24 +569,24 @@
0
                 buf[0 ... (buf.size - (EOL + boundary + EOL).size)] = ""
0
               end
0
 
0
- c = if bufsize < content_length
0
+ c = if bufsize < body_size
0
                     body.read(bufsize)
0
                   else
0
- body.read(content_length)
0
+ body.read(body_size)
0
                   end
0
               if c.nil? || c.empty?
0
                 raise EOFError, "bad content body"
0
               end
0
               buf.concat(c)
0
- content_length -= c.size
0
+ body_size -= c.size
0
             end
0
 
0
             buf = buf.sub(/\A((?:.|\n)*?)(?:[\r\n]{1,2})?#{quoted_boundary}([\r\n]{1,2}|--)/n) do
0
               content.print $1
0
               if "--" == $2
0
- content_length = -1
0
+ body_size = -1
0
               end
0
- boundary_end = $2.dup
0
+ boundary_end = $2.dup
0
               ""
0
             end
0
 
0
@@ -607,7 +613,7 @@
0
             else
0
               params[name] = [content]
0
             end
0
- break if content_length == -1
0
+ break if body_size == -1
0
           end
0
           raise EOFError, "bad boundary end of body part" unless boundary_end=~/--/
0
 
...
49
50
51
52
 
53
54
55
...
340
341
342
 
343
344
345
...
49
50
51
 
52
53
54
55
...
340
341
342
343
344
345
346
0
@@ -49,7 +49,7 @@
0
     # Either the RAW_POST_DATA environment variable or the URL-encoded request
0
     # parameters.
0
     def raw_post
0
- env['RAW_POST_DATA'] ||= url_encoded_request_parameters
0
+ env['RAW_POST_DATA'] ||= returning(url_encoded_request_parameters) { |b| b.force_encoding(Encoding::BINARY) if b.respond_to?(:force_encoding) }
0
     end
0
 
0
     def port=(number)
0
@@ -340,6 +340,7 @@
0
       @content_type = content_type
0
       @original_filename = path.sub(/^.*#{File::SEPARATOR}([^#{File::SEPARATOR}]+)$/) { $1 }
0
       @tempfile = Tempfile.new(@original_filename)
0
+ @tempfile.set_encoding(Encoding::BINARY) if @tempfile.respond_to?(:set_encoding)
0
       @tempfile.binmode if binary
0
       FileUtils.copy_file(path, @tempfile.path)
0
     end
...
511
512
513
 
 
 
 
 
 
 
 
514
515
516
517
 
 
518
519
520
521
522
523
 
524
525
526
527
...
529
530
531
532
 
533
534
535
 
536
537
538
539
...
541
542
543
544
 
545
546
547
 
548
549
550
...
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
 
533
534
535
536
537
...
539
540
541
 
542
543
544
 
545
546
547
548
549
...
551
552
553
 
554
555
556
 
557
558
559
560
0
@@ -511,16 +511,26 @@
0
 
0
   FILES_DIR = File.dirname(__FILE__) + '/../fixtures/multipart'
0
 
0
+ if RUBY_VERSION < '1.9'
0
+ READ_BINARY = 'rb'
0
+ READ_PLAIN = 'r'
0
+ else
0
+ READ_BINARY = 'rb:binary'
0
+ READ_PLAIN = 'r:binary'
0
+ end
0
+
0
   def test_test_uploaded_file
0
     filename = 'mona_lisa.jpg'
0
     path = "#{FILES_DIR}/#{filename}"
0
     content_type = 'image/png'
0
+ expected = File.read(path)
0
+ expected.force_encoding(Encoding::BINARY) if expected.respond_to?(:force_encoding)
0
 
0
     file = ActionController::TestUploadedFile.new(path, content_type)
0
     assert_equal filename, file.original_filename
0
     assert_equal content_type, file.content_type
0
     assert_equal file.path, file.local_path
0
- assert_equal File.read(path), file.read
0
+ assert_equal expected, file.read
0
   end
0
   
0
   def test_test_uploaded_file_with_binary
0
0
@@ -529,10 +539,10 @@
0
     content_type = 'image/png'
0
     
0
     binary_uploaded_file = ActionController::TestUploadedFile.new(path, content_type, :binary)
0
- assert_equal File.open(path, 'rb').read, binary_uploaded_file.read
0
+ assert_equal File.open(path, READ_BINARY).read, binary_uploaded_file.read
0
     
0
     plain_uploaded_file = ActionController::TestUploadedFile.new(path, content_type)
0
- assert_equal File.open(path, 'r').read, plain_uploaded_file.read
0
+ assert_equal File.open(path, READ_PLAIN).read, plain_uploaded_file.read
0
   end
0
 
0
   def test_fixture_file_upload_with_binary
0
0
@@ -541,10 +551,10 @@
0
     content_type = 'image/jpg'
0
     
0
     binary_file_upload = fixture_file_upload(path, content_type, :binary)
0
- assert_equal File.open(path, 'rb').read, binary_file_upload.read
0
+ assert_equal File.open(path, READ_BINARY).read, binary_file_upload.read
0
     
0
     plain_file_upload = fixture_file_upload(path, content_type)
0
- assert_equal File.open(path, 'r').read, plain_file_upload.read
0
+ assert_equal File.open(path, READ_PLAIN).read, plain_file_upload.read
0
   end
0
 
0
   def test_fixture_file_upload

Comments

    No one has commented yet.