puma 2.1.1 → 7.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (125) hide show
  1. checksums.yaml +7 -0
  2. data/History.md +3273 -0
  3. data/LICENSE +23 -20
  4. data/README.md +388 -100
  5. data/bin/puma-wild +25 -0
  6. data/bin/pumactl +1 -1
  7. data/docs/architecture.md +74 -0
  8. data/docs/compile_options.md +55 -0
  9. data/docs/deployment.md +137 -0
  10. data/docs/fork_worker.md +41 -0
  11. data/docs/images/puma-connection-flow-no-reactor.png +0 -0
  12. data/docs/images/puma-connection-flow.png +0 -0
  13. data/docs/images/puma-general-arch.png +0 -0
  14. data/docs/java_options.md +54 -0
  15. data/docs/jungle/README.md +9 -0
  16. data/docs/jungle/rc.d/README.md +74 -0
  17. data/docs/jungle/rc.d/puma +61 -0
  18. data/docs/jungle/rc.d/puma.conf +10 -0
  19. data/docs/kubernetes.md +73 -0
  20. data/docs/nginx.md +5 -5
  21. data/docs/plugins.md +42 -0
  22. data/docs/rails_dev_mode.md +28 -0
  23. data/docs/restart.md +65 -0
  24. data/docs/signals.md +98 -0
  25. data/docs/stats.md +148 -0
  26. data/docs/systemd.md +253 -0
  27. data/docs/testing_benchmarks_local_files.md +150 -0
  28. data/docs/testing_test_rackup_ci_files.md +36 -0
  29. data/ext/puma_http11/PumaHttp11Service.java +2 -2
  30. data/ext/puma_http11/extconf.rb +59 -2
  31. data/ext/puma_http11/http11_parser.c +319 -487
  32. data/ext/puma_http11/http11_parser.h +15 -13
  33. data/ext/puma_http11/http11_parser.java.rl +27 -43
  34. data/ext/puma_http11/http11_parser.rl +27 -24
  35. data/ext/puma_http11/http11_parser_common.rl +8 -8
  36. data/ext/puma_http11/mini_ssl.c +696 -38
  37. data/ext/puma_http11/no_ssl/PumaHttp11Service.java +15 -0
  38. data/ext/puma_http11/org/jruby/puma/Http11.java +145 -113
  39. data/ext/puma_http11/org/jruby/puma/Http11Parser.java +139 -172
  40. data/ext/puma_http11/org/jruby/puma/MiniSSL.java +413 -193
  41. data/ext/puma_http11/puma_http11.c +183 -175
  42. data/lib/puma/app/status.rb +77 -29
  43. data/lib/puma/binder.rb +349 -119
  44. data/lib/puma/cli.rb +164 -769
  45. data/lib/puma/client.rb +591 -132
  46. data/lib/puma/cluster/worker.rb +183 -0
  47. data/lib/puma/cluster/worker_handle.rb +127 -0
  48. data/lib/puma/cluster.rb +634 -0
  49. data/lib/puma/cluster_accept_loop_delay.rb +91 -0
  50. data/lib/puma/commonlogger.rb +115 -0
  51. data/lib/puma/configuration.rb +370 -202
  52. data/lib/puma/const.rb +266 -115
  53. data/lib/puma/control_cli.rb +219 -120
  54. data/lib/puma/detect.rb +45 -2
  55. data/lib/puma/dsl.rb +1496 -0
  56. data/lib/puma/error_logger.rb +115 -0
  57. data/lib/puma/events.rb +44 -60
  58. data/lib/puma/io_buffer.rb +48 -5
  59. data/lib/puma/jruby_restart.rb +2 -51
  60. data/lib/puma/json_serialization.rb +96 -0
  61. data/lib/puma/launcher/bundle_pruner.rb +104 -0
  62. data/lib/puma/launcher.rb +502 -0
  63. data/lib/puma/log_writer.rb +147 -0
  64. data/lib/puma/minissl/context_builder.rb +96 -0
  65. data/lib/puma/minissl.rb +355 -37
  66. data/lib/puma/null_io.rb +79 -12
  67. data/lib/puma/plugin/systemd.rb +90 -0
  68. data/lib/puma/plugin/tmp_restart.rb +36 -0
  69. data/lib/puma/plugin.rb +111 -0
  70. data/lib/puma/rack/builder.rb +297 -0
  71. data/lib/puma/rack/urlmap.rb +93 -0
  72. data/lib/puma/rack_default.rb +21 -4
  73. data/lib/puma/reactor.rb +102 -133
  74. data/lib/puma/request.rb +703 -0
  75. data/lib/puma/runner.rb +211 -0
  76. data/lib/puma/sd_notify.rb +146 -0
  77. data/lib/puma/server.rb +544 -436
  78. data/lib/puma/single.rb +72 -0
  79. data/lib/puma/state_file.rb +69 -0
  80. data/lib/puma/thread_pool.rb +282 -56
  81. data/lib/puma/util.rb +125 -0
  82. data/lib/puma.rb +80 -6
  83. data/lib/rack/handler/puma.rb +125 -47
  84. data/tools/Dockerfile +26 -0
  85. data/tools/trickletest.rb +44 -0
  86. metadata +77 -148
  87. data/COPYING +0 -55
  88. data/Gemfile +0 -10
  89. data/History.txt +0 -291
  90. data/Manifest.txt +0 -60
  91. data/Rakefile +0 -121
  92. data/TODO +0 -5
  93. data/docs/config.md +0 -0
  94. data/ext/puma_http11/ext_help.h +0 -15
  95. data/ext/puma_http11/io_buffer.c +0 -154
  96. data/lib/puma/accept_nonblock.rb +0 -23
  97. data/lib/puma/capistrano.rb +0 -34
  98. data/lib/puma/compat.rb +0 -11
  99. data/lib/puma/daemon_ext.rb +0 -20
  100. data/lib/puma/delegation.rb +0 -11
  101. data/lib/puma/java_io_buffer.rb +0 -45
  102. data/lib/puma/rack_patch.rb +0 -25
  103. data/puma.gemspec +0 -45
  104. data/test/test_app_status.rb +0 -88
  105. data/test/test_cli.rb +0 -171
  106. data/test/test_config.rb +0 -16
  107. data/test/test_http10.rb +0 -27
  108. data/test/test_http11.rb +0 -126
  109. data/test/test_integration.rb +0 -154
  110. data/test/test_iobuffer.rb +0 -38
  111. data/test/test_minissl.rb +0 -25
  112. data/test/test_null_io.rb +0 -31
  113. data/test/test_persistent.rb +0 -238
  114. data/test/test_puma_server.rb +0 -207
  115. data/test/test_rack_handler.rb +0 -10
  116. data/test/test_rack_server.rb +0 -141
  117. data/test/test_thread_pool.rb +0 -146
  118. data/test/test_unix_socket.rb +0 -39
  119. data/test/test_ws.rb +0 -89
  120. data/tools/jungle/init.d/README.md +0 -54
  121. data/tools/jungle/init.d/puma +0 -332
  122. data/tools/jungle/init.d/run-puma +0 -3
  123. data/tools/jungle/upstart/README.md +0 -61
  124. data/tools/jungle/upstart/puma-manager.conf +0 -31
  125. data/tools/jungle/upstart/puma.conf +0 -52
data/lib/puma/util.rb CHANGED
@@ -1,3 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'uri/common'
4
+
1
5
  module Puma
2
6
  module Util
3
7
  module_function
@@ -5,5 +9,126 @@ module Puma
5
9
  def pipe
6
10
  IO.pipe
7
11
  end
12
+
13
+ # Escapes and unescapes a URI escaped string with
14
+ # +encoding+. +encoding+ will be the target encoding of the string
15
+ # returned, and it defaults to UTF-8
16
+ if defined?(::Encoding)
17
+ def escape(s, encoding = Encoding::UTF_8)
18
+ URI.encode_www_form_component(s, encoding)
19
+ end
20
+
21
+ def unescape(s, encoding = Encoding::UTF_8)
22
+ URI.decode_www_form_component(s, encoding)
23
+ end
24
+ else
25
+ def escape(s, encoding = nil)
26
+ URI.encode_www_form_component(s, encoding)
27
+ end
28
+
29
+ def unescape(s, encoding = nil)
30
+ URI.decode_www_form_component(s, encoding)
31
+ end
32
+ end
33
+ module_function :unescape, :escape
34
+
35
+ DEFAULT_SEP = /[&;] */n
36
+
37
+ # Stolen from Mongrel, with some small modifications:
38
+ # Parses a query string by breaking it up at the '&'
39
+ # and ';' characters. You can also use this to parse
40
+ # cookies by changing the characters used in the second
41
+ # parameter (which defaults to '&;').
42
+ def parse_query(qs, d = nil, &unescaper)
43
+ unescaper ||= method(:unescape)
44
+
45
+ params = {}
46
+
47
+ (qs || '').split(d ? /[#{d}] */n : DEFAULT_SEP).each do |p|
48
+ next if p.empty?
49
+ k, v = p.split('=', 2).map(&unescaper)
50
+
51
+ if cur = params[k]
52
+ if cur.class == Array
53
+ params[k] << v
54
+ else
55
+ params[k] = [cur, v]
56
+ end
57
+ else
58
+ params[k] = v
59
+ end
60
+ end
61
+
62
+ params
63
+ end
64
+
65
+ # A case-insensitive Hash that preserves the original case of a
66
+ # header when set.
67
+ class HeaderHash < Hash
68
+ def self.new(hash={})
69
+ HeaderHash === hash ? hash : super(hash)
70
+ end
71
+
72
+ def initialize(hash={})
73
+ super()
74
+ @names = {}
75
+ hash.each { |k, v| self[k] = v }
76
+ end
77
+
78
+ def each
79
+ super do |k, v|
80
+ yield(k, v.respond_to?(:to_ary) ? v.to_ary.join("\n") : v)
81
+ end
82
+ end
83
+
84
+ # @!attribute [r] to_hash
85
+ def to_hash
86
+ hash = {}
87
+ each { |k,v| hash[k] = v }
88
+ hash
89
+ end
90
+
91
+ def [](k)
92
+ super(k) || super(@names[k.downcase])
93
+ end
94
+
95
+ def []=(k, v)
96
+ canonical = k.downcase
97
+ delete k if @names[canonical] && @names[canonical] != k # .delete is expensive, don't invoke it unless necessary
98
+ @names[k] = @names[canonical] = k
99
+ super k, v
100
+ end
101
+
102
+ def delete(k)
103
+ canonical = k.downcase
104
+ result = super @names.delete(canonical)
105
+ @names.delete_if { |name,| name.downcase == canonical }
106
+ result
107
+ end
108
+
109
+ def include?(k)
110
+ @names.include?(k) || @names.include?(k.downcase)
111
+ end
112
+
113
+ alias_method :has_key?, :include?
114
+ alias_method :member?, :include?
115
+ alias_method :key?, :include?
116
+
117
+ def merge!(other)
118
+ other.each { |k, v| self[k] = v }
119
+ self
120
+ end
121
+
122
+ def merge(other)
123
+ hash = dup
124
+ hash.merge! other
125
+ end
126
+
127
+ def replace(other)
128
+ clear
129
+ other.each { |k, v| self[k] = v }
130
+ self
131
+ end
132
+ end
8
133
  end
9
134
  end
data/lib/puma.rb CHANGED
@@ -1,14 +1,88 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Standard libraries
2
4
  require 'socket'
3
5
  require 'tempfile'
4
- require 'yaml'
5
- require 'time'
6
- require 'etc'
7
6
  require 'uri'
8
7
  require 'stringio'
9
8
 
10
9
  require 'thread'
11
10
 
12
- # Ruby Puma
13
- require 'puma/const'
14
- require 'puma/server'
11
+ # use require, see https://github.com/puma/puma/pull/2381
12
+ require 'puma/puma_http11'
13
+
14
+ require_relative 'puma/detect'
15
+ require_relative 'puma/json_serialization'
16
+
17
+ module Puma
18
+ # when Puma is loaded via `Puma::CLI`, all files are loaded via
19
+ # `require_relative`. The below are for non-standard loading
20
+ autoload :Const, "#{__dir__}/puma/const"
21
+ autoload :Server, "#{__dir__}/puma/server"
22
+ autoload :Launcher, "#{__dir__}/puma/launcher"
23
+ autoload :LogWriter, "#{__dir__}/puma/log_writer"
24
+
25
+ # at present, MiniSSL::Engine is only defined in extension code (puma_http11),
26
+ # not in minissl.rb
27
+ HAS_SSL = const_defined?(:MiniSSL, false) && MiniSSL.const_defined?(:Engine, false)
28
+
29
+ HAS_UNIX_SOCKET = Object.const_defined?(:UNIXSocket) && !IS_WINDOWS
30
+
31
+ if HAS_SSL
32
+ require_relative 'puma/minissl'
33
+ else
34
+ module MiniSSL
35
+ # this class is defined so that it exists when Puma is compiled
36
+ # without ssl support, as Server and Reactor use it in rescue statements.
37
+ class SSLError < StandardError ; end
38
+ end
39
+ end
40
+
41
+ def self.ssl?
42
+ HAS_SSL
43
+ end
44
+
45
+ def self.abstract_unix_socket?
46
+ @abstract_unix ||=
47
+ if HAS_UNIX_SOCKET
48
+ begin
49
+ ::UNIXServer.new("\0puma.temp.unix").close
50
+ true
51
+ rescue ArgumentError # darwin
52
+ false
53
+ end
54
+ else
55
+ false
56
+ end
57
+ end
58
+
59
+ # @!attribute [rw] stats_object=
60
+ def self.stats_object=(val)
61
+ @get_stats = val
62
+ end
63
+
64
+ # @!attribute [rw] stats_object
65
+ def self.stats
66
+ Puma::JSONSerialization.generate @get_stats.stats
67
+ end
68
+
69
+ # @!attribute [r] stats_hash
70
+ # @version 5.0.0
71
+ def self.stats_hash
72
+ @get_stats.stats
73
+ end
74
+
75
+ def self.set_thread_name(name)
76
+ Thread.current.name = "puma #{name}"
77
+ end
78
+
79
+ # Shows deprecated warning for renamed methods.
80
+ # @example
81
+ # Puma.deprecate_method_change :on_booted, __callee__, __method__
82
+ #
83
+ def self.deprecate_method_change(method_old, method_caller, method_new)
84
+ if method_old == method_caller
85
+ warn "Use '#{method_new}', '#{method_caller}' is deprecated and will be removed in v8"
86
+ end
87
+ end
88
+ end
@@ -1,66 +1,144 @@
1
- require 'rack/handler'
2
- require 'puma'
3
-
4
- module Rack
5
- module Handler
6
- module Puma
7
- DEFAULT_OPTIONS = {
8
- :Host => '0.0.0.0',
9
- :Port => 8080,
10
- :Threads => '0:16',
11
- :Verbose => false
12
- }
1
+ # frozen_string_literal: true
2
+
3
+ module Puma
4
+
5
+ # This module is used as an 'include' file in code at bottom of file. It loads
6
+ # into either `Rackup::Handler::Puma` or `Rack::Handler::Puma`.
7
+
8
+ module RackHandler
9
+ DEFAULT_OPTIONS = {
10
+ :Verbose => false,
11
+ :Silent => false
12
+ }
13
+
14
+ def config(app, options = {})
15
+ require_relative '../../puma'
16
+ require_relative '../../puma/configuration'
17
+ require_relative '../../puma/log_writer'
18
+ require_relative '../../puma/launcher'
19
+
20
+ default_options = DEFAULT_OPTIONS.dup
21
+
22
+ # Libraries pass in values such as :Port and there is no way to determine
23
+ # if it is a default provided by the library or a special value provided
24
+ # by the user. A special key `user_supplied_options` can be passed. This
25
+ # contains an array of all explicitly defined user options. We then
26
+ # know that all other values are defaults
27
+ if user_supplied_options = options.delete(:user_supplied_options)
28
+ (options.keys - user_supplied_options).each do |k|
29
+ default_options[k] = options.delete(k)
30
+ end
31
+ end
13
32
 
14
- def self.run(app, options = {})
15
- options = DEFAULT_OPTIONS.merge(options)
33
+ @events = options[:events] || ::Puma::Events.new
16
34
 
17
- if options[:Verbose]
18
- app = Rack::CommonLogger.new(app, STDOUT)
35
+ conf = ::Puma::Configuration.new(options, default_options.merge({ events: @events })) do |user_config, file_config, default_config|
36
+ if options.delete(:Verbose)
37
+ begin
38
+ require 'rack/commonlogger' # Rack 1.x
39
+ rescue LoadError
40
+ require 'rack/common_logger' # Rack 2 and later
41
+ end
42
+ app = ::Rack::CommonLogger.new(app, STDOUT)
19
43
  end
20
44
 
21
45
  if options[:environment]
22
- ENV['RACK_ENV'] = options[:environment].to_s
46
+ user_config.environment options[:environment]
23
47
  end
24
48
 
25
- server = ::Puma::Server.new(app)
26
- min, max = options[:Threads].split(':', 2)
27
-
28
- puts "Puma #{::Puma::Const::PUMA_VERSION} starting..."
29
- puts "* Min threads: #{min}, max threads: #{max}"
30
- puts "* Environment: #{ENV['RACK_ENV']}"
31
- puts "* Listening on tcp://#{options[:Host]}:#{options[:Port]}"
32
-
33
- server.add_tcp_listener options[:Host], options[:Port]
34
- server.min_threads = Integer(min)
35
- server.max_threads = Integer(max)
36
- yield server if block_given?
37
-
38
- begin
39
- server.run.join
40
- rescue Interrupt
41
- puts "* Gracefully stopping, waiting for requests to finish"
42
- server.stop(true)
43
- puts "* Goodbye!"
49
+ if options[:Threads]
50
+ min, max = options.delete(:Threads).split(':', 2)
51
+ user_config.threads min, max
44
52
  end
45
53
 
54
+ if options[:Host] || options[:Port]
55
+ host = options[:Host] || default_options[:Host]
56
+ port = options[:Port] || default_options[:Port]
57
+ self.set_host_port_to_config(host, port, user_config)
58
+ end
59
+
60
+ if default_options[:Host]
61
+ file_config.set_default_host(default_options[:Host])
62
+ end
63
+ self.set_host_port_to_config(default_options[:Host], default_options[:Port], default_config)
64
+
65
+ user_config.app app
46
66
  end
67
+ conf
68
+ end
69
+
70
+ def run(app, **options)
71
+ conf = self.config(app, options)
72
+
73
+ log_writer = options.delete(:Silent) ? ::Puma::LogWriter.strings : ::Puma::LogWriter.stdio
74
+
75
+ launcher = ::Puma::Launcher.new(conf, log_writer: log_writer, events: @events)
47
76
 
48
- def self.valid_options
49
- {
50
- "Host=HOST" => "Hostname to listen on (default: localhost)",
51
- "Port=PORT" => "Port to listen on (default: 8080)",
52
- "Threads=MIN:MAX" => "min:max threads to use (default 0:16)",
53
- "Quiet" => "Don't report each request"
54
- }
77
+ yield launcher if block_given?
78
+ begin
79
+ launcher.run
80
+ rescue Interrupt
81
+ puts "* Gracefully stopping, waiting for requests to finish"
82
+ launcher.stop
83
+ puts "* Goodbye!"
55
84
  end
56
85
  end
57
86
 
58
- register :puma, Puma
87
+ def valid_options
88
+ {
89
+ "Host=HOST" => "Hostname to listen on (default: localhost)",
90
+ "Port=PORT" => "Port to listen on (default: 8080)",
91
+ "Threads=MIN:MAX" => "min:max threads to use (default 0:16)",
92
+ "Verbose" => "Don't report each request (default: false)"
93
+ }
94
+ end
95
+
96
+ def set_host_port_to_config(host, port, config)
97
+ config.clear_binds! if host || port
98
+
99
+ if host&.start_with? '.', '/', '@'
100
+ config.bind "unix://#{host}"
101
+ elsif host&.start_with? 'ssl://'
102
+ uri = URI.parse(host)
103
+ uri.port ||= port || ::Puma::Configuration::DEFAULTS[:tcp_port]
104
+ config.bind uri.to_s
105
+ else
106
+
107
+ if host
108
+ port ||= ::Puma::Configuration::DEFAULTS[:tcp_port]
109
+ end
110
+
111
+ if port
112
+ host ||= ::Puma::Configuration::DEFAULTS[:tcp_host]
113
+ config.port port, host
114
+ end
115
+ end
116
+ end
59
117
  end
60
118
  end
61
119
 
62
- # This is to trick newrelic into enabling the agent automatically.
63
- module Mongrel
64
- class HttpServer
120
+ # rackup was removed in Rack 3, it is now a separate gem
121
+ if Object.const_defined?(:Rackup) && ::Rackup.const_defined?(:Handler)
122
+ module Rackup
123
+ module Handler
124
+ module Puma
125
+ class << self
126
+ include ::Puma::RackHandler
127
+ end
128
+ end
129
+ register :puma, Puma
130
+ end
131
+ end
132
+ else
133
+ do_register = Object.const_defined?(:Rack) && ::Rack.release < '3'
134
+ module Rack
135
+ module Handler
136
+ module Puma
137
+ class << self
138
+ include ::Puma::RackHandler
139
+ end
140
+ end
141
+ end
65
142
  end
143
+ ::Rack::Handler.register(:puma, ::Rack::Handler::Puma) if do_register
66
144
  end
data/tools/Dockerfile ADDED
@@ -0,0 +1,26 @@
1
+ # Use this Dockerfile to create minimal reproductions of issues
2
+ # Build (MRI): docker build -f tools/Dockerfile .
3
+ # Build (JRuby): docker build -f tools/Dockerfile --build-arg RUBY_IMAGE=jruby:9.4 .
4
+
5
+ ARG RUBY_IMAGE=ruby:latest
6
+ FROM ${RUBY_IMAGE}
7
+
8
+ # Set BUNDLE_FROZEN=false if you need to update Gemfile.lock during a build.
9
+ ARG BUNDLE_FROZEN=true
10
+
11
+ RUN apt-get update \
12
+ && apt-get install -y --no-install-recommends ragel procps git \
13
+ && rm -rf /var/lib/apt/lists/*
14
+
15
+ # Only freeze Bundler and compile native extensions when using MRI.
16
+ RUN if [ "$(ruby -e 'print RUBY_ENGINE')" = "ruby" ] && [ "${BUNDLE_FROZEN}" = "true" ]; then bundle config --global frozen 1; fi
17
+
18
+ WORKDIR /usr/src/app
19
+
20
+ COPY . .
21
+
22
+ RUN bundle install
23
+ RUN if [ "$(ruby -e 'print RUBY_ENGINE')" = "ruby" ]; then bundle exec rake compile; fi
24
+
25
+ EXPOSE 9292
26
+ CMD ["bundle", "exec", "bin/puma", "test/rackup/hello.ru"]
@@ -0,0 +1,44 @@
1
+ require 'socket'
2
+ require 'stringio'
3
+
4
+ def do_test(st, chunk)
5
+ s = TCPSocket.new('127.0.0.1',ARGV[0].to_i);
6
+ req = StringIO.new(st)
7
+ nout = 0
8
+ randstop = rand(st.length / 10)
9
+ STDERR.puts "stopping after: #{randstop}"
10
+
11
+ begin
12
+ while data = req.read(chunk)
13
+ nout += s.write(data)
14
+ s.flush
15
+ sleep 0.1
16
+ if nout > randstop
17
+ STDERR.puts "BANG! after #{nout} bytes."
18
+ break
19
+ end
20
+ end
21
+ rescue Object => e
22
+ STDERR.puts "ERROR: #{e}"
23
+ ensure
24
+ s.close
25
+ end
26
+ end
27
+
28
+ content = "-" * (1024 * 240)
29
+ st = "GET / HTTP/1.1\r\nHost: www.zedshaw.com\r\nContent-Type: text/plain\r\nContent-Length: #{content.length}\r\n\r\n#{content}"
30
+
31
+ puts "length: #{content.length}"
32
+
33
+ threads = []
34
+ ARGV[1].to_i.times do
35
+ t = Thread.new do
36
+ size = 100
37
+ puts ">>>> #{size} sized chunks"
38
+ do_test(st, size)
39
+ end
40
+
41
+ threads << t
42
+ end
43
+
44
+ threads.each {|t| t.join}