Skip to content
Draft
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
30 changes: 12 additions & 18 deletions lib/net/http/generic_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ def initialize(sock)
def write(buf)
# avoid memcpy() of buf, buf can huge and eat memory bandwidth
rv = buf.bytesize
return rv if rv == 0
@sock.write("#{rv.to_s(16)}\r\n", buf, "\r\n")
rv
end
Expand Down Expand Up @@ -331,7 +332,9 @@ def send_request_with_body_data(sock, ver, path, params)
if chunked?
delete 'Content-Length' # chunked framing is authoritative; never emit both (smuggling)
write_header sock, ver, path
encode_multipart_form_data(sock, params, opt)
chunker = Chunker.new(sock)
encode_multipart_form_data(chunker, params, opt)
chunker.finish
else
require 'tempfile'
file = Tempfile.new('multipart')
Expand All @@ -353,7 +356,6 @@ def encode_multipart_form_data(out, params, opt)
if /[\r\n]/.match?(boundary.to_s)
raise ArgumentError, "multipart boundary cannot include CR/LF"
end
chunked_p = chunked?

buf = +''
params.each do |key, value, h={}|
Expand All @@ -376,16 +378,12 @@ def encode_multipart_form_data(out, params, opt)
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)
# +out+ is an IO, and +value+ is an IO
flush_buffer(out, buf)
while (chunk = value.read(256 * 1024))
out.write(chunk)
end
end
else
# non-file field:
Expand All @@ -398,8 +396,7 @@ def encode_multipart_form_data(out, params, opt)
buf << "\r\n"
end
buf << "--#{boundary}--\r\n"
flush_buffer(out, buf, chunked_p)
out << "0\r\n\r\n" if chunked_p
flush_buffer(out, buf)
end

def quote_string(str, charset)
Expand All @@ -410,11 +407,8 @@ def quote_string(str, charset)
str.gsub(/[\\"]/, '\\\\\&')
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
def flush_buffer(out, buf)
out.write(buf)
buf.clear
end

Expand Down
67 changes: 61 additions & 6 deletions test/net/http/test_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,16 @@ def test_require_does_not_load_resolv
RUBY
end

def test_chunker_skips_empty_writes
out = StringIO.new(+'')
chunker = Net::HTTPGenericRequest::Chunker.new(out)
chunker.write('')
chunker.write('ab')
chunker.write('')
chunker.finish
assert_equal "2\r\nab\r\n0\r\n\r\n", out.string
end

end

module TestNetHTTP_version_1_1_methods
Expand Down Expand Up @@ -957,12 +967,57 @@ def test_set_form_with_file
/^Content-Disposition: form-data; name="file"; filename="#{filename}"\r\n/,
header)
assert_equal(expected, body)
}
}
end

def test_set_form_chunked_with_files
require 'tempfile'
Tempfile.create('ruby-test') {|empty|
Tempfile.create('ruby-test') {|file|
file.binmode
file << "skipped" << $test_net_http_data
file.rewind
file.read(7)
string_io = StringIO.new("skipped data")
string_io.read(8)
data = [
['empty', empty, {filename: 'a'}],
['file', file, {filename: 'b'}],
['string_io', string_io, {filename: 'c'}],
['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="file"; filename="b"
Content-Type: application/octet-stream

<data>
--<boundary>
Content-Disposition: form-data; name="string_io"; filename="c"
Content-Type: application/octet-stream

# 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)
data
--<boundary>
Content-Disposition: form-data; name="after"

value
--<boundary>--
__EOM__
expected.sub!(/<data>/, $test_net_http_data)
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.gsub(/<boundary>/, 'BOUNDARY'), res.body)
}
}
}
end
Expand All @@ -971,7 +1026,7 @@ def test_set_form_multipart_crlf_injection
build = ->(data, opt = {}) {
req = Net::HTTP::Post.new('/')
req.set_form(data, 'multipart/form-data')
out = +''
out = StringIO.new(+'')
req.send(:encode_multipart_form_data, out, req.instance_variable_get(:@body_data), opt)
}
assert_raise(ArgumentError) { build.call([["foo\r\nX-Injected: 1", 'v']]) }
Expand Down
Loading