class Rainbows::MaxBody

Middleware used to enforce client_max_body_size for TeeInput users.

There is no need to configure this middleware manually, it will automatically be configured for you based on the client_max_body_size setting.

For more fine-grained control, you may also define it per-endpoint in your Rack config.ru like this:

map "/limit_1M" do
  use Rainbows::MaxBody, 1024*1024
  run MyApp
end
map "/limit_10M" do
  use Rainbows::MaxBody, 1024*1024*10
  run MyApp
end

This is only compatible with concurrency models that expose a streaming “rack.input” to the Rack application. Thus it is NOT compatible with any of the following as they fully buffer the request body before the application dispatch:

However, the global Rainbows::Configurator#client_max_body_size is compatible with all concurrency models Rainbows! supports.

Public Class Methods

new(app, limit = nil) click to toggle source

This is automatically called when used with Rack::Builder#use

# File lib/rainbows/max_body.rb, line 41
def initialize(app, limit = nil)
  case limit
  when Integer, nil
  else
    raise ArgumentError, "limit not an Integer"
  end
  @app, @limit = app, limit
end

Public Instance Methods

call(env) click to toggle source

our main Rack middleware endpoint

# File lib/rainbows/max_body.rb, line 51
def call(env)
  @limit = Rainbows.server.client_max_body_size if nil == @limit
  catch(:rainbows_EFBIG) do
    len = env['CONTENT_LENGTH']
    if len && len.to_i > @limit
      return err
    elsif /\Achunked\z/i =~ env['HTTP_TRANSFER_ENCODING']
      limit_input!(env)
    end
    @app.call(env)
  end || err
end
limit_input!(env) click to toggle source
# File lib/rainbows/max_body.rb, line 85
def limit_input!(env)
  input = env['rack.input']
  klass = input.respond_to?(:rewind) ? RewindableWrapper : Wrapper
  env['rack.input'] = klass.new(input, @limit)
end