Class: Falcon::Adapters::Input
- Inherits:
-
Object
- Object
- Falcon::Adapters::Input
- Defined in:
- lib/falcon/adapters/input.rb
Overview
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.
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
Instance Method Summary collapse
-
#close ⇒ Object
close must never be called on the input stream.
-
#each(&block) ⇒ Object
each must be called without arguments and only yield Strings.
- #eof? ⇒ Boolean
-
#gets ⇒ String?
gets must be called without arguments and return a string, or nil on EOF.
-
#initialize(body) ⇒ Input
constructor
A new instance of Input.
-
#read(length = nil, buffer = nil) ⇒ Object
read behaves like IO#read.
- #respond_to?(name) ⇒ Boolean
-
#rewind ⇒ Object
rewind must be called without arguments.
Constructor Details
#initialize(body) ⇒ Input
Returns a new instance of Input.
28 29 30 31 32 33 34 |
# File 'lib/falcon/adapters/input.rb', line 28 def initialize(body) @body = body # Will hold remaining data in `#read`. @buffer = nil @finished = @body.nil? end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
36 37 38 |
# File 'lib/falcon/adapters/input.rb', line 36 def body @body end |
Instance Method Details
#close ⇒ Object
close must never be called on the input stream. huh?
110 111 112 |
# File 'lib/falcon/adapters/input.rb', line 110 def close @body.finish end |
#each(&block) ⇒ Object
each must be called without arguments and only yield Strings.
39 40 41 42 43 44 45 |
# File 'lib/falcon/adapters/input.rb', line 39 def each(&block) return to_enum unless block_given? while chunk = gets yield chunk end end |
#eof? ⇒ Boolean
93 94 95 |
# File 'lib/falcon/adapters/input.rb', line 93 def eof? @finished and @buffer.nil? end |
#gets ⇒ String?
gets must be called without arguments and return a string, or nil on EOF.
99 100 101 102 103 104 105 106 107 |
# File 'lib/falcon/adapters/input.rb', line 99 def gets if @buffer.nil? return read_next else buffer = @buffer @buffer = nil return buffer end end |
#read(length = nil, buffer = nil) ⇒ Object
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.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/falcon/adapters/input.rb', line 69 def read(length = nil, buffer = nil) buffer ||= Async::IO::BinaryString.new buffer.clear until buffer.bytesize == length @buffer = read_next if @buffer.nil? break if @buffer.nil? remaining_length = length - buffer.bytesize if length if remaining_length && remaining_length < @buffer.bytesize buffer << @buffer.byteslice(0, remaining_length) @buffer = @buffer.byteslice(remaining_length..-1) else buffer << @buffer @buffer = nil end end return nil if buffer.empty? && length && length > 0 return buffer end |
#respond_to?(name) ⇒ Boolean
57 58 59 60 61 62 63 |
# File 'lib/falcon/adapters/input.rb', line 57 def respond_to?(name, *) if name == :rewind @body.respond_to?(:rewind) else super end end |
#rewind ⇒ Object
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.
48 49 50 51 52 53 54 55 |
# File 'lib/falcon/adapters/input.rb', line 48 def rewind if @body # If the body is not rewindable, this will fail. @body.rewind @buffer = nil @finished = false end end |