Skip to content

Commit b38c279

Browse files
committed
Support chunked data and fixed test failure with multipart/form-data
1 parent 9c16c38 commit b38c279

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

test/net/http/test_http.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,7 @@ def test_set_form
843843
__EOM__
844844
start {|http|
845845
_test_set_form_urlencoded(http, data.reject{|k,v|!v.is_a?(String)})
846+
@server.mount('/', lambda {|req, res| res.body = req.body })
846847
_test_set_form_multipart(http, false, data, expected)
847848
_test_set_form_multipart(http, true, data, expected)
848849
}
@@ -887,6 +888,7 @@ def test_set_form_with_file
887888
expected.sub!(/<filename>/, filename)
888889
expected.sub!(/<data>/, $test_net_http_data)
889890
start {|http|
891+
@server.mount('/', lambda {|req, res| res.body = req.body })
890892
data.each{|k,v|v.rewind rescue nil}
891893
req = Net::HTTP::Post.new('/')
892894
req.set_form(data, 'multipart/form-data')

test/net/http/utils.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,12 @@ def initialize(method, path, headers, socket)
9696
@path, @query = parse_path_and_query(path)
9797
@headers = headers
9898
@socket = socket
99-
if method == 'POST' && @path == '/continue'
100-
@body = read_body
99+
if method == 'POST' && (@path == '/continue' || @headers['Content-Type'].include?('multipart/form-data'))
100+
if @headers['Transfer-Encoding'] == 'chunked'
101+
@body = read_chunked_body
102+
else
103+
@body = read_body
104+
end
101105
@query = @body.split('&').each_with_object({}) do |pair, hash|
102106
key, value = pair.split('=')
103107
hash[key] = value
@@ -148,6 +152,15 @@ def read_body
148152
return unless content_length && content_length > 0
149153
@socket.read(content_length)
150154
end
155+
156+
def read_chunked_body
157+
body = ""
158+
while (chunk_size = @socket.gets.strip.to_i(16)) > 0
159+
body << @socket.read(chunk_size)
160+
@socket.read(2) # read \r\n after each chunk
161+
end
162+
body
163+
end
151164
end
152165

153166
class Response

0 commit comments

Comments
 (0)