Unicorn::TeeInput

acts like tee(1) on an input input to provide a input-like stream while providing rewindable semantics through a File/StringIO backing store. On the first pass, the input is only read on demand so your Rack application can use input notification (upload progress and like). This should fully conform to the Rack::Lint::InputWrapper specification on the public API. This class is intended to be a strict interpretation of Rack::Lint::InputWrapper functionality and will not support any deviations from it.

When processing uploads, Unicorn exposes a TeeInput object under “rack.input“ of the Rack environment.

Public Class Methods

client_body_buffer_size() click to toggle source

returns the maximum size of request bodies to buffer in memory, amounts larger than this are buffered to the filesystem

    # File lib/unicorn/tee_input.rb, line 27
27:   def self.client_body_buffer_size
28:     @@client_body_buffer_size
29:   end
client_body_buffer_size=(bytes) click to toggle source

sets the maximum size of request bodies to buffer in memory, amounts larger than this are buffered to the filesystem

    # File lib/unicorn/tee_input.rb, line 21
21:   def self.client_body_buffer_size=(bytes)
22:     @@client_body_buffer_size = bytes
23:   end
new(socket, request) click to toggle source

Initializes a new TeeInput object. You normally do not have to call this unless you are writing an HTTP server.

    # File lib/unicorn/tee_input.rb, line 33
33:   def initialize(socket, request)
34:     @len = request.content_length
35:     super
36:     @tmp = @len && @len <= @@client_body_buffer_size ?
37:            StringIO.new("") : Unicorn::TmpIO.new
38:   end

Public Instance Methods

gets => string or nil click to toggle source

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.

    # File lib/unicorn/tee_input.rb, line 96
96:   def gets
97:     @socket ? tee(super) : @tmp.gets
98:   end
read([length [, buffer ]]) => string, buffer, or nil click to toggle source

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).

    # File lib/unicorn/tee_input.rb, line 83
83:   def read(*args)
84:     @socket ? tee(super) : @tmp.read(*args)
85:   end
rewind => 0 click to toggle source

Positions the ios pointer to the beginning of input, returns the offset (zero) of the ios pointer. Subsequent reads will start from the beginning of the previously-buffered input.

     # File lib/unicorn/tee_input.rb, line 106
106:   def rewind
107:     return 0 if 0 == @tmp.size
108:     consume! if @socket
109:     @tmp.rewind # Rack does not specify what the return value is here
110:   end
size => Integer click to toggle source

Returns the size of the input. For requests with a Content-Length header value, this will not read data off the socket and just return the value of the Content-Length header as an Integer.

For Transfer-Encoding:chunked requests, this requires consuming all of the input stream before returning since there’s no other way to determine the size of the request body beforehand.

This method is no longer part of the Rack specification as of Rack 1.2, so its use is not recommended. This method only exists for compatibility with Rack applications designed for Rack 1.1 and earlier. Most applications should only need to call read with a specified length in a loop until it returns nil.

    # File lib/unicorn/tee_input.rb, line 56
56:   def size
57:     @len and return @len
58:     pos = @tmp.pos
59:     consume!
60:     @tmp.pos = pos
61:     @len = @tmp.size
62:   end

Private Instance Methods

consume!() click to toggle source

consumes the stream of the socket

     # File lib/unicorn/tee_input.rb, line 115
115:   def consume!
116:     junk = ""
117:     nil while read(@@io_chunk_size, junk)
118:   end
tee(buffer) click to toggle source
     # File lib/unicorn/tee_input.rb, line 120
120:   def tee(buffer)
121:     if buffer && buffer.size > 0
122:       @tmp.write(buffer)
123:     end
124:     buffer
125:   end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.