Class: Legion::API::Middleware::BodyLimit

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/api/middleware/body_limit.rb

Constant Summary collapse

MAX_BODY_SIZE =

1MB

1_048_576

Instance Method Summary collapse

Constructor Details

#initialize(app, max_size: MAX_BODY_SIZE) ⇒ BodyLimit

Returns a new instance of BodyLimit.



11
12
13
14
# File 'lib/legion/api/middleware/body_limit.rb', line 11

def initialize(app, max_size: MAX_BODY_SIZE)
  @app = app
  @max_size = max_size
end

Instance Method Details

#call(env) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/legion/api/middleware/body_limit.rb', line 16

def call(env)
  content_length = env['CONTENT_LENGTH'].to_i
  if content_length > @max_size
    if defined?(Legion::Logging)
      Legion::Logging.warn "API body limit exceeded: #{content_length} bytes > #{@max_size} for #{env['REQUEST_METHOD']} #{env['PATH_INFO']}"
    end
    body = Legion::JSON.dump({
                               error: { code:    'payload_too_large',
                                        message: "request body exceeds #{@max_size} bytes" },
                               meta:  { timestamp: Time.now.utc.iso8601 }
                             })
    return [413, { 'content-type' => 'application/json' }, [body]]
  end
  @app.call(env)
end