# Copyright, 2019, 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_relative'writable'moduleAsyncmoduleHTTPmoduleBody# 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.classStreamdefinitialize(input,output=Writable.new)@input=input@output=output# Will hold remaining data in `#read`.@buffer=nil@closed=falseendattr:inputattr: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 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>0returnbufferenddefread_nonblock(length,buffer=nil)@buffer||=read_nextchunk=nilif@buffer.bytesize>lengthchunk=@buffer.byteslice(0,length)@buffer=@buffer.byteslice(length,@buffer.bytesize)elsechunk=@buffer@buffer=nilendifbufferbuffer.replace(chunk)elsebuffer=chunkendreturnbufferenddefwrite(buffer)@output.write(buffer)endaliaswrite_nonblockwritedefflushenddefclose_read@input&.closeend# close must never be called on the input stream. huh?defclose_write@output&.closeend# Close the input and output bodies.defcloseself.close_readself.close_writeensure@closed=trueend# Whether the stream has been closed.defclosed?@closedend# Whether there are any output chunks remaining?defempty?@output.empty?endprivatedefread_nextifchunk=@input&.readreturnchunkelse@input=nilreturnnilendendendendendend