# File lib/mongrel.rb, line 194
194:     def initialize(params, initial_body, socket)
195:       @params = params
196:       @socket = socket
197: 
198:       clen = params[Const::CONTENT_LENGTH].to_i - initial_body.length
199: 
200:       if clen > Const::MAX_BODY
201:         @body = Tempfile.new(Const::MONGREL_TMP_BASE)
202:         @body.binmode
203:       else
204:         @body = StringIO.new
205:       end
206: 
207:       begin
208:         @body.write(initial_body)
209: 
210:         # write the odd sized chunk first
211:         clen -= @body.write(@socket.read(clen % Const::CHUNK_SIZE))
212: 
213:         # then stream out nothing but perfectly sized chunks
214:         while clen > 0
215:           data = @socket.read(Const::CHUNK_SIZE)
216:           # have to do it this way since @socket.eof? causes it to block
217:           raise "Socket closed or read failure" if not data or data.length != Const::CHUNK_SIZE
218:           clen -= @body.write(data)
219:           # ASSUME: we are writing to a disk and these writes always write the requested amount
220:         end
221: 
222:         # rewind to keep the world happy
223:         @body.rewind
224:       rescue Object
225:         # any errors means we should delete the file, including if the file is dumped
226:         STDERR.puts "Error reading request: #$!"
227:         @body.delete if @body.class == Tempfile
228:         @body = nil # signals that there was a problem
229:       end
230:     end