开发者

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

开发者 https://www.devze.com 2023-01-03 00:53 出处:网络
Say I have a Sinatra route ala: put \'/data\' do request.body.read # ... end It appears that the entire request.body is read into memory. Is there a way to consume the body as it comes into the sys

Say I have a Sinatra route ala:

put '/data' do
  request.body.read
  # ...
end

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 Rac开发者_JAVA百科k/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.

put '/data' do
  while request.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
0

精彩评论

暂无评论...
验证码 换一张
取 消