lib/protocol/http/body/stream.rb in protocol-http-0.23.2 vs lib/protocol/http/body/stream.rb in protocol-http-0.23.3
- old
+ new
@@ -39,102 +39,107 @@
end
attr :input
attr :output
- # rack.hijack_io must respond to:
- # read, write, read_nonblock, write_nonblock, flush, close, close_read, close_write, closed?
-
- # 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)
- return '' if length == 0
+ # This provides a read-only interface for data, which is surprisingly tricky to implement correctly.
+ module Reader
+ # rack.hijack_io must respond to:
+ # read, write, read_nonblock, write_nonblock, flush, close, close_read, close_write, closed?
- buffer ||= Async::IO::Buffer.new
+ # 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)
+ return '' if length == 0
+
+ buffer ||= Async::IO::Buffer.new
- # Take any previously buffered data and replace it into the given buffer.
- if @buffer
- buffer.replace(@buffer)
- @buffer = nil
+ # Take any previously buffered data and replace it into the given buffer.
+ if @buffer
+ buffer.replace(@buffer)
+ @buffer = nil
+ end
+
+ if length
+ while buffer.bytesize < length and chunk = read_next
+ buffer << chunk
+ end
+
+ # This ensures the subsequent `slice!` works correctly.
+ buffer.force_encoding(Encoding::BINARY)
+
+ # This will be at least one copy:
+ @buffer = buffer.byteslice(length, buffer.bytesize)
+
+ # This should be zero-copy:
+ buffer.slice!(length, buffer.bytesize)
+
+ if buffer.empty?
+ return nil
+ else
+ return buffer
+ end
+ else
+ while chunk = read_next
+ buffer << chunk
+ end
+
+ return buffer
+ end
end
- if length
- while buffer.bytesize < length and chunk = read_next
- buffer << chunk
+ # Read at most `length` bytes from the stream. Will avoid reading from the underlying stream if possible.
+ def read_partial(length = nil)
+ if @buffer
+ buffer = @buffer
+ @buffer = nil
+ else
+ buffer = read_next
end
- # This ensures the subsequent `slice!` works correctly.
- buffer.force_encoding(Encoding::BINARY)
+ if buffer and length
+ if buffer.bytesize > length
+ # This ensures the subsequent `slice!` works correctly.
+ buffer.force_encoding(Encoding::BINARY)
- # This will be at least one copy:
- @buffer = buffer.byteslice(length, buffer.bytesize)
+ @buffer = buffer.byteslice(length, buffer.bytesize)
+ buffer.slice!(length, buffer.bytesize)
+ end
+ end
- # This should be zero-copy:
- buffer.slice!(length)
+ return buffer
+ end
+
+ def read_nonblock(length, buffer = nil)
+ @buffer ||= read_next
+ chunk = nil
- if buffer.empty?
- return nil
+ unless @buffer
+ buffer&.clear
+ return
+ end
+
+ if @buffer.bytesize > length
+ chunk = @buffer.byteslice(0, length)
+ @buffer = @buffer.byteslice(length, @buffer.bytesize)
else
- return buffer
+ chunk = @buffer
+ @buffer = nil
end
- else
- while chunk = read_next
- buffer << chunk
+
+ if buffer
+ buffer.replace(chunk)
+ else
+ buffer = chunk
end
return buffer
end
end
- # Read at most `length` bytes from the stream. Will avoid reading from the underlying stream if possible.
- def read_partial(length = nil)
- if @buffer
- buffer = @buffer
- @buffer = nil
- else
- buffer = read_next
- end
-
- if buffer and length
- if buffer.bytesize > length
- # This ensures the subsequent `slice!` works correctly.
- buffer.force_encoding(Encoding::BINARY)
-
- @buffer = buffer.byteslice(length, buffer.bytesize)
- buffer.slice!(length)
- end
- end
-
- return buffer
- end
-
- def read_nonblock(length, buffer = nil)
- @buffer ||= read_next
- chunk = nil
-
- unless @buffer
- buffer&.clear
- return
- end
-
- if @buffer.bytesize > length
- chunk = @buffer.byteslice(0, length)
- @buffer = @buffer.byteslice(length, @buffer.bytesize)
- else
- chunk = @buffer
- @buffer = nil
- end
-
- if buffer
- buffer.replace(chunk)
- else
- buffer = chunk
- end
-
- return buffer
- end
+ include Reader
def write(buffer)
if @output
@output.write(buffer)
return buffer.bytesize