# frozen_string_literal: true# Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com># # Permission is hereby granted, free of charge, to any person obtaining a copy# of this software and associated documentation files (the "Software"), to deal# in the Software without restriction, including without limitation the rights# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell# copies of the Software, and to permit persons to whom the Software is# furnished to do so, subject to the following conditions:# # The above copyright notice and this permission notice shall be included in# all copies or substantial portions of the Software.# # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN# THE SOFTWARE.require'async/io/buffer'require'protocol/http/body/rewindable'moduleFalconmoduleAdapters# Wraps a streaming input body into the interface required by `rack.input`.## 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`.## This implementation is not always rewindable, to avoid buffering the input when handling large uploads. See {Rewindable} for more details.classInput# Initialize the input wrapper.# @parameter body [Protocol::HTTP::Body::Readable]definitialize(body)@body=body# Will hold remaining data in `#read`.@buffer=nil@finished=@body.nil?end# The input body.# @attribute [Protocol::HTTP::Body::Readable]attr:body# Enumerate chunks of the request body.# @yields {|chunk| ...}# @parameter chunk [String]defeach(&block)returnto_enumunlessblock_given?whilechunk=getsyieldchunkendend# Rewind the input stream back to the start.## `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.## @returns [Boolean] Whether the body could be rewound.defrewindif@bodyand@body.respond_to?:rewind# If the body is not rewindable, this will fail.@body.rewind@buffer=nil@finished=falsereturntrueendreturnfalseend# Read data from the input stream.# # `read` behaves like `IO#read`. Its signature is `read(length = nil, buffer = nil)`. 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. When the end is reached, this method returns `nil` if `length` is given and not `nil`, or an empty `String` if `length` is not given or is `nil`. If `buffer` is given, then the read data will be placed into the `buffer` instead of a newly created `String` object.## @parameter length [Integer] the amount of data to read# @parameter buffer [String] the buffer which will receive the data# @returns a buffer containing the datadefread(length=nil,buffer=nil)buffer||=Async::IO::Buffer.newbuffer.clearuntilbuffer.bytesize==length@buffer=read_nextif@buffer.nil?breakif@buffer.nil?remaining_length=length-buffer.bytesizeiflengthifremaining_length&&remaining_length<@buffer.bytesize# We know that we are not going to reuse the original buffer.# But byteslice will generate a hidden copy. So let's freeze it first:@buffer.freezebuffer<<@buffer.byteslice(0,remaining_length)@buffer=@buffer.byteslice(remaining_length,@buffer.bytesize)elsebuffer<<@buffer@buffer=nilendendreturnnilifbuffer.empty?&&length&&length>0returnbufferend# Has the input stream been read completely?# @returns [Boolean]defeof?@finishedand@buffer.nil?end# Read the next chunk of data from the input stream.## `gets` must be called without arguments and return a `String`, or `nil` when the input stream has no more data.## @returns [String | Nil] The next chunk from the body.defgetsif@buffer.nil?returnread_nextelsebuffer=@buffer@buffer=nilreturnbufferendend# Close and discard the remainder of the input stream.defclose@body&.closeendprivatedefread_nextreturnnilif@finishedifchunk=@body.readreturnchunkelse@finished=truereturnnilendendendendend