summary refs log tree commit
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2010-12-18 05:07:13 +0800
committerJoshua Peek <josh@joshpeek.com>2010-12-20 04:00:54 +0800
commita5dae21c4f9044a7422aebd6f1137ef94a20f684 (patch)
treed94dc866c6f14282c1fb3beaf3cd688623e8f8eb
parent59bd279ab832cda3a6cc43bfd9590c3008c58285 (diff)
downloadrack-a5dae21c4f9044a7422aebd6f1137ef94a20f684.tar.gz
read from the input until we find the boundary. fixes #45
-rw-r--r--lib/rack/utils.rb8
-rw-r--r--test/spec_request.rb41
2 files changed, 46 insertions, 3 deletions
diff --git a/lib/rack/utils.rb b/lib/rack/utils.rb
index c44bae8f..725646ea 100644
--- a/lib/rack/utils.rb
+++ b/lib/rack/utils.rb
@@ -526,10 +526,12 @@ module Rack
 
           content_length -= boundary_size
 
-          read_buffer = ''
+          read_buffer = nil
 
-          status = input.read(boundary_size, read_buffer)
-          raise EOFError, "bad content body"  unless status == boundary + EOL
+          loop do
+            read_buffer = input.gets
+            break if read_buffer == boundary + EOL
+          end
 
           rx = /(?:#{EOL})?#{Regexp.quote boundary}(#{EOL}|--)/n
 
diff --git a/test/spec_request.rb b/test/spec_request.rb
index 9f77f131..c985d5a6 100644
--- a/test/spec_request.rb
+++ b/test/spec_request.rb
@@ -410,6 +410,47 @@ describe Rack::Request do
       req.media_type_params['bling'].should.equal 'bam'
   end
 
+  should "parse with junk before boundry" do
+    # Adapted from RFC 1867.
+    input = <<EOF
+blah blah\r
+\r
+--AaB03x\r
+content-disposition: form-data; name="reply"\r
+\r
+yes\r
+--AaB03x\r
+content-disposition: form-data; name="fileupload"; filename="dj.jpg"\r
+Content-Type: image/jpeg\r
+Content-Transfer-Encoding: base64\r
+\r
+/9j/4AAQSkZJRgABAQAAAQABAAD//gA+Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcg\r
+--AaB03x--\r
+EOF
+    req = Rack::Request.new Rack::MockRequest.env_for("/",
+                      "CONTENT_TYPE" => "multipart/form-data, boundary=AaB03x",
+                      "CONTENT_LENGTH" => input.size,
+                      :input => input)
+
+    req.POST.should.include "fileupload"
+    req.POST.should.include "reply"
+
+    req.should.be.form_data
+    req.content_length.should.equal input.size
+    req.media_type.should.equal 'multipart/form-data'
+    req.media_type_params.should.include 'boundary'
+    req.media_type_params['boundary'].should.equal 'AaB03x'
+
+    req.POST["reply"].should.equal "yes"
+
+    f = req.POST["fileupload"]
+    f.should.be.kind_of Hash
+    f[:type].should.equal "image/jpeg"
+    f[:filename].should.equal "dj.jpg"
+    f.should.include :tempfile
+    f[:tempfile].size.should.equal 76
+  end
+
   should "parse multipart form data" do
     # Adapted from RFC 1867.
     input = <<EOF