simplify initialization of request buffer

This commit is contained in:
HoneyryderChuck 2023-10-23 17:54:11 +01:00
parent d5e469d6c6
commit c5fc8aeb19

View File

@ -13,7 +13,7 @@ module HTTPX
@threshold_size = threshold_size
@bytesize = bytesize
@encoding = encoding
try_upgrade_buffer
@buffer = StringIO.new("".b)
super(@buffer)
end
@ -66,23 +66,20 @@ module HTTPX
# initializes the buffer into a StringIO, or turns it into a Tempfile when the threshold
# has been reached.
def try_upgrade_buffer
if !@buffer.is_a?(Tempfile) && @bytesize > @threshold_size
aux = @buffer
return unless @bytesize > @threshold_size
@buffer = Tempfile.new("httpx", encoding: Encoding::BINARY, mode: File::RDWR)
return if @buffer.is_a?(Tempfile)
if aux
aux.rewind
::IO.copy_stream(aux, @buffer)
aux.close
end
aux = @buffer
else
return if @buffer
@buffer = StringIO.new("".b)
@buffer = Tempfile.new("httpx", encoding: Encoding::BINARY, mode: File::RDWR)
if aux
aux.rewind
::IO.copy_stream(aux, @buffer)
aux.close
end
__setobj__(@buffer)
end