diff --git a/test/net/http/utils.rb b/test/net/http/utils.rb index b9b289d..1cb313e 100644 --- a/test/net/http/utils.rb +++ b/test/net/http/utils.rb @@ -107,6 +107,7 @@ def initialize(method, path, headers, socket) hash[key] = value end if @body && @body.include?('=') end + rescue EOFError end def [](key) @@ -150,13 +151,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