From 4577f8f5ac6c48f9dce3cd5eead793b5886ba1c0 Mon Sep 17 00:00:00 2001 From: Hiroshi SHIBATA Date: Wed, 10 Jul 2024 17:36:59 +0900 Subject: [PATCH] Handle test failure with test_post(TestNetHTTP_v1_2) --- test/net/http/utils.rb | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) 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]