yahns Ruby server user/dev discussion
 help / color / mirror / code / Atom feed
fadbddcd92ba7071ee27ad28bb96897b1d31b843 blob 1856 bytes (raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
 
# -*- encoding: binary -*-
# Copyright (C) 2009-2013, Eric Wong <normalperson@yhbt.net> et. al.
# License: GPLv2 or later (https://www.gnu.org/licenses/gpl-2.0.txt)

# 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
class Yahns::MaxBody # :nodoc:
  # This is automatically called when used with Rack::Builder#use
  # See Yahns::MaxBody
  def initialize(app, limit)
    Integer === limit or raise ArgumentError, "limit not an Integer"
    @app = app
    @limit = limit
  end

  RACK_INPUT = "rack.input".freeze # :nodoc:
  CONTENT_LENGTH = "CONTENT_LENGTH" # :nodoc:
  HTTP_TRANSFER_ENCODING = "HTTP_TRANSFER_ENCODING" # :nodoc:

  # our main Rack middleware endpoint
  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

  # Rack response returned when there's an error
  def err # :nodoc:
    [ 413, { 'Content-Length' => '0', 'Content-Type' => 'text/plain' }, [] ]
  end

  def limit_input!(env) # :nodoc:
    input = env[RACK_INPUT]
    klass = input.respond_to?(:rewind) ? RewindableWrapper : Wrapper
    env[RACK_INPUT] = klass.new(input, @limit)
  end
end
require_relative 'max_body/wrapper'
require_relative 'max_body/rewindable_wrapper'
debug log:

solving fadbddc ...
found fadbddc in https://yhbt.net/yahns.git

Code repositories for project(s) associated with this inbox:

	../../../yahns.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).