Class: Yahns::MaxBody

Inherits:
Object
  • Object
show all
Defined in:
lib/yahns/max_body.rb

Overview

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 Yahns::MaxBody, 1024*1024
  run MyApp
end
map "/limit_10M" do
  use Yahns::MaxBody, 1024*1024*10
  run MyApp
end

Defined Under Namespace

Classes: RewindableWrapper, Wrapper

Constant Summary collapse

RACK_INPUT =

:nodoc:

"rack.input".freeze
CONTENT_LENGTH =

:nodoc:

"CONTENT_LENGTH"
HTTP_TRANSFER_ENCODING =

:nodoc:

"HTTP_TRANSFER_ENCODING"

Instance Method Summary collapse

Constructor Details

#initialize(app, limit) ⇒ MaxBody

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



25
26
27
28
29
# File 'lib/yahns/max_body.rb', line 25

def initialize(app, limit)
  Integer === limit or raise ArgumentError, "limit not an Integer"
  @app = app
  @limit = limit
end

Instance Method Details

#call(env) ⇒ Object

our main Rack middleware endpoint



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/yahns/max_body.rb', line 36

def call(env) # :nodoc:
  catch(:yahns_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

#errObject

Rack response returned when there’s an error



49
50
51
# File 'lib/yahns/max_body.rb', line 49

def err # :nodoc:
  [ 413, { 'Content-Length' => '0', 'Content-Type' => 'text/plain' }, [] ]
end

#limit_input!(env) ⇒ Object

:nodoc:



53
54
55
56
57
# File 'lib/yahns/max_body.rb', line 53

def limit_input!(env) # :nodoc:
  input = env[RACK_INPUT]
  klass = input.respond_to?(:rewind) ? RewindableWrapper : Wrapper
  env[RACK_INPUT] = klass.new(input, @limit)
end