class Input < ::Protocol::HTTP::Body::Readable
- Inherits from
::Protocol::HTTP::Body::Readable
A request body reconstructed from input channel events.
Definitions
def initialize(channel, headers, metadata)
Initialize an input body.
Signature
-
parameter
channelChannel The input channel.
-
parameter
headersProtocol::HTTP::Headers The request headers to receive trailers.
-
parameter
metadataHash The original body metadata.
Implementation
def initialize(channel, headers, metadata)
@channel = channel
@headers = headers
@length = metadata[:length]
@stream = metadata[:stream]
@finished = false
@closed = false
end
attr :length
Signature
-
attribute
Integer | Nil The original body length.
def stream?
Signature
-
returns
Boolean Whether the original body preferred streaming.
Implementation
def stream?
@stream
end
def empty?
Signature
-
returns
Boolean Whether the body has finished.
Implementation
def empty?
@finished
end
def read
Read the next request body chunk.
Signature
-
returns
String | Nil The next chunk, or
nilafter body completion.
Implementation
def read
return nil if @closed || @finished
loop do
message = @channel.read
raise ClosedError unless message
type, payload = message
case type
when :chunk
return payload
when :trailers
apply_trailers(payload)
when :end
@finished = true
return nil
when :error
@finished = true
raise RemoteError.new(payload)
else
raise ClosedError, "Unexpected input event: #{type.inspect}!"
end
end
end
def close(error = nil)
Close the request body.
A body closed after EOF leaves the channel available for a subsequent upgraded stream.
Implementation
def close(error = nil)
return if @closed
@closed = true
@channel.close_read unless @finished
end