Can I have Sinatra / Rack not read the entire request body into memory?

You cannot avoid this in general without patching Sinatra and/or Rack. It is done by Rack::Request when request. POST is called by Sinatra to create params But you could place a middleware in front of Sinatra to remove the body: require 'sinatra' require 'stringio' use Rack::Config do |env| if env'PATH_INFO' == '/data' and env'REQUEST_METHOD' == 'PUT' env'rack.

Input', env'data. Input' = StringIO. New, env'rack.

Input' end end put '/data' do while request. Env'data. Input'.body.

Read(1024)! = nil # ... end # ... end.

You cannot avoid this in general without patching Sinatra and/or Rack. It is done by Rack::Request when request. POST is called by Sinatra to create params.

But you could place a middleware in front of Sinatra to remove the body: require 'sinatra' require 'stringio' use Rack::Config do |env| if env'PATH_INFO' == '/data' and env'REQUEST_METHOD' == 'PUT' env'rack. Input', env'data. Input' = StringIO.

New, env'rack. Input' end end put '/data' do while request. Env'data.

Input'.body. Read(1024)! = nil # ... end # ... end.

Konstantin - Thanks for the detailed response... Some questions for you an an edit or two and I'll accept your answer. 1. (the edit) In your code fragment, can you please fix StrinIO.

New to be StringIO. New? 2.

If you used Rack to remove the body, won't the entire body still be read by Rack from the network into memory? If that's the case, maybe a small edit to your answer to reflect it? 3.

I haven't seen Ruby like env'rack. Input', env'data. Input' = StrinIO.

New, env'rack. Input' before. Can you please explain in a comment what this code fragment is doing?

Thanks again! – Chris Markle Jun 12 '10 at 17:36 1 a,b = 1,2 assigns 1 to a and 2 to b, so env'rack. Input', env'data.

Input' = StringIO. New, env'rack. Input' sets env'rack.

Input' to an empty StringIO and puts its old value(probably a temp file handle) in env'data. Input' so you can reference it later. – BaroqueBobcat Jun 12 '10 at 21:43 BaroqueBobca is right.

This is pattern is especially common if you want to exchange the values of two variables. Imagine you have a and be and want to set both a = be and be = a. To not lose the old value of a you would have to create a temporary variable, like old = a; a = b; be = c.

Ruby supports another syntax (and so does Python, afaik): a, be = b, a, which avoids the temporary variable, as it first evaluates the right hand (b, a) completely and then does the assignment. – Konstantin Haase Jun 13 '10 at 9:44.

It appears that the entire request. Body is read into memory. Is there a way to consume the body as it comes into the system, rather than having it all buffered in Rack/Sinatra beforehand?

I see I can do this to read the body in parts, but the entire body still seems to be read into memory beforehand.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions