Skip to content
Open
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
24 changes: 10 additions & 14 deletions lib/net/http/generic_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -374,18 +374,13 @@ def encode_multipart_form_data(out, params, opt)
"name=\"#{key}\"; filename=\"#{filename}\"\r\n" \
"Content-Type: #{type}\r\n\r\n"
if !out.respond_to?(:write) || !value.respond_to?(:read)
# if +out+ is not an IO or +value+ is not an IO
buf << (value.respond_to?(:read) ? value.read : value)
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
else
# +out+ is an IO, and +value+ is not a File but an IO
flush_buffer(out, buf, chunked_p)
1 while flush_buffer(out, value.read(4096), chunked_p)
buf.clear
while (chunk = value.read(4096))
flush_buffer(out, chunk, chunked_p)
end
end
else
# non-file field:
Expand All @@ -411,11 +406,12 @@ def quote_string(str, charset)
end

def flush_buffer(out, buf, chunked_p)
return unless buf
out << "%x\r\n"%buf.bytesize if chunked_p
out << buf
out << "\r\n" if chunked_p
buf.clear
return if buf.nil? || buf.empty?
if chunked_p
out.write("%x\r\n" % buf.bytesize, buf, "\r\n")
else
out << buf
end
end

##
Expand Down
75 changes: 70 additions & 5 deletions test/net/http/test_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -957,16 +957,81 @@ def test_set_form_with_file
/^Content-Disposition: form-data; name="file"; filename="#{filename}"\r\n/,
header)
assert_equal(expected, body)
}
}
end

# TODO: test with chunked
# data.each{|k,v|v.rewind rescue nil}
# req['Transfer-Encoding'] = 'chunked'
# res = http.request req
# assert_equal(expected, res.body)
def test_set_form_multipart_chunked_frames_file_parts_by_bytes_read
require 'tempfile'
Tempfile.create('ruby-test') {|empty|
Tempfile.create('ruby-test') {|file|
large = "x" * 5000
file << "skipped " << large
file.rewind
file.read(8)
string_io = StringIO.new(+"skipped data")
string_io.read(8)
reads = ["", "data".freeze]
reader = Object.new
reader.define_singleton_method(:read) {|*| reads.shift }
req = Net::HTTP::Post.new('/')
req['Transfer-Encoding'] = 'chunked'
req.set_form([
['empty_file', empty, {filename: 'a.txt'}],
['partly_read_large_file', file, {filename: 'b.txt'}],
['partly_read_string_io', string_io, {filename: 'c.txt'}],
['reads_empty_string_first', reader, {filename: 'd.txt'}],
['after', 'value'],
], 'multipart/form-data')
out = StringIO.new(+'')
req.send(:encode_multipart_form_data, out, req.instance_variable_get(:@body_data), {boundary: 'BOUNDARY'})

expected = <<~"__EOM__".gsub(/\n/, "\r\n")
--BOUNDARY
Content-Disposition: form-data; name="empty_file"; filename="a.txt"
Content-Type: application/octet-stream


--BOUNDARY
Content-Disposition: form-data; name="partly_read_large_file"; filename="b.txt"
Content-Type: application/octet-stream

#{large}
--BOUNDARY
Content-Disposition: form-data; name="partly_read_string_io"; filename="c.txt"
Content-Type: application/octet-stream

data
--BOUNDARY
Content-Disposition: form-data; name="reads_empty_string_first"; filename="d.txt"
Content-Type: application/octet-stream

data
--BOUNDARY
Content-Disposition: form-data; name="after"

value
--BOUNDARY--
__EOM__
assert_equal expected, decode_chunked(out.string)
}
}
end

def decode_chunked(body)
chunks = []
loop do
size_line, body = body.split("\r\n", 2)
size = Integer(size_line, 16)
assert_equal "\r\n", body.byteslice(size, 2)
chunk, body = body.byteslice(0, size), body.byteslice(size + 2..)
break if size == 0
chunks << chunk
end
assert_equal "", body, 'nothing may follow the last chunk'
chunks.join
end

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