Class: Pitchfork::StreamInput
- Inherits:
-
Object
- Object
- Pitchfork::StreamInput
- Defined in:
- lib/pitchfork/stream_input.rb
Overview
When processing uploads, pitchfork may expose a StreamInput object under “rack.input” of the Rack environment when Pitchfork::Configurator#rewindable_input is set to false
Direct Known Subclasses
Constant Summary collapse
- @@io_chunk_size =
The I/O chunk size (in
bytes
) for I/O operations where the size cannot be user-specified when a method is called. The default is 16 kilobytes. Pitchfork::Const::CHUNK_SIZE
Instance Method Summary collapse
-
#each ⇒ Object
:call-seq: ios.each { |line| block } => ios.
-
#gets(sep = $/) ⇒ Object
:call-seq: ios.gets => string or nil.
-
#initialize(socket, request) ⇒ StreamInput
constructor
Initializes a new StreamInput object.
-
#read(length = nil, rv = ''.b) ⇒ Object
:call-seq: ios.read([length [, buffer ]]) => string, buffer, or nil.
Constructor Details
#initialize(socket, request) ⇒ StreamInput
Initializes a new StreamInput object. You normally do not have to call this unless you are writing an HTTP server.
16 17 18 19 20 21 22 23 24 |
# File 'lib/pitchfork/stream_input.rb', line 16 def initialize(socket, request) # :nodoc: @chunked = request.content_length.nil? @socket = socket @parser = request @buf = request.buf @rbuf = +'' @bytes_read = 0 filter_body(@rbuf, @buf) unless @buf.empty? end |
Instance Method Details
#each ⇒ Object
:call-seq:
ios.each { |line| block } => ios
Executes the block for every “line” in ios, where lines are separated by the global record separator ($/, typically “n”).
102 103 104 105 106 107 108 |
# File 'lib/pitchfork/stream_input.rb', line 102 def each while line = gets yield line end self # Rack does not specify what the return value is here end |
#gets(sep = $/) ⇒ Object
:call-seq:
ios.gets => string or nil
Reads the next “line” from the I/O stream; lines are separated by the global record separator ($/, typically “n”). A global record separator of nil reads the entire unread contents of ios. Returns nil if called at the end of file. This takes zero arguments for strict Rack::Lint compatibility, unlike IO#gets.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/pitchfork/stream_input.rb', line 81 def gets(sep = $/) if sep.nil? read_all(rv = ''.b) return rv.empty? ? nil : rv end re = /\A(.*?#{Regexp.escape(sep)})/ begin @rbuf.sub!(re, ''.b) and return $1 return @rbuf.empty? ? nil : @rbuf.slice!(0, @rbuf.size) if eof? @socket.readpartial(@@io_chunk_size, @buf) or eof! filter_body(once = ''.b, @buf) @rbuf << once end while true end |
#read(length = nil, rv = ''.b) ⇒ Object
:call-seq:
ios.read([length [, buffer ]]) => string, buffer, or nil
Reads at most length bytes from the I/O stream, or to the end of file if length is omitted or is nil. length must be a non-negative integer or nil. If the optional buffer argument is present, it must reference a String, which will receive the data.
At end of file, it returns nil or ” depend on length. ios.read() and ios.read(nil) returns ”. ios.read(length [, buffer]) returns nil.
If the Content-Length of the HTTP request is known (as is the common case for POST requests), then ios.read(length [, buffer]) will block until the specified length is read (or it is the last chunk). Otherwise, for uncommon “Transfer-Encoding: chunked” requests, ios.read(length [, buffer]) will return immediately if there is any data and only block when nothing is available (providing IO#readpartial semantics).
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/pitchfork/stream_input.rb', line 45 def read(length = nil, rv = ''.b) if length if length <= @rbuf.size length < 0 and raise ArgumentError, "negative length #{length} given" rv.replace(@rbuf.slice!(0, length)) else to_read = length - @rbuf.size rv.replace(@rbuf.slice!(0, @rbuf.size)) until to_read == 0 || eof? || (rv.size > 0 && @chunked) begin @socket.readpartial(to_read, @buf) rescue EOFError eof! end filter_body(@rbuf, @buf) rv << @rbuf to_read -= @rbuf.size end @rbuf.clear end rv = nil if rv.empty? && length != 0 else read_all(rv) end rv end |