我可以让 Sinatra / Rack 不将整个请求正文读入内存吗?
假设我有一条 Sinatra 路线 ala:
put '/data' do
request.body.read
# ...
end
看起来整个 request.body 都被读入内存。有没有办法在主体进入系统时消耗它,而不是预先将其全部缓冲在 Rack/Sinatra 中?
我发现我可以这样做来读取部分正文,但整个正文似乎仍然被预先读入内存。
put '/data' do
while request.body.read(1024) != nil
# ...
end
# ...
end
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 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.
put '/data' do
while request.body.read(1024) != nil
# ...
end
# ...
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果不修补 Sinatra 和/或 Rack,通常无法避免这种情况。当
request.POST
是 由 Sinatra 调用来创建参数
。但是你可以在 Sinatra 前面放置一个中间件来删除主体:
You cannot avoid this in general without patching Sinatra and/or Rack. It is done by
Rack::Request
whenrequest.POST
is called by Sinatra to createparams
.But you could place a middleware in front of Sinatra to remove the body: