Class: Rack::ContentLengthChecker
- Inherits:
-
Object
- Object
- Rack::ContentLengthChecker
- Defined in:
- lib/rack/content_length_checker.rb
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, warn: {}, fatal: {}, logger: ::Logger.new(STDOUT)) ⇒ Object
constructor
Object.
Constructor Details
#initialize(app, warn: {}, fatal: {}, logger: ::Logger.new(STDOUT)) ⇒ Object
Returns Object.
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/rack/content_length_checker.rb', line 11 def initialize(app, warn: {}, fatal: {}, logger: ::Logger.new(STDOUT)) @app = app @logger = logger @warn = warn @fatal = fatal @warn_length = @warn[:length].to_i @fatal_length = @fatal[:length] ? @fatal[:length].to_i : Float::INFINITY raise ArgumentError, 'warn length is greater than 0' if @warn_length < 0 raise ArgumentError, 'fatal length is greater than 1' if @fatal_length < 1 raise ArgumentError, 'Warn length is smaller than fatal one.' if ((!@fatal.empty?) && (@warn[:length].to_i > @fatal[:length].to_i)) end |
Instance Method Details
#call(env) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/rack/content_length_checker.rb', line 23 def call(env) request_length = env['CONTENT_LENGTH'].to_i error_status = 413 error_headers = {'Content-Type' => 'text/plain'} error_body = "Request Entity Too Large : #{request_length} bytes" = "remote_addr:#{env['HTTP_X_FORWARDED_FOR']}\tmethod:#{env['REQUEST_METHOD']}\turi:#{env['REQUEST_URI']}\tmsg:#{error_body}" case request_length when @warn_length..@fatal_length-1 unless @warn.empty? @logger.warn() return [error_status, error_headers, [error_body]] if @warn[:is_error] end when @fatal_length..Float::INFINITY unless @fatal.empty? @logger.fatal() return [error_status, error_headers, [error_body]] if @fatal[:is_error] end end status, headers, body = @app.call(env) [status, headers, body] end |