lib/falcon/adapters/input.rb in falcon-0.17.1 vs lib/falcon/adapters/input.rb in falcon-0.17.4
- old
+ new
@@ -26,12 +26,12 @@
# The input stream is an IO-like object which contains the raw HTTP POST data. When applicable, its external encoding must be “ASCII-8BIT” and it must be opened in binary mode, for Ruby 1.9 compatibility. The input stream must respond to gets, each, read and rewind.
class Input
def initialize(body)
@body = body
- # The current buffer, which is extended by calling `#fill_buffer`.
- @buffer = Async::IO::BinaryString.new
+ # Will hold remaining data in `#read`.
+ @buffer = nil
@finished = @body.nil?
end
attr :body
@@ -47,11 +47,11 @@
# rewind must be called without arguments. It rewinds the input stream back to the beginning. It must not raise Errno::ESPIPE: that is, it may not be a pipe or a socket. Therefore, handler developers must buffer the input data into some rewindable object if the underlying input stream is not rewindable.
def rewind
if @body
# If the body is not rewindable, this will fail.
@body.rewind
- @buffer.clear
+ @buffer = nil
@finished = false
end
end
def respond_to?(name, *)
@@ -65,53 +65,45 @@
# read behaves like IO#read. Its signature is read([length, [buffer]]). If given, length must be a non-negative Integer (>= 0) or nil, and buffer must be a String and may not be nil. If length is given and not nil, then this method reads at most length bytes from the input stream. If length is not given or nil, then this method reads all data until EOF. When EOF is reached, this method returns nil if length is given and not nil, or “” if length is not given or is nil. If buffer is given, then the read data will be placed into buffer instead of a newly created String object.
# @param length [Integer] the amount of data to read
# @param buffer [String] the buffer which will receive the data
# @return a buffer containing the data
def read(length = nil, buffer = nil)
- if length
- fill_buffer(length) if @buffer.bytesize <= length
+ buffer ||= Async::IO::BinaryString.new
+ buffer.clear
+
+ until buffer.bytesize == length
+ @buffer = read_next if @buffer.nil?
+ break if @buffer.nil?
- chunk = @buffer.slice!(0, length)
+ remaining_length = length - buffer.bytesize if length
- if buffer
- # TODO https://bugs.ruby-lang.org/issues/14745
- buffer.replace(chunk)
+ if remaining_length && remaining_length < @buffer.bytesize
+ buffer << @buffer.byteslice(0, remaining_length)
+ @buffer = @buffer.byteslice(remaining_length..-1)
else
- buffer = chunk
+ buffer << @buffer
+ @buffer = nil
end
-
- if buffer.empty? and length > 0
- return nil
- else
- return buffer
- end
- else
- buffer ||= Async::IO::BinaryString.new
-
- buffer.replace(@buffer)
- @buffer.clear
-
- while chunk = read_next
- buffer << chunk
- end
-
- return buffer
end
+
+ return nil if buffer.empty? && length && length > 0
+
+ return buffer
end
def eof?
- @finished and @buffer.empty?
+ @finished and @buffer.nil?
end
# gets must be called without arguments and return a string, or nil on EOF.
# @return [String, nil] The next chunk from the body.
def gets
- if @buffer.empty?
+ if @buffer.nil?
return read_next
else
- buffer = @buffer.dup
- @buffer.clear
+ buffer = @buffer
+ @buffer = nil
return buffer
end
end
# close must never be called on the input stream. huh?
@@ -127,15 +119,9 @@
if chunk = @body.read
return chunk
else
@finished = true
return nil
- end
- end
-
- def fill_buffer(length)
- while @buffer.bytesize < length and chunk = read_next
- @buffer << chunk
end
end
end
end
end