Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions lib/net/http/generic_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -379,9 +379,12 @@ def encode_multipart_form_data(out, params, opt)
elsif value.respond_to?(:size) && chunked_p
# if +out+ is an IO and +value+ is a File, use IO.copy_stream
flush_buffer(out, buf, chunked_p)
out << "%x\r\n" % value.size if chunked_p
IO.copy_stream(value, out)
out << "\r\n" if chunked_p
size = value.size
if size > 0
out << "%x\r\n" % size
IO.copy_stream(value, out)
out << "\r\n"
end
else
# +out+ is an IO, and +value+ is not a File but an IO
flush_buffer(out, buf, chunked_p)
Expand Down
36 changes: 36 additions & 0 deletions test/net/http/test_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,42 @@ def test_set_form_with_file
}
end

def test_set_form_chunked_with_empty_file
require 'tempfile'
Tempfile.create('ruby-test') {|empty|
data = [
['empty', empty, {filename: 'a'}],
['string_io', StringIO.new(''), {filename: 'b'}],
['after', 'value'],
]
expected = <<"__EOM__".gsub(/\n/, "\r\n")
--BOUNDARY
Content-Disposition: form-data; name="empty"; filename="a"
Content-Type: application/octet-stream
--BOUNDARY
Content-Disposition: form-data; name="string_io"; filename="b"
Content-Type: application/octet-stream
--BOUNDARY
Content-Disposition: form-data; name="after"
value
--BOUNDARY--
__EOM__
start {|http|
@server.mount('/', lambda {|req, res| res.body = req.body })
req = Net::HTTP::Post.new('/')
req['Transfer-Encoding'] = 'chunked'
req.set_form(data, 'multipart/form-data', boundary: 'BOUNDARY')
res = http.request req
assert_equal(expected, res.body)
}
}
end

def test_set_form_multipart_crlf_injection
build = ->(data, opt = {}) {
req = Net::HTTP::Post.new('/')
Expand Down
Loading