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