diff --git a/test/net/http/utils.rb b/test/net/http/utils.rb index b9b289d..44a61c9 100644 --- a/test/net/http/utils.rb +++ b/test/net/http/utils.rb @@ -150,13 +150,27 @@ def parse_path_and_query(path) def read_body content_length = @headers['Content-Length']&.to_i return unless content_length && content_length > 0 - @socket.read(content_length) + + body = "" + while body.bytesize < content_length + chunk = @socket.read(content_length - body.bytesize) + break unless chunk + body << chunk + end + + body end def read_chunked_body body = "" while (chunk_size = @socket.gets.strip.to_i(16)) > 0 - body << @socket.read(chunk_size) + chunk = "" + while chunk.bytesize < chunk_size + part = @socket.read(chunk_size - chunk.bytesize) + break unless part + chunk << part + end + body << chunk @socket.read(2) # read \r\n after each chunk end body @@ -304,7 +318,15 @@ def handle_get(path, headers, socket) end def handle_post(path, headers, socket) - body = socket.read(headers['Content-Length'].to_i) + buffer_size = 1024 + remaining_size ||= headers['Content-Length'].to_i + body = "" + while remaining_size > 0 + sz = [buffer_size, remaining_size].min + break unless buf = socket.read(sz) + remaining_size -= buf.bytesize + body << buf + end scheme = headers['X-Request-Scheme'] || 'http' host = @config['host'] port = socket.addr[1]