streaming input for large requests

Eric Wong normalperson at
Tue Aug 10 19:25:27 EDT 2010

John Leach <john at> wrote:
> Hi,
> I'm looking to be able to get access to the request body as it is
> available on the socket, so I can process uploads on the fly, as they
> stream in.

Hi John,

Cool!  If you need some example code, you should check out upr,  Sadly the demo machine is down, but the one
application I helped somebody write (on a private LAN somewhere) still
works well :)

There's also the test/examples in the Rainbows! source tree:


> But to be rewindable, I'm assuming they're being stored somewhere?  I'd
> like to be able to handle huge request bodies bit by bit without having
> them written to disk (or worse, stored in ram!).  Is there some way to
> do this?

Yes, we store uploads to an unlinked temporary file if the body is
larger than 112 Kbytes (this threshold was established by Mongrel back
in the day).

Rack currently requires rewindability, but this requirement will
most likely be optional in Rack 2.x, and we'll update our code
to match, then.

Meanwhile, you can either:

1. Write a module to disable writes to tmp for the Unicorn::TeeInput
   class (or monkey patch it) it.

2. Without loading Rack::Lint (or anything that wraps env["rack.input"]):
   Redirect the temporary file to /dev/null:

    input = env["rack.input"]
    if input.respond_to?(:tmp)
      tmp = input.tmp
      # StringIO is used for bodies <112K, can't reopen those
      tmp.respond_to?(:reopen) and tmp.reopen('/dev/null', 'wb')

Eric Wong

More information about the rainbows-talk mailing list