Protocol::RackSourceProtocolRackInput

class Input

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 class Protocol::Rack::Rewindable for more details.

Definitions

def initialize(body)

Initialize the input wrapper.

Signature

parameter body Protocol::HTTP::Body::Readable

Implementation

def initialize(body)
	@body = body
	@closed = false
	
	# Will hold remaining data in `#read`.
	@buffer = nil
end

attr :body

The input body.

Signature

attribute Protocol::HTTP::Body::Readable

def each(&block)

Enumerate chunks of the request body.

Signature

yields {|chunk| ...}
parameter chunk String

Implementation

def each(&block)
	return to_enum unless block_given?
	
	return if closed? 
	
	while chunk = read_partial
		yield chunk
	end
end

def close(error = nil)

Close the input and output bodies.

Implementation

def close(error = nil)
	@closed = true
	
	if @body
		@body.close(error)
		@body = nil
	end
	
	return nil
end

def rewind

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.

Signature

returns Boolean

Whether the body could be rewound.

Implementation

def rewind
	if @body and @body.respond_to?(:rewind)
		# If the body is not rewindable, this will fail.
		@body.rewind
		@buffer = nil
		@finished = false
		
		return true
	end
	
	return false
end

def closed?

Whether the stream has been closed.

Implementation

def closed?
	@closed or @body.nil?
end

def empty?

Whether there are any input chunks remaining?

Implementation

def empty?
	@body.nil?
end