[PATCH] Avoid slurping or parsing request body on PUT requests

19 views
Skip to first unread message

Eric Wong

unread,
Oct 17, 2008, 2:43:25 PM10/17/08
to rack-...@googlegroups.com
Uploading a large file via the HTTP PUT method causes
`@env["rack.input"].read' to be called inside the POST method. This
means the entire file is slurped into memory and was needlessly causing
my Sinatra process to eat up 300M+ for some uploads I've been doing.
---

I actually opened a report for this on Rubyforge, but I realized
that I often forget about that on my projects, as well.

http://rubyforge.org/tracker/index.php?func=detail&aid=22346&group_id=3113&atid=12001

lib/rack/request.rb | 2 +-
1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/lib/rack/request.rb b/lib/rack/request.rb
index 9d16f49..2cf1944 100644
--- a/lib/rack/request.rb
+++ b/lib/rack/request.rb
@@ -122,7 +122,7 @@ module Rack

# The union of GET and POST data.
def params
- self.GET.update(self.POST)
+ self.put? ? self.GET : self.GET.update(self.POST)
rescue EOFError => e
self.GET
end
--
Eric Wong

Christian Neukirchen

unread,
Oct 19, 2008, 1:29:33 PM10/19/08
to rack-...@googlegroups.com
Eric Wong <normal...@yhbt.net> writes:

> Uploading a large file via the HTTP PUT method causes
> `@env["rack.input"].read' to be called inside the POST method. This
> means the entire file is slurped into memory and was needlessly causing
> my Sinatra process to eat up 300M+ for some uploads I've been doing.
> ---
>
> I actually opened a report for this on Rubyforge, but I realized
> that I often forget about that on my projects, as well.

Your patch breaks all POST uploads, doesn't it?

If you use a proper multipart (NOT url-encoded!) it should use a tempfile...

--
Christian Neukirchen <chneuk...@gmail.com> http://chneukirchen.org

Eric Wong

unread,
Oct 21, 2008, 4:56:35 PM10/21/08
to Rack Development
On Oct 19, 10:29 am, Christian Neukirchen <chneukirc...@gmail.com>
wrote:
> Eric Wong <normalper...@yhbt.net> writes:
> > Uploading a large file via the HTTP PUT method causes
> > `@env["rack.input"].read' to be called inside the POST method.  This
> > means the entire file is slurped into memory and was needlessly causing
> > my Sinatra process to eat up 300M+ for some uploads I've been doing.
> > ---
>
> >  I actually opened a report for this on Rubyforge, but I realized
> >  that I often forget about that on my projects, as well.
>
> Your patch breaks all POST uploads, doesn't it?
>
> If you use a proper multipart (NOT url-encoded!) it should use a tempfile...

test/spec_rack_request.rb with POST uploads seem to pass. I'm not
sure about users of the MethodOverride hack...

I'm real PUT requests generated with curl, not the MethodOverride hack
via browsers; so none of my requests are multipart.

Christian Neukirchen

unread,
Oct 21, 2008, 6:50:20 PM10/21/08
to rack-...@googlegroups.com
Eric Wong <normal...@gmail.com> writes:

Wouldn't it be easier to just don't call POST then?

Eric Wong

unread,
Oct 22, 2008, 4:25:39 AM10/22/08
to Rack Development
On Oct 21, 3:50 pm, Christian Neukirchen <chneukirc...@gmail.com>
wrote:
You mean the POST method in Rack::Request? That's exactly what my
patch
does inside Rack::Request#params. If a PUT request is detected, then
I
avoid calling Rack::Request#POST.

Ben Bleything

unread,
Nov 28, 2008, 4:22:41 AM11/28/08
to Rack Development
Is there any update on this patch? This problem is biting me right
now and I'd really like to see it resolved :)

Christian Neukirchen

unread,
Nov 28, 2008, 9:59:13 AM11/28/08
to rack-...@googlegroups.com
Ben Bleything <b...@bleything.net> writes:

> Is there any update on this patch? This problem is biting me right
> now and I'd really like to see it resolved :)

Applied now, I misunderstood the patch.

Ben Bleything

unread,
Nov 28, 2008, 12:49:29 PM11/28/08
to Rack Development
On Nov 28, 6:59 am, Christian Neukirchen <chneukirc...@gmail.com>
wrote:
> Applied now, I misunderstood the patch.

Great! Thanks, Christian!
Reply all
Reply to author
Forward
0 new messages