puma 5.6.7 → 6.4.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of puma might be problematic. Click here for more details.

Files changed (64) hide show
  1. checksums.yaml +4 -4
  2. data/History.md +269 -13
  3. data/README.md +78 -29
  4. data/bin/puma-wild +1 -1
  5. data/docs/compile_options.md +34 -0
  6. data/docs/fork_worker.md +1 -3
  7. data/docs/kubernetes.md +12 -0
  8. data/docs/nginx.md +1 -1
  9. data/docs/systemd.md +1 -2
  10. data/docs/testing_benchmarks_local_files.md +150 -0
  11. data/docs/testing_test_rackup_ci_files.md +36 -0
  12. data/ext/puma_http11/extconf.rb +11 -8
  13. data/ext/puma_http11/http11_parser.c +1 -1
  14. data/ext/puma_http11/http11_parser.h +1 -1
  15. data/ext/puma_http11/http11_parser.java.rl +2 -2
  16. data/ext/puma_http11/http11_parser.rl +2 -2
  17. data/ext/puma_http11/http11_parser_common.rl +2 -2
  18. data/ext/puma_http11/mini_ssl.c +122 -18
  19. data/ext/puma_http11/org/jruby/puma/Http11.java +3 -3
  20. data/ext/puma_http11/org/jruby/puma/Http11Parser.java +1 -1
  21. data/ext/puma_http11/org/jruby/puma/MiniSSL.java +156 -53
  22. data/ext/puma_http11/puma_http11.c +17 -9
  23. data/lib/puma/app/status.rb +4 -4
  24. data/lib/puma/binder.rb +50 -53
  25. data/lib/puma/cli.rb +16 -18
  26. data/lib/puma/client.rb +59 -19
  27. data/lib/puma/cluster/worker.rb +18 -11
  28. data/lib/puma/cluster/worker_handle.rb +4 -1
  29. data/lib/puma/cluster.rb +33 -30
  30. data/lib/puma/commonlogger.rb +21 -14
  31. data/lib/puma/configuration.rb +78 -58
  32. data/lib/puma/const.rb +129 -92
  33. data/lib/puma/control_cli.rb +15 -11
  34. data/lib/puma/detect.rb +4 -0
  35. data/lib/puma/dsl.rb +237 -56
  36. data/lib/puma/error_logger.rb +18 -9
  37. data/lib/puma/events.rb +6 -126
  38. data/lib/puma/io_buffer.rb +39 -4
  39. data/lib/puma/jruby_restart.rb +2 -1
  40. data/lib/puma/launcher/bundle_pruner.rb +104 -0
  41. data/lib/puma/launcher.rb +102 -175
  42. data/lib/puma/log_writer.rb +147 -0
  43. data/lib/puma/minissl/context_builder.rb +24 -12
  44. data/lib/puma/minissl.rb +99 -11
  45. data/lib/puma/plugin/systemd.rb +90 -0
  46. data/lib/puma/plugin/tmp_restart.rb +1 -1
  47. data/lib/puma/rack/builder.rb +6 -6
  48. data/lib/puma/rack/urlmap.rb +1 -1
  49. data/lib/puma/rack_default.rb +19 -4
  50. data/lib/puma/reactor.rb +19 -10
  51. data/lib/puma/request.rb +365 -170
  52. data/lib/puma/runner.rb +56 -20
  53. data/lib/puma/sd_notify.rb +149 -0
  54. data/lib/puma/server.rb +116 -89
  55. data/lib/puma/single.rb +13 -11
  56. data/lib/puma/state_file.rb +1 -4
  57. data/lib/puma/thread_pool.rb +57 -19
  58. data/lib/puma/util.rb +0 -11
  59. data/lib/puma.rb +9 -10
  60. data/lib/rack/handler/puma.rb +113 -86
  61. metadata +9 -5
  62. data/lib/puma/queue_close.rb +0 -26
  63. data/lib/puma/systemd.rb +0 -46
  64. data/lib/rack/version_restriction.rb +0 -15
data/lib/puma/binder.rb CHANGED
@@ -3,24 +3,15 @@
3
3
  require 'uri'
4
4
  require 'socket'
5
5
 
6
- require 'puma/const'
7
- require 'puma/util'
8
- require 'puma/configuration'
6
+ require_relative 'const'
7
+ require_relative 'util'
8
+ require_relative 'configuration'
9
9
 
10
10
  module Puma
11
11
 
12
12
  if HAS_SSL
13
- require 'puma/minissl'
14
- require 'puma/minissl/context_builder'
15
-
16
- # Odd bug in 'pure Ruby' nio4r version 2.5.2, which installs with Ruby 2.3.
17
- # NIO doesn't create any OpenSSL objects, but it rescues an OpenSSL error.
18
- # The bug was that it did not require openssl.
19
- # @todo remove when Ruby 2.3 support is dropped
20
- #
21
- if windows? && RbConfig::CONFIG['ruby_version'] == '2.3.0'
22
- require 'openssl'
23
- end
13
+ require_relative 'minissl'
14
+ require_relative 'minissl/context_builder'
24
15
  end
25
16
 
26
17
  class Binder
@@ -28,8 +19,8 @@ module Puma
28
19
 
29
20
  RACK_VERSION = [1,6].freeze
30
21
 
31
- def initialize(events, conf = Configuration.new)
32
- @events = events
22
+ def initialize(log_writer, conf = Configuration.new)
23
+ @log_writer = log_writer
33
24
  @conf = conf
34
25
  @listeners = []
35
26
  @inherited_fds = {}
@@ -38,7 +29,7 @@ module Puma
38
29
 
39
30
  @proto_env = {
40
31
  "rack.version".freeze => RACK_VERSION,
41
- "rack.errors".freeze => events.stderr,
32
+ "rack.errors".freeze => log_writer.stderr,
42
33
  "rack.multithread".freeze => conf.options[:max_threads] > 1,
43
34
  "rack.multiprocess".freeze => conf.options[:workers] >= 1,
44
35
  "rack.run_once".freeze => false,
@@ -51,14 +42,12 @@ module Puma
51
42
  # infer properly.
52
43
 
53
44
  "QUERY_STRING".freeze => "",
54
- SERVER_PROTOCOL => HTTP_11,
55
45
  SERVER_SOFTWARE => PUMA_SERVER_STRING,
56
46
  GATEWAY_INTERFACE => CGI_VER
57
47
  }
58
48
 
59
49
  @envs = {}
60
50
  @ios = []
61
- localhost_authority
62
51
  end
63
52
 
64
53
  attr_reader :ios
@@ -80,7 +69,7 @@ module Puma
80
69
  # @!attribute [r] connected_ports
81
70
  # @version 5.0.0
82
71
  def connected_ports
83
- ios.map { |io| io.addr[1] }.uniq
72
+ t = ios.map { |io| io.addr[1] }; t.uniq!; t
84
73
  end
85
74
 
86
75
  # @version 5.0.0
@@ -98,7 +87,7 @@ module Puma
98
87
  # @version 5.0.0
99
88
  #
100
89
  def create_activated_fds(env_hash)
101
- @events.debug "ENV['LISTEN_FDS'] #{ENV['LISTEN_FDS'].inspect} env_hash['LISTEN_PID'] #{env_hash['LISTEN_PID'].inspect}"
90
+ @log_writer.debug "ENV['LISTEN_FDS'] #{ENV['LISTEN_FDS'].inspect} env_hash['LISTEN_PID'] #{env_hash['LISTEN_PID'].inspect}"
102
91
  return [] unless env_hash['LISTEN_FDS'] && env_hash['LISTEN_PID'].to_i == $$
103
92
  env_hash['LISTEN_FDS'].to_i.times do |index|
104
93
  sock = TCPServer.for_fd(socket_activation_fd(index))
@@ -106,11 +95,11 @@ module Puma
106
95
  [:unix, Socket.unpack_sockaddr_un(sock.getsockname)]
107
96
  rescue ArgumentError # Try to parse as a port/ip
108
97
  port, addr = Socket.unpack_sockaddr_in(sock.getsockname)
109
- addr = "[#{addr}]" if addr =~ /\:/
98
+ addr = "[#{addr}]" if addr&.include? ':'
110
99
  [:tcp, addr, port]
111
100
  end
112
101
  @activated_sockets[key] = sock
113
- @events.debug "Registered #{key.join ':'} for activation from LISTEN_FDS"
102
+ @log_writer.debug "Registered #{key.join ':'} for activation from LISTEN_FDS"
114
103
  end
115
104
  ["LISTEN_FDS", "LISTEN_PID"] # Signal to remove these keys from ENV
116
105
  end
@@ -152,29 +141,30 @@ module Puma
152
141
  end
153
142
  end
154
143
 
155
- def parse(binds, logger, log_msg = 'Listening')
144
+ def parse(binds, log_writer = nil, log_msg = 'Listening')
145
+ log_writer ||= @log_writer
156
146
  binds.each do |str|
157
147
  uri = URI.parse str
158
148
  case uri.scheme
159
149
  when "tcp"
160
150
  if fd = @inherited_fds.delete(str)
161
151
  io = inherit_tcp_listener uri.host, uri.port, fd
162
- logger.log "* Inherited #{str}"
152
+ log_writer.log "* Inherited #{str}"
163
153
  elsif sock = @activated_sockets.delete([ :tcp, uri.host, uri.port ])
164
154
  io = inherit_tcp_listener uri.host, uri.port, sock
165
- logger.log "* Activated #{str}"
155
+ log_writer.log "* Activated #{str}"
166
156
  else
167
157
  ios_len = @ios.length
168
158
  params = Util.parse_query uri.query
169
159
 
170
- opt = params.key?('low_latency') && params['low_latency'] != 'false'
160
+ low_latency = params.key?('low_latency') && params['low_latency'] != 'false'
171
161
  backlog = params.fetch('backlog', 1024).to_i
172
162
 
173
- io = add_tcp_listener uri.host, uri.port, opt, backlog
163
+ io = add_tcp_listener uri.host, uri.port, low_latency, backlog
174
164
 
175
165
  @ios[ios_len..-1].each do |i|
176
166
  addr = loc_addr_str i
177
- logger.log "* #{log_msg} on http://#{addr}"
167
+ log_writer.log "* #{log_msg} on http://#{addr}"
178
168
  end
179
169
  end
180
170
 
@@ -191,12 +181,12 @@ module Puma
191
181
  if fd = @inherited_fds.delete(str)
192
182
  @unix_paths << path unless abstract || File.exist?(path)
193
183
  io = inherit_unix_listener path, fd
194
- logger.log "* Inherited #{str}"
184
+ log_writer.log "* Inherited #{str}"
195
185
  elsif sock = @activated_sockets.delete([ :unix, path ]) ||
196
186
  @activated_sockets.delete([ :unix, File.realdirpath(path) ])
197
187
  @unix_paths << path unless abstract || File.exist?(path)
198
188
  io = inherit_unix_listener path, sock
199
- logger.log "* Activated #{str}"
189
+ log_writer.log "* Activated #{str}"
200
190
  else
201
191
  umask = nil
202
192
  mode = nil
@@ -220,11 +210,12 @@ module Puma
220
210
 
221
211
  @unix_paths << path unless abstract || File.exist?(path)
222
212
  io = add_unix_listener path, umask, mode, backlog
223
- logger.log "* #{log_msg} on #{str}"
213
+ log_writer.log "* #{log_msg} on #{str}"
224
214
  end
225
215
 
226
216
  @listeners << [str, io]
227
217
  when "ssl"
218
+ cert_key = %w[cert key]
228
219
 
229
220
  raise "Puma compiled without SSL support" unless HAS_SSL
230
221
 
@@ -233,49 +224,51 @@ module Puma
233
224
  # If key and certs are not defined and localhost gem is required.
234
225
  # localhost gem will be used for self signed
235
226
  # Load localhost authority if not loaded.
236
- if params.values_at('cert', 'key').all? { |v| v.to_s.empty? }
227
+ # Ruby 3 `values_at` accepts an array, earlier do not
228
+ if params.values_at(*cert_key).all? { |v| v.to_s.empty? }
237
229
  ctx = localhost_authority && localhost_authority_context
238
230
  end
239
231
 
240
232
  ctx ||=
241
233
  begin
242
234
  # Extract cert_pem and key_pem from options[:store] if present
243
- ['cert', 'key'].each do |v|
244
- if params[v] && params[v].start_with?('store:')
235
+ cert_key.each do |v|
236
+ if params[v]&.start_with?('store:')
245
237
  index = Integer(params.delete(v).split('store:').last)
246
238
  params["#{v}_pem"] = @conf.options[:store][index]
247
239
  end
248
240
  end
249
- MiniSSL::ContextBuilder.new(params, @events).context
241
+ MiniSSL::ContextBuilder.new(params, @log_writer).context
250
242
  end
251
243
 
252
244
  if fd = @inherited_fds.delete(str)
253
- logger.log "* Inherited #{str}"
245
+ log_writer.log "* Inherited #{str}"
254
246
  io = inherit_ssl_listener fd, ctx
255
247
  elsif sock = @activated_sockets.delete([ :tcp, uri.host, uri.port ])
256
248
  io = inherit_ssl_listener sock, ctx
257
- logger.log "* Activated #{str}"
249
+ log_writer.log "* Activated #{str}"
258
250
  else
259
251
  ios_len = @ios.length
260
252
  backlog = params.fetch('backlog', 1024).to_i
261
- io = add_ssl_listener uri.host, uri.port, ctx, optimize_for_latency = true, backlog
253
+ low_latency = params['low_latency'] != 'false'
254
+ io = add_ssl_listener uri.host, uri.port, ctx, low_latency, backlog
262
255
 
263
256
  @ios[ios_len..-1].each do |i|
264
257
  addr = loc_addr_str i
265
- logger.log "* #{log_msg} on ssl://#{addr}?#{uri.query}"
258
+ log_writer.log "* #{log_msg} on ssl://#{addr}?#{uri.query}"
266
259
  end
267
260
  end
268
261
 
269
262
  @listeners << [str, io] if io
270
263
  else
271
- logger.error "Invalid URI: #{str}"
264
+ log_writer.error "Invalid URI: #{str}"
272
265
  end
273
266
  end
274
267
 
275
268
  # If we inherited fds but didn't use them (because of a
276
269
  # configuration change), then be sure to close them.
277
270
  @inherited_fds.each do |str, fd|
278
- logger.log "* Closing unused inherited connection: #{str}"
271
+ log_writer.log "* Closing unused inherited connection: #{str}"
279
272
 
280
273
  begin
281
274
  IO.for_fd(fd).close
@@ -295,7 +288,7 @@ module Puma
295
288
  fds = @ios.map(&:to_i)
296
289
  @activated_sockets.each do |key, sock|
297
290
  next if fds.include? sock.to_i
298
- logger.log "* Closing unused activated socket: #{key.first}://#{key[1..-1].join ':'}"
291
+ log_writer.log "* Closing unused activated socket: #{key.first}://#{key[1..-1].join ':'}"
299
292
  begin
300
293
  sock.close
301
294
  rescue SystemCallError
@@ -319,7 +312,7 @@ module Puma
319
312
  local_certificates_path = File.expand_path("~/.localhost")
320
313
  [File.join(local_certificates_path, "localhost.key"), File.join(local_certificates_path, "localhost.crt")]
321
314
  end
322
- MiniSSL::ContextBuilder.new({ "key" => key_path, "cert" => crt_path }, @events).context
315
+ MiniSSL::ContextBuilder.new({ "key" => key_path, "cert" => crt_path }, @log_writer).context
323
316
  end
324
317
 
325
318
  # Tell the server to listen on host +host+, port +port+.
@@ -337,7 +330,7 @@ module Puma
337
330
  return
338
331
  end
339
332
 
340
- host = host[1..-2] if host and host[0..0] == '['
333
+ host = host[1..-2] if host&.start_with? '['
341
334
  tcp_server = TCPServer.new(host, port)
342
335
 
343
336
  if optimize_for_latency
@@ -371,7 +364,7 @@ module Puma
371
364
  return
372
365
  end
373
366
 
374
- host = host[1..-2] if host[0..0] == '['
367
+ host = host[1..-2] if host&.start_with? '['
375
368
  s = TCPServer.new(host, port)
376
369
  if optimize_for_latency
377
370
  s.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
@@ -457,11 +450,14 @@ module Puma
457
450
 
458
451
  def close_listeners
459
452
  @listeners.each do |l, io|
460
- io.close unless io.closed?
461
- uri = URI.parse l
462
- next unless uri.scheme == 'unix'
463
- unix_path = "#{uri.host}#{uri.path}"
464
- File.unlink unix_path if @unix_paths.include?(unix_path) && File.exist?(unix_path)
453
+ begin
454
+ io.close unless io.closed?
455
+ uri = URI.parse l
456
+ next unless uri.scheme == 'unix'
457
+ unix_path = "#{uri.host}#{uri.path}"
458
+ File.unlink unix_path if @unix_paths.include?(unix_path) && File.exist?(unix_path)
459
+ rescue Errno::EBADF
460
+ end
465
461
  end
466
462
  end
467
463
 
@@ -482,9 +478,10 @@ module Puma
482
478
 
483
479
  # @!attribute [r] loopback_addresses
484
480
  def loopback_addresses
485
- Socket.ip_address_list.select do |addrinfo|
481
+ t = Socket.ip_address_list.select do |addrinfo|
486
482
  addrinfo.ipv6_loopback? || addrinfo.ipv4_loopback?
487
- end.map { |addrinfo| addrinfo.ip_address }.uniq
483
+ end
484
+ t.map! { |addrinfo| addrinfo.ip_address }; t.uniq!; t
488
485
  end
489
486
 
490
487
  def loc_addr_str(io)
data/lib/puma/cli.rb CHANGED
@@ -3,11 +3,11 @@
3
3
  require 'optparse'
4
4
  require 'uri'
5
5
 
6
- require 'puma'
7
- require 'puma/configuration'
8
- require 'puma/launcher'
9
- require 'puma/const'
10
- require 'puma/events'
6
+ require_relative '../puma'
7
+ require_relative 'configuration'
8
+ require_relative 'launcher'
9
+ require_relative 'const'
10
+ require_relative 'log_writer'
11
11
 
12
12
  module Puma
13
13
  class << self
@@ -21,19 +21,13 @@ module Puma
21
21
  # Handles invoke a Puma::Server in a command line style.
22
22
  #
23
23
  class CLI
24
- # @deprecated 6.0.0
25
- KEYS_NOT_TO_PERSIST_IN_STATE = Launcher::KEYS_NOT_TO_PERSIST_IN_STATE
26
-
27
24
  # Create a new CLI object using +argv+ as the command line
28
25
  # arguments.
29
26
  #
30
- # +stdout+ and +stderr+ can be set to IO-like objects which
31
- # this object will report status on.
32
- #
33
- def initialize(argv, events=Events.stdio)
27
+ def initialize(argv, log_writer = LogWriter.stdio, events = Events.new)
34
28
  @debug = false
35
29
  @argv = argv.dup
36
-
30
+ @log_writer = log_writer
37
31
  @events = events
38
32
 
39
33
  @conf = nil
@@ -69,7 +63,7 @@ module Puma
69
63
  end
70
64
  end
71
65
 
72
- @launcher = Puma::Launcher.new(@conf, :events => @events, :argv => argv)
66
+ @launcher = Puma::Launcher.new(@conf, :log_writer => @log_writer, :events => @events, :argv => argv)
73
67
  end
74
68
 
75
69
  attr_reader :launcher
@@ -83,7 +77,7 @@ module Puma
83
77
 
84
78
  private
85
79
  def unsupported(str)
86
- @events.error(str)
80
+ @log_writer.error(str)
87
81
  raise UnsupportedOption
88
82
  end
89
83
 
@@ -99,7 +93,7 @@ module Puma
99
93
  #
100
94
 
101
95
  def setup_options
102
- @conf = Configuration.new do |user_config, file_config|
96
+ @conf = Configuration.new({}, {events: @events}) do |user_config, file_config|
103
97
  @parser = OptionParser.new do |o|
104
98
  o.on "-b", "--bind URI", "URI to bind to (tcp://, unix://, ssl://)" do |arg|
105
99
  user_config.bind arg
@@ -150,9 +144,13 @@ module Puma
150
144
  $LOAD_PATH.unshift(*arg.split(':'))
151
145
  end
152
146
 
147
+ o.on "--idle-timeout SECONDS", "Number of seconds until the next request before automatic shutdown" do |arg|
148
+ user_config.idle_timeout arg
149
+ end
150
+
153
151
  o.on "-p", "--port PORT", "Define the TCP port to bind to",
154
152
  "Use -b for more advanced options" do |arg|
155
- user_config.bind "tcp://#{Configuration::DefaultTCPHost}:#{arg}"
153
+ user_config.bind "tcp://#{Configuration::DEFAULTS[:tcp_host]}:#{arg}"
156
154
  end
157
155
 
158
156
  o.on "--pidfile PATH", "Use PATH as a pidfile" do |arg|
@@ -186,7 +184,7 @@ module Puma
186
184
  end
187
185
 
188
186
  o.on "-s", "--silent", "Do not log prompt messages other than errors" do
189
- @events = Events.new NullIO.new, $stderr
187
+ @log_writer = LogWriter.new(NullIO.new, $stderr)
190
188
  end
191
189
 
192
190
  o.on "-S", "--state PATH", "Where to store the state details" do |arg|
data/lib/puma/client.rb CHANGED
@@ -8,9 +8,9 @@ class IO
8
8
  end
9
9
  end
10
10
 
11
- require 'puma/detect'
11
+ require_relative 'detect'
12
+ require_relative 'io_buffer'
12
13
  require 'tempfile'
13
- require 'forwardable'
14
14
 
15
15
  if Puma::IS_JRUBY
16
16
  # We have to work around some OpenSSL buffer/io-readiness bugs
@@ -25,6 +25,9 @@ module Puma
25
25
 
26
26
  class HttpParserError501 < IOError; end
27
27
 
28
+ #———————————————————————— DO NOT USE — this class is for internal use only ———
29
+
30
+
28
31
  # An instance of this class represents a unique request from a client.
29
32
  # For example, this could be a web request from a browser or from CURL.
30
33
  #
@@ -38,7 +41,7 @@ module Puma
38
41
  # the header and body are fully buffered via the `try_to_finish` method.
39
42
  # They can be used to "time out" a response via the `timeout_at` reader.
40
43
  #
41
- class Client
44
+ class Client # :nodoc:
42
45
 
43
46
  # this tests all values but the last, which must be chunked
44
47
  ALLOWED_TRANSFER_ENCODING = %w[compress deflate gzip].freeze
@@ -58,17 +61,13 @@ module Puma
58
61
  EmptyBody = NullIO.new
59
62
 
60
63
  include Puma::Const
61
- extend Forwardable
62
64
 
63
65
  def initialize(io, env=nil)
64
66
  @io = io
65
67
  @to_io = io.to_io
68
+ @io_buffer = IOBuffer.new
66
69
  @proto_env = env
67
- if !env
68
- @env = nil
69
- else
70
- @env = env.dup
71
- end
70
+ @env = env&.dup
72
71
 
73
72
  @parser = HttpParser.new
74
73
  @parsed_bytes = 0
@@ -86,7 +85,11 @@ module Puma
86
85
  @requests_served = 0
87
86
  @hijacked = false
88
87
 
88
+ @http_content_length_limit = nil
89
+ @http_content_length_limit_exceeded = false
90
+
89
91
  @peerip = nil
92
+ @peer_family = nil
90
93
  @listener = nil
91
94
  @remote_addr_header = nil
92
95
  @expect_proxy_proto = false
@@ -94,16 +97,22 @@ module Puma
94
97
  @body_remain = 0
95
98
 
96
99
  @in_last_chunk = false
100
+
101
+ # need unfrozen ASCII-8BIT, +'' is UTF-8
102
+ @read_buffer = String.new # rubocop: disable Performance/UnfreezeString
97
103
  end
98
104
 
99
105
  attr_reader :env, :to_io, :body, :io, :timeout_at, :ready, :hijacked,
100
- :tempfile
106
+ :tempfile, :io_buffer, :http_content_length_limit_exceeded
101
107
 
102
- attr_writer :peerip
108
+ attr_writer :peerip, :http_content_length_limit
103
109
 
104
110
  attr_accessor :remote_addr_header, :listener
105
111
 
106
- def_delegators :@io, :closed?
112
+ # Remove in Puma 7?
113
+ def closed?
114
+ @to_io.closed?
115
+ end
107
116
 
108
117
  # Test to see if io meets a bare minimum of functioning, @to_io needs to be
109
118
  # used for MiniSSL::Socket
@@ -139,6 +148,7 @@ module Puma
139
148
 
140
149
  def reset(fast_check=true)
141
150
  @parser.reset
151
+ @io_buffer.reset
142
152
  @read_header = true
143
153
  @read_proxy = !!@expect_proxy_proto
144
154
  @env = @proto_env.dup
@@ -149,6 +159,7 @@ module Puma
149
159
  @body_remain = 0
150
160
  @peerip = nil if @remote_addr_header
151
161
  @in_last_chunk = false
162
+ @http_content_length_limit_exceeded = false
152
163
 
153
164
  if @buffer
154
165
  return false unless try_to_parse_proxy_protocol
@@ -208,6 +219,17 @@ module Puma
208
219
  end
209
220
 
210
221
  def try_to_finish
222
+ if env[CONTENT_LENGTH] && above_http_content_limit(env[CONTENT_LENGTH].to_i)
223
+ @http_content_length_limit_exceeded = true
224
+ end
225
+
226
+ if @http_content_length_limit_exceeded
227
+ @buffer = nil
228
+ @body = EmptyBody
229
+ set_ready
230
+ return true
231
+ end
232
+
211
233
  return read_body if in_data_phase
212
234
 
213
235
  begin
@@ -237,6 +259,10 @@ module Puma
237
259
 
238
260
  @parsed_bytes = @parser.execute(@env, @buffer, @parsed_bytes)
239
261
 
262
+ if @parser.finished? && above_http_content_limit(@parser.body.bytesize)
263
+ @http_content_length_limit_exceeded = true
264
+ end
265
+
240
266
  if @parser.finished?
241
267
  return setup_body
242
268
  elsif @parsed_bytes >= MAX_HEADER
@@ -274,7 +300,7 @@ module Puma
274
300
  return @peerip if @peerip
275
301
 
276
302
  if @remote_addr_header
277
- hdr = (@env[@remote_addr_header] || LOCALHOST_IP).split(/[\s,]/).first
303
+ hdr = (@env[@remote_addr_header] || @io.peeraddr.last).split(/[\s,]/).first
278
304
  @peerip = hdr
279
305
  return hdr
280
306
  end
@@ -282,6 +308,16 @@ module Puma
282
308
  @peerip ||= @io.peeraddr.last
283
309
  end
284
310
 
311
+ def peer_family
312
+ return @peer_family if @peer_family
313
+
314
+ @peer_family ||= begin
315
+ @io.local_address.afamily
316
+ rescue
317
+ Socket::AF_INET
318
+ end
319
+ end
320
+
285
321
  # Returns true if the persistent connection can be closed immediately
286
322
  # without waiting for the configured idle/shutdown timeout.
287
323
  # @version 5.0.0
@@ -305,7 +341,7 @@ module Puma
305
341
  private
306
342
 
307
343
  def setup_body
308
- @body_read_start = Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond)
344
+ @body_read_start = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)
309
345
 
310
346
  if @env[HTTP_EXPECT] == CONTINUE
311
347
  # TODO allow a hook here to check the headers before
@@ -349,7 +385,7 @@ module Puma
349
385
 
350
386
  if cl
351
387
  # cannot contain characters that are not \d, or be empty
352
- if cl =~ CONTENT_LENGTH_VALUE_INVALID || cl.empty?
388
+ if CONTENT_LENGTH_VALUE_INVALID.match?(cl) || cl.empty?
353
389
  raise HttpParserError, "Invalid Content-Length: #{cl.inspect}"
354
390
  end
355
391
  else
@@ -402,7 +438,7 @@ module Puma
402
438
  end
403
439
 
404
440
  begin
405
- chunk = @io.read_nonblock(want)
441
+ chunk = @io.read_nonblock(want, @read_buffer)
406
442
  rescue IO::WaitReadable
407
443
  return false
408
444
  rescue SystemCallError, IOError
@@ -434,7 +470,7 @@ module Puma
434
470
  def read_chunked_body
435
471
  while true
436
472
  begin
437
- chunk = @io.read_nonblock(4096)
473
+ chunk = @io.read_nonblock(4096, @read_buffer)
438
474
  rescue IO::WaitReadable
439
475
  return false
440
476
  rescue SystemCallError, IOError
@@ -514,7 +550,7 @@ module Puma
514
550
  # Puma doesn't process chunk extensions, but should parse if they're
515
551
  # present, which is the reason for the semicolon regex
516
552
  chunk_hex = line.strip[/\A[^;]+/]
517
- if chunk_hex =~ CHUNK_SIZE_INVALID
553
+ if CHUNK_SIZE_INVALID.match? chunk_hex
518
554
  raise HttpParserError, "Invalid chunk size: '#{chunk_hex}'"
519
555
  end
520
556
  len = chunk_hex.to_i(16)
@@ -583,10 +619,14 @@ module Puma
583
619
 
584
620
  def set_ready
585
621
  if @body_read_start
586
- @env['puma.request_body_wait'] = Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond) - @body_read_start
622
+ @env['puma.request_body_wait'] = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond) - @body_read_start
587
623
  end
588
624
  @requests_served += 1
589
625
  @ready = true
590
626
  end
627
+
628
+ def above_http_content_limit(value)
629
+ @http_content_length_limit&.< value
630
+ end
591
631
  end
592
632
  end
@@ -2,27 +2,29 @@
2
2
 
3
3
  module Puma
4
4
  class Cluster < Puma::Runner
5
+ #—————————————————————— DO NOT USE — this class is for internal use only ———
6
+
7
+
5
8
  # This class is instantiated by the `Puma::Cluster` and represents a single
6
9
  # worker process.
7
10
  #
8
11
  # At the core of this class is running an instance of `Puma::Server` which
9
12
  # gets created via the `start_server` method from the `Puma::Runner` class
10
13
  # that this inherits from.
11
- class Worker < Puma::Runner
14
+ class Worker < Puma::Runner # :nodoc:
12
15
  attr_reader :index, :master
13
16
 
14
17
  def initialize(index:, master:, launcher:, pipes:, server: nil)
15
- super launcher, launcher.events
18
+ super(launcher)
16
19
 
17
20
  @index = index
18
21
  @master = master
19
- @launcher = launcher
20
- @options = launcher.options
21
22
  @check_pipe = pipes[:check_pipe]
22
23
  @worker_write = pipes[:worker_write]
23
24
  @fork_pipe = pipes[:fork_pipe]
24
25
  @wakeup = pipes[:wakeup]
25
26
  @server = server
27
+ @hook_data = {}
26
28
  end
27
29
 
28
30
  def run
@@ -52,13 +54,14 @@ module Puma
52
54
 
53
55
  # Invoke any worker boot hooks so they can get
54
56
  # things in shape before booting the app.
55
- @launcher.config.run_hooks :before_worker_boot, index, @launcher.events
57
+ @config.run_hooks(:before_worker_boot, index, @log_writer, @hook_data)
56
58
 
57
59
  begin
58
60
  server = @server ||= start_server
59
61
  rescue Exception => e
60
62
  log "! Unable to start worker"
61
- log e.backtrace[0]
63
+ log e
64
+ log e.backtrace.join("\n ")
62
65
  exit 1
63
66
  end
64
67
 
@@ -83,8 +86,7 @@ module Puma
83
86
  if restart_server.length > 0
84
87
  restart_server.clear
85
88
  server.begin_restart(true)
86
- @launcher.config.run_hooks :before_refork, nil, @launcher.events
87
- Puma::Util.nakayoshi_gc @events if @options[:nakayoshi_fork]
89
+ @config.run_hooks(:before_refork, nil, @log_writer, @hook_data)
88
90
  end
89
91
  elsif idx == 0 # restart server
90
92
  restart_server << true << false
@@ -113,6 +115,11 @@ module Puma
113
115
 
114
116
  while restart_server.pop
115
117
  server_thread = server.run
118
+
119
+ if @log_writer.debug? && index == 0
120
+ debug_loaded_extensions "Loaded Extensions - worker 0:"
121
+ end
122
+
116
123
  stat_thread ||= Thread.new(@worker_write) do |io|
117
124
  Puma.set_thread_name "stat pld"
118
125
  base_payload = "p#{Process.pid}"
@@ -138,7 +145,7 @@ module Puma
138
145
 
139
146
  # Invoke any worker shutdown hooks so they can prevent the worker
140
147
  # exiting until any background operations are completed
141
- @launcher.config.run_hooks :before_worker_shutdown, index, @launcher.events
148
+ @config.run_hooks(:before_worker_shutdown, index, @log_writer, @hook_data)
142
149
  ensure
143
150
  @worker_write << "t#{Process.pid}\n" rescue nil
144
151
  @worker_write.close
@@ -147,7 +154,7 @@ module Puma
147
154
  private
148
155
 
149
156
  def spawn_worker(idx)
150
- @launcher.config.run_hooks :before_worker_fork, idx, @launcher.events
157
+ @config.run_hooks(:before_worker_fork, idx, @log_writer, @hook_data)
151
158
 
152
159
  pid = fork do
153
160
  new_worker = Worker.new index: idx,
@@ -165,7 +172,7 @@ module Puma
165
172
  exit! 1
166
173
  end
167
174
 
168
- @launcher.config.run_hooks :after_worker_fork, idx, @launcher.events
175
+ @config.run_hooks(:after_worker_fork, idx, @log_writer, @hook_data)
169
176
  pid
170
177
  end
171
178
  end