Skip to content

Commit

Permalink
read from the input until we find the boundary. fixes #45
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove authored and josh committed Dec 19, 2010
1 parent 59bd279 commit a5dae21
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/rack/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -526,10 +526,12 @@ def self.parse_multipart(env)

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

Expand Down
41 changes: 41 additions & 0 deletions test/spec_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,47 @@
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
Expand Down

0 comments on commit a5dae21

Please sign in to comment.