1 # -*- encoding: binary -*-
5 # acts like tee(1) on an input input to provide a input-like stream
6 # while providing rewindable semantics through a File/StringIO backing
7 # store. On the first pass, the input is only read on demand so your
8 # Rack application can use input notification (upload progress and
9 # like). This should fully conform to the Rack::Lint::InputWrapper
10 # specification on the public API. This class is intended to be a
11 # strict interpretation of Rack::Lint::InputWrapper functionality and
12 # will not support any deviations from it.
14 # When processing uploads, Unicorn exposes a TeeInput object under
15 # "rack.input" of the Rack environment.
16 class TeeInput < Struct.new(:socket, :req, :parser, :buf, :len, :tmp, :buf2)
18 # Initializes a new TeeInput object. You normally do not have to call
19 # this unless you are writing an HTTP server.
22 self.len = parser.content_length
23 self.tmp = len && len < Const::MAX_BODY ? StringIO.new("") : Util.tmpio
26 parser.filter_body(buf2, buf) and finalize_input
35 # Returns the size of the input. For requests with a Content-Length
36 # header value, this will not read data off the socket and just return
37 # the value of the Content-Length header as an Integer.
39 # For Transfer-Encoding:chunked requests, this requires consuming
40 # all of the input stream before returning since there's no other
41 # way to determine the size of the request body beforehand.
47 while tee(Const::CHUNK_SIZE, buf2)
56 # ios.read([length [, buffer ]]) => string, buffer, or nil
58 # Reads at most length bytes from the I/O stream, or to the end of
59 # file if length is omitted or is nil. length must be a non-negative
60 # integer or nil. If the optional buffer argument is present, it
61 # must reference a String, which will receive the data.
63 # At end of file, it returns nil or "" depend on length.
64 # ios.read() and ios.read(nil) returns "".
65 # ios.read(length [, buffer]) returns nil.
67 # If the Content-Length of the HTTP request is known (as is the common
68 # case for POST requests), then ios.read(length [, buffer]) will block
69 # until the specified length is read (or it is the last chunk).
70 # Otherwise, for uncommon "Transfer-Encoding: chunked" requests,
71 # ios.read(length [, buffer]) will return immediately if there is
72 # any data and only block when nothing is available (providing
73 # IO#readpartial semantics).
75 socket or return tmp.read(*args)
80 while tee(Const::CHUNK_SIZE, buf2)
86 diff = tmp.size - tmp.pos
88 ensure_length(tee(length, rv), length)
90 ensure_length(tmp.read(diff > length ? length : diff, rv), length)
96 # ios.gets => string or nil
98 # Reads the next ``line'' from the I/O stream; lines are separated
99 # by the global record separator ($/, typically "\n"). A global
100 # record separator of nil reads the entire unread contents of ios.
101 # Returns nil if called at the end of file.
102 # This takes zero arguments for strict Rack::Lint compatibility,
105 socket or return tmp.gets
106 nil == $/ and return read
109 if tmp.pos == orig_size
110 tee(Const::CHUNK_SIZE, buf2) or return nil
114 line = tmp.gets # cannot be nil here since size > pos
115 $/ == line[-$/.size, $/.size] and return line
117 # unlikely, if we got here, then tmp is at EOF
120 tee(Const::CHUNK_SIZE, buf2) or break
123 $/ == line[-$/.size, $/.size] and return line
124 # tmp is at EOF again here, retry the loop
131 # ios.each { |line| block } => ios
133 # Executes the block for every ``line'' in *ios*, where lines are
134 # separated by the global record separator ($/, typically "\n").
140 self # Rack does not specify what the return value is here
146 # Positions the *ios* pointer to the beginning of input, returns
147 # the offset (zero) of the +ios+ pointer. Subsequent reads will
148 # start from the beginning of the previously-buffered input.
150 tmp.rewind # Rack does not specify what the return value is here
158 # in case client only did a premature shutdown(SHUT_WR)
159 # we do support clients that shutdown(SHUT_WR) after the
160 # _entire_ request has been sent, and those will not have
161 # raised EOFError on us.
162 socket.close if socket
163 raise ClientShutdown, "bytes_read=#{tmp.size}", []
170 # tees off a +length+ chunk of data from the input into the IO
171 # backing store as well as returning it. +dst+ must be specified.
172 # returns nil if reading from the input returns nil
174 unless parser.body_eof?
175 if parser.filter_body(dst, socket.readpartial(length, buf)).nil?
177 tmp.seek(0, IO::SEEK_END) # workaround FreeBSD/OSX + MRI 1.8.x bug
187 while parser.trailers(req, buf).nil?
188 # Don't worry about raising ClientShutdown here on EOFError, tee()
189 # will catch EOFError when app is processing it, otherwise in
190 # initialize we never get any chance to enter the app so the
191 # EOFError will just get trapped by Unicorn and not the Rack app
192 buf << socket.readpartial(Const::CHUNK_SIZE)
197 # tee()s into +dst+ until it is of +length+ bytes (or until
198 # we've reached the Content-Length of the request body).
199 # Returns +dst+ (the exact object, not a duplicate)
200 # To continue supporting applications that need near-real-time
201 # streaming input bodies, this is a no-op for
202 # "Transfer-Encoding: chunked" requests.
203 def ensure_length(dst, length)
204 # len is nil for chunked bodies, so we can't ensure length for those
205 # since they could be streaming bidirectionally and we don't want to
206 # block the caller in that case.
207 return dst if dst.nil? || len.nil?
209 while dst.size < length && tee(length - dst.size, buf2)