puma 8.1.4 → 8.2.0.pre1

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 (55) hide show
  1. checksums.yaml +4 -4
  2. data/History.md +114 -19
  3. data/README.md +4 -3
  4. data/docs/GLOSSARY.md +160 -0
  5. data/docs/architecture.md +41 -42
  6. data/docs/binding.md +3 -0
  7. data/docs/cluster_mode.md +21 -21
  8. data/docs/control_server.md +4 -4
  9. data/docs/deployment/nginx.md +2 -0
  10. data/docs/deployment/systemd.md +15 -0
  11. data/docs/puma_support.md +12 -0
  12. data/docs/restart.md +2 -2
  13. data/docs/signals.md +3 -3
  14. data/docs/ssl.md +65 -0
  15. data/docs/upgrading/5.0-Upgrade.md +1 -1
  16. data/docs/upgrading/6.0-Upgrade.md +1 -1
  17. data/docs/upgrading/7.0-Upgrade.md +1 -1
  18. data/docs/upgrading/8.0-Upgrade.md +1 -1
  19. data/ext/puma_http11/extconf.rb +7 -5
  20. data/ext/puma_http11/http11_parser.c +66 -84
  21. data/ext/puma_http11/http11_parser.h +0 -5
  22. data/ext/puma_http11/mini_ssl.c +17 -61
  23. data/ext/puma_http11/puma_http11.c +125 -7
  24. data/lib/puma/app/status.rb +22 -7
  25. data/lib/puma/binder.rb +0 -1
  26. data/lib/puma/cli.rb +1 -3
  27. data/lib/puma/client.rb +114 -56
  28. data/lib/puma/client_env.rb +9 -11
  29. data/lib/puma/cluster/worker.rb +0 -6
  30. data/lib/puma/cluster.rb +3 -14
  31. data/lib/puma/cluster_accept_loop_delay.rb +18 -7
  32. data/lib/puma/commonlogger.rb +3 -4
  33. data/lib/puma/configuration.rb +4 -1
  34. data/lib/puma/const.rb +5 -5
  35. data/lib/puma/control_cli.rb +5 -7
  36. data/lib/puma/detect.rb +1 -1
  37. data/lib/puma/dsl.rb +70 -14
  38. data/lib/puma/http_parser_error.rb +8 -0
  39. data/lib/puma/launcher/bundle_pruner.rb +3 -21
  40. data/lib/puma/launcher.rb +13 -5
  41. data/lib/puma/minissl/context_builder.rb +3 -2
  42. data/lib/puma/minissl.rb +3 -12
  43. data/lib/puma/plugin/systemd.rb +69 -3
  44. data/lib/puma/rack/urlmap.rb +2 -2
  45. data/lib/puma/reactor.rb +138 -25
  46. data/lib/puma/response.rb +52 -33
  47. data/lib/puma/runner.rb +4 -1
  48. data/lib/puma/sd_notify.rb +21 -0
  49. data/lib/puma/server.rb +14 -14
  50. data/lib/puma/single.rb +0 -1
  51. data/lib/puma/thread_pool.rb +87 -14
  52. data/lib/puma/util.rb +5 -16
  53. data/lib/puma.rb +22 -5
  54. data/lib/rack/handler/puma.rb +1 -5
  55. metadata +4 -1
@@ -13,9 +13,54 @@ Puma::Plugin.create do
13
13
 
14
14
  launcher.log_writer.log "* Enabling systemd notification integration"
15
15
 
16
+ extend_timeout_usec = Puma::SdNotify.extend_timeout_usec
17
+ if extend_timeout_usec.positive?
18
+ extend_timeout_max_usec = Puma::SdNotify.extend_timeout_max_usec
19
+ extend_sleep_time = extend_timeout_usec / usec_per_sec / 2
20
+ extend_deadline = monotonic_time + extend_timeout_max_usec / usec_per_sec if extend_timeout_max_usec.positive?
21
+
22
+ pipe_r, @extend_stop = IO.pipe
23
+
24
+ launcher.log_writer.log "Extending systemd startup timeout every #{extend_sleep_time.round(1)} sec"
25
+
26
+ @extend_thread = Thread.new do
27
+ Puma.set_thread_name "systemd extend"
28
+ begin
29
+ loop do
30
+ break if IO.select([pipe_r], nil, nil, 0)
31
+
32
+ usec = extend_timeout_usec
33
+ if extend_deadline
34
+ remaining_usec = ((extend_deadline - monotonic_time) * usec_per_sec).ceil
35
+ break unless remaining_usec.positive?
36
+
37
+ usec = [usec, remaining_usec].min
38
+ end
39
+
40
+ begin
41
+ Puma::SdNotify.extend_timeout(usec)
42
+ rescue Puma::SdNotify::NotifyError
43
+ break
44
+ end
45
+ break if usec < extend_timeout_usec
46
+
47
+ break if IO.select([pipe_r], nil, nil, extend_sleep_time)
48
+ end
49
+ ensure
50
+ pipe_r.close
51
+ end
52
+ end
53
+ end
54
+
16
55
  # hook_events
17
- launcher.events.after_booted { Puma::SdNotify.ready }
18
- launcher.events.after_stopped { Puma::SdNotify.stopping }
56
+ launcher.events.after_booted do
57
+ stop_extending
58
+ Puma::SdNotify.ready
59
+ end
60
+ launcher.events.after_stopped do
61
+ stop_extending
62
+ Puma::SdNotify.stopping
63
+ end
19
64
  launcher.events.before_restart { Puma::SdNotify.reloading }
20
65
 
21
66
  # start watchdog
@@ -59,10 +104,31 @@ Puma::Plugin.create do
59
104
 
60
105
  private
61
106
 
107
+ def usec_per_sec
108
+ 1_000_000.0
109
+ end
110
+
111
+ def stop_extending
112
+ stop = @extend_stop
113
+ thread = @extend_thread
114
+ @extend_stop = nil
115
+ @extend_thread = nil
116
+
117
+ begin
118
+ stop&.close
119
+ rescue IOError
120
+ end
121
+ thread&.join
122
+ end
123
+
124
+ def monotonic_time
125
+ Process.clock_gettime(Process::CLOCK_MONOTONIC)
126
+ end
127
+
62
128
  def watchdog_sleep_time
63
129
  usec = Integer(ENV["WATCHDOG_USEC"])
64
130
 
65
- sec_f = usec / 1_000_000.0
131
+ sec_f = usec / usec_per_sec
66
132
  # "It is recommended that a daemon sends a keep-alive notification message
67
133
  # to the service manager every half of the time returned here."
68
134
  sec_f / 2
@@ -14,8 +14,8 @@ module Puma::Rack
14
14
  # first, since they are most specific.
15
15
 
16
16
  class URLMap
17
- NEGATIVE_INFINITY = -1.0 / 0.0
18
- INFINITY = 1.0 / 0.0
17
+ NEGATIVE_INFINITY = -Float::INFINITY
18
+ INFINITY = Float::INFINITY
19
19
 
20
20
  def initialize(map = {})
21
21
  remap(map)
data/lib/puma/reactor.rb CHANGED
@@ -33,8 +33,13 @@ module Puma
33
33
 
34
34
  @selector = ::NIO::Selector.new(NIO::Selector.backends.delete(backend))
35
35
  @input = Queue.new
36
- @timeouts = []
37
- @timeout_keys = {}.compare_by_identity
36
+ @timeouts = {}.compare_by_identity
37
+ @early_timeouts = {}.compare_by_identity
38
+ @earliest = nil
39
+ @early_earliest = nil
40
+ @last_timeout_at = 0.0
41
+ @early_last_timeout_at = 0.0
42
+ @needs_early_sort = false
38
43
  @block = block
39
44
  @reactor_size = 0
40
45
  @reactor_max = 0
@@ -78,25 +83,51 @@ module Puma
78
83
  def select_loop
79
84
  begin
80
85
  until @input.closed? && @input.empty?
81
- requeue_moved_timeouts
86
+ @earliest ||= @timeouts.each_key { |k| break k } unless @timeouts.empty?
87
+ @early_earliest ||= @early_timeouts.each_key { |k| break k } unless @early_timeouts.empty?
88
+
89
+ if @needs_early_sort ||
90
+ (@earliest && @timeouts[@earliest] < @earliest.timeout_at) ||
91
+ (@early_earliest && @early_timeouts[@early_earliest] < @early_earliest.timeout_at)
92
+ if !@needs_early_sort && @early_earliest.nil? && @timeouts.size == 1
93
+ @timeouts[@earliest] = @last_timeout_at = @earliest.timeout_at
94
+ else
95
+ requeue_moved_timeouts
96
+ @earliest ||= @timeouts.each_key { |k| break k } unless @timeouts.empty?
97
+ @early_earliest ||= @early_timeouts.each_key { |k| break k } unless @early_timeouts.empty?
98
+ end
99
+ end
82
100
 
83
101
  # Wakeup any registered object that receives incoming data.
84
102
  # Block until the earliest timeout or Selector#wakeup is called.
85
- timeout = (earliest = @timeouts.first) && earliest.timeout
103
+ timeout = if @early_earliest
104
+ t1 = @earliest&.timeout
105
+ t2 = @early_earliest.timeout
106
+ t1 && t1 < t2 ? t1 : t2
107
+ else
108
+ @earliest&.timeout
109
+ end
110
+
86
111
  @selector.select(timeout) do |monitor|
87
112
  monitor.interests = interests(monitor.value) unless wakeup!(monitor.value)
88
113
  end
89
114
 
90
115
  # Wakeup all objects that timed out.
91
- timed_out = @timeouts.take_while { |client| client.timeout == 0 }
92
- timed_out.each { |client| wakeup!(client) }
116
+ unless @early_timeouts.empty?
117
+ @early_earliest ||= @early_timeouts.each_key { |k| break k }
118
+ expire_timeouts(@early_timeouts) if @early_earliest.timeout == 0
119
+ end
120
+ unless @timeouts.empty?
121
+ @earliest ||= @timeouts.each_key { |k| break k }
122
+ expire_timeouts(@timeouts) if @earliest.timeout == 0
123
+ end
93
124
 
94
125
  unless @input.empty?
95
126
  until @input.empty?
96
127
  client = @input.pop
97
128
  register(client) if client.io_ok?
98
129
  end
99
- @timeouts.sort_by! { |client| @timeout_keys[client] }
130
+ sort_early_timeouts! if @needs_early_sort
100
131
  end
101
132
  end
102
133
  rescue StandardError => e
@@ -107,21 +138,88 @@ module Puma
107
138
  end
108
139
 
109
140
  # Wakeup all remaining objects on shutdown.
110
- @timeouts.each(&@block)
141
+ @early_timeouts.each_key(&@block)
142
+ @timeouts.each_key(&@block)
111
143
  @selector.close
112
144
  end
113
145
 
146
+ def expire_timeouts(map)
147
+ timed_out = []
148
+ map.each_key do |client|
149
+ break unless client.timeout == 0
150
+ timed_out << client
151
+ end
152
+ timed_out.each { |client| wakeup!(client) }
153
+ if map.equal?(@timeouts)
154
+ @earliest = (@timeouts.each_key { |k| break k } unless @timeouts.empty?)
155
+ else
156
+ @early_earliest = (@early_timeouts.each_key { |k| break k } unless @early_timeouts.empty?)
157
+ end
158
+ end
159
+
114
160
  # Start monitoring the object.
115
161
  def register(client)
116
162
  @selector.register(client.to_io, interests(client)).value = client
117
163
  @reactor_size += 1
118
164
  @reactor_max = @reactor_size if @reactor_max < @reactor_size
119
- @timeouts << client
120
- @timeout_keys[client] = client.timeout_at
165
+ t_at = client.timeout_at
166
+ if @timeouts.empty? || t_at >= @last_timeout_at
167
+ @earliest = client if @timeouts.empty?
168
+ @last_timeout_at = t_at
169
+ @timeouts[client] = t_at
170
+ else
171
+ if @early_timeouts.empty?
172
+ @early_earliest = client
173
+ @early_last_timeout_at = t_at
174
+ elsif t_at < @early_last_timeout_at
175
+ @needs_early_sort = true
176
+ else
177
+ @early_last_timeout_at = t_at
178
+ end
179
+ @early_timeouts[client] = t_at
180
+ end
121
181
  rescue ArgumentError
122
182
  # unreadable clients raise error when processed by NIO
123
183
  end
124
184
 
185
+ def insert_timeout(client)
186
+ t_at = client.timeout_at
187
+ if @timeouts.empty? || t_at >= @last_timeout_at
188
+ @earliest = client if @timeouts.empty?
189
+ @last_timeout_at = t_at
190
+ @timeouts[client] = t_at
191
+ else
192
+ if @early_timeouts.empty?
193
+ @early_earliest = client
194
+ @early_last_timeout_at = t_at
195
+ elsif t_at < @early_last_timeout_at
196
+ @needs_early_sort = true
197
+ else
198
+ @early_last_timeout_at = t_at
199
+ end
200
+ @early_timeouts[client] = t_at
201
+ end
202
+ end
203
+
204
+ def delete_timeout(client)
205
+ if @timeouts.delete(client)
206
+ @earliest = nil if @earliest.equal?(client)
207
+ @last_timeout_at = 0.0 if @timeouts.empty?
208
+ elsif @early_timeouts.delete(client)
209
+ @early_earliest = nil if @early_earliest.equal?(client)
210
+ @early_last_timeout_at = 0.0 if @early_timeouts.empty?
211
+ end
212
+ end
213
+
214
+ def sort_early_timeouts!
215
+ sorted = @early_timeouts.sort_by { |_client, t_at| t_at }
216
+ @early_timeouts.clear
217
+ sorted.each { |c, t_at| @early_timeouts[c] = t_at }
218
+ @early_earliest = sorted.first&.first
219
+ @early_last_timeout_at = sorted.last&.last || 0.0
220
+ @needs_early_sort = false
221
+ end
222
+
125
223
  def interests(client)
126
224
  client.write_pending? ? :w : :r
127
225
  end
@@ -132,32 +230,47 @@ module Puma
132
230
  if @block.call client
133
231
  @selector.deregister client.to_io
134
232
  @reactor_size -= 1
135
- @timeouts.delete client
136
- @timeout_keys.delete client
233
+ if @timeouts.delete(client)
234
+ @earliest = nil if @earliest.equal?(client)
235
+ @last_timeout_at = 0.0 if @timeouts.empty?
236
+ elsif @early_timeouts.delete(client)
237
+ @early_earliest = nil if @early_earliest.equal?(client)
238
+ @early_last_timeout_at = 0.0 if @early_timeouts.empty?
239
+ end
137
240
  true
138
241
  else
139
- if client.timeout_at < @timeout_keys[client]
140
- @timeouts.delete client
141
- requeue client
242
+ queued_at = @timeouts[client] || @early_timeouts[client]
243
+ if queued_at && client.timeout_at < queued_at
244
+ delete_timeout(client)
245
+ insert_timeout(client)
246
+ sort_early_timeouts! if @needs_early_sort
142
247
  end
143
248
  false
144
249
  end
145
250
  end
146
251
 
147
- # `@timeouts` is sorted by the timeout each client had when it was queued.
252
+ # Timeouts are ordered by the timeout each client had when it was queued.
148
253
  # Timeouts that moved earlier are requeued in `wakeup!`. Timeouts that moved
149
- # later are requeued here, once the client reaches the front.
254
+ # later are requeued here, once the client reaches the front of its lane.
150
255
  def requeue_moved_timeouts
151
- while (client = @timeouts.first) && @timeout_keys[client] < client.timeout_at
152
- @timeouts.shift
153
- requeue client
256
+ sort_early_timeouts! if @needs_early_sort
257
+
258
+ unless @timeouts.empty?
259
+ while (client = (@earliest ||= @timeouts.each_key { |k| break k })) && @timeouts[client] < client.timeout_at
260
+ delete_timeout(client)
261
+ insert_timeout(client)
262
+ break if @timeouts.empty?
263
+ end
154
264
  end
155
- end
156
265
 
157
- def requeue(client)
158
- key = @timeout_keys[client] = client.timeout_at
159
- index = @timeouts.bsearch_index { |c| @timeout_keys[c] > key } || @timeouts.size
160
- @timeouts.insert index, client
266
+ unless @early_timeouts.empty?
267
+ while (client = (@early_earliest ||= @early_timeouts.each_key { |k| break k })) && @early_timeouts[client] < client.timeout_at
268
+ delete_timeout(client)
269
+ insert_timeout(client)
270
+ break if @early_timeouts.empty?
271
+ end
272
+ sort_early_timeouts! if @needs_early_sort
273
+ end
161
274
  end
162
275
  end
163
276
  end
data/lib/puma/response.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "time"
4
+
3
5
  module Puma
4
6
  #———————————————————————— DO NOT USE — this class is for internal use only ———
5
7
 
@@ -20,7 +22,7 @@ module Puma
20
22
  BODY_LEN_MAX = 1_024 * 256
21
23
 
22
24
  # File body: smaller bodies are combined with io_buffer, then written to
23
- # socket. Larger bodies are written separately using `copy_stream`
25
+ # socket. Larger bodies are written separately in chunks.
24
26
  IO_BODY_MAX = 1_024 * 64
25
27
 
26
28
  # Array body: elements are collected in io_buffer. When io_buffer's size
@@ -45,7 +47,6 @@ module Puma
45
47
  # It'll return +:async+ if the connection remains open but will be handled
46
48
  # elsewhere, i.e. the connection has been hijacked by the Rack application.
47
49
  #
48
- # Finally, it'll return +true+ on keep-alive connections.
49
50
  # @param processor [Puma::ThreadPool::ProcessorThread]
50
51
  # @param client [Puma::Client]
51
52
  # @param requests [Integer]
@@ -156,7 +157,7 @@ module Puma
156
157
  # Close the connection after a reasonable number of inline requests
157
158
  force_keep_alive = @enable_keep_alives && client.requests_served < @max_keep_alive
158
159
 
159
- resp_info = str_headers(env, status, headers, res_body, io_buffer, force_keep_alive)
160
+ resp_info = str_headers(env, status, headers, io_buffer, force_keep_alive)
160
161
 
161
162
  close_body = false
162
163
  response_hijack = nil
@@ -183,7 +184,7 @@ module Puma
183
184
  body = res_body
184
185
  end
185
186
  elsif !res_body.is_a?(::File) && res_body.respond_to?(:to_path) &&
186
- (fn = res_body.to_path) && File.readable?(fn = res_body.to_path)
187
+ (fn = res_body.to_path) && File.readable?(fn)
187
188
  body = File.open fn, 'rb'
188
189
  close_body = true
189
190
  content_length = body.size
@@ -255,7 +256,7 @@ module Puma
255
256
  return :async
256
257
  end
257
258
 
258
- fast_write_response socket, body, io_buffer, chunked, content_length.to_i
259
+ fast_write_response socket, body, io_buffer, chunked, content_length
259
260
 
260
261
  # if we're shutting down, close keep_alive connections
261
262
  !shutting_down? && keep_alive ? :keep_alive : :close
@@ -302,27 +303,20 @@ module Puma
302
303
  # @param body [Enumerable, File] the body object
303
304
  # @param io_buffer [Puma::IOBuffer] contains headers
304
305
  # @param chunked [Boolean]
305
- # @paramn content_length [Integer
306
+ # @param content_length [Integer, nil]
306
307
  # @raise [ConnectionError]
307
308
  #
308
309
  def fast_write_response(socket, body, io_buffer, chunked, content_length)
309
- if body.is_a?(::File) && body.respond_to?(:read)
310
- if chunked # would this ever happen?
311
- while chunk = body.read(BODY_LEN_MAX)
312
- io_buffer.append chunk.bytesize.to_s(16), LINE_END, chunk, LINE_END
313
- end
314
- fast_write_str socket, CLOSE_CHUNKED
310
+ if body.is_a?(::File) && content_length && body.respond_to?(:read)
311
+ if content_length <= IO_BODY_MAX
312
+ io_buffer.write body.read(content_length)
313
+ fast_write_str socket, io_buffer.read_and_reset
315
314
  else
316
- if content_length <= IO_BODY_MAX
317
- io_buffer.write body.read(content_length)
318
- fast_write_str socket, io_buffer.read_and_reset
319
- else
320
- fast_write_str socket, io_buffer.read_and_reset
321
- remaining = content_length
322
- while remaining > 0 && (chunk = body.read([remaining, BODY_LEN_MAX].min))
323
- fast_write_str socket, chunk
324
- remaining -= chunk.bytesize
325
- end
315
+ fast_write_str socket, io_buffer.read_and_reset
316
+ remaining = content_length
317
+ while remaining > 0 && (chunk = body.read([remaining, BODY_LEN_MAX].min))
318
+ fast_write_str socket, chunk
319
+ remaining -= chunk.bytesize
326
320
  end
327
321
  end
328
322
  elsif body.is_a?(::Array) && body.length == 1
@@ -400,14 +394,14 @@ module Puma
400
394
  # @return [Boolean]
401
395
  #
402
396
  def illegal_header_key?(header_key)
403
- !!(ILLEGAL_HEADER_KEY_REGEX =~ header_key.to_s)
397
+ ILLEGAL_HEADER_KEY_REGEX.match?(header_key.to_s)
404
398
  end
405
399
 
406
400
  # @param header_value [#to_s]
407
401
  # @return [Boolean]
408
402
  #
409
403
  def illegal_header_value?(header_value)
410
- !!(ILLEGAL_HEADER_VALUE_REGEX =~ header_value.to_s)
404
+ ILLEGAL_HEADER_VALUE_REGEX.match?(header_value.to_s)
411
405
  end
412
406
 
413
407
  # Used in the lambda for env[ `Puma::Const::EARLY_HINTS` ]
@@ -425,8 +419,6 @@ module Puma
425
419
  next if illegal_header_value?(v)
426
420
  eh_str << "#{k}: #{v}\r\n"
427
421
  end
428
- elsif !(vs.to_s.empty? || !illegal_header_value?(vs))
429
- eh_str << "#{k}: #{vs}\r\n"
430
422
  end
431
423
  end
432
424
  eh_str.freeze
@@ -442,7 +434,9 @@ module Puma
442
434
  private :fetch_status_code
443
435
 
444
436
  def request_has_close?(env)
445
- conn = env.fetch(HTTP_CONNECTION, "").downcase
437
+ return false unless (conn = env[HTTP_CONNECTION])
438
+
439
+ conn = conn.downcase
446
440
  conn.include?(CLOSE) && conn.split(",").any? { |option| option.strip == CLOSE }
447
441
  end
448
442
  private :request_has_close?
@@ -451,15 +445,13 @@ module Puma
451
445
  # @param env [Hash] see Puma::Client#env, from request
452
446
  # @param status [Integer] the status returned by the Rack application
453
447
  # @param headers [Hash] the headers returned by the Rack application
454
- # @param content_length [Integer,nil] content length if it can be determined from the
455
- # response body
456
- # @param io_buffer [Puma::IOBuffer] modified inn place
448
+ # @param io_buffer [Puma::IOBuffer] modified in place
457
449
  # @param force_keep_alive [Boolean] 'anded' with keep_alive, based on system
458
450
  # status and `@max_keep_alive`
459
451
  # @return [Hash] resp_info
460
452
  # @version 5.0.3
461
453
  #
462
- def str_headers(env, status, headers, res_body, io_buffer, force_keep_alive)
454
+ def str_headers(env, status, headers, io_buffer, force_keep_alive)
463
455
 
464
456
  line_ending = LINE_END
465
457
  colon = COLON
@@ -467,7 +459,8 @@ module Puma
467
459
  resp_info = {}
468
460
  resp_info[:no_body] = env[REQUEST_METHOD] == HEAD
469
461
 
470
- http_11 = env[SERVER_PROTOCOL] == HTTP_11
462
+ proto = env[SERVER_PROTOCOL]
463
+ http_11 = proto == HTTP_11 || status == 505 || (proto != HTTP_10 && proto&.start_with?('HTTP/1.'))
471
464
  if http_11
472
465
  resp_info[:allow_chunked] = true
473
466
  resp_info[:keep_alive] = !request_has_close?(env)
@@ -485,7 +478,8 @@ module Puma
485
478
  end
486
479
  else
487
480
  resp_info[:allow_chunked] = false
488
- resp_info[:keep_alive] = env.fetch(HTTP_CONNECTION, "").downcase == KEEP_ALIVE
481
+ conn = env[HTTP_CONNECTION]
482
+ resp_info[:keep_alive] = conn ? conn.downcase == KEEP_ALIVE : false
489
483
 
490
484
  # Same optimization as above for HTTP/1.1
491
485
  #
@@ -507,6 +501,7 @@ module Puma
507
501
  resp_info[:keep_alive] &&= force_keep_alive
508
502
 
509
503
  resp_info[:response_hijack] = nil
504
+ has_date = false
510
505
 
511
506
  headers.each do |k, vs|
512
507
  next if illegal_header_key?(k)
@@ -525,10 +520,17 @@ module Puma
525
520
  when HIJACK
526
521
  resp_info[:response_hijack] = vs
527
522
  next
523
+ when DATE
524
+ has_date = true
528
525
  when BANNED_HEADER_KEY
529
526
  next
530
527
  end
531
528
 
529
+ if vs.is_a?(::String) && (vs.empty? || !illegal_header_value?(vs))
530
+ io_buffer.append key, colon, vs, line_ending
531
+ next
532
+ end
533
+
532
534
  ary = if vs.is_a?(::Array) && !vs.empty?
533
535
  vs
534
536
  elsif vs.respond_to?(:to_s) && !vs.to_s.empty?
@@ -546,6 +548,10 @@ module Puma
546
548
  end
547
549
  end
548
550
 
551
+ # RFC 9110 §6.6.1: an origin server with a clock MUST send a Date header
552
+ # in 2xx/3xx/4xx responses and MUST NOT send one in 1xx responses
553
+ io_buffer << cached_date_header if @date_header && !has_date && status >= 200
554
+
549
555
  # HTTP/1.1 & 1.0 assume different defaults:
550
556
  # - HTTP 1.0 assumes the connection will be closed if not specified
551
557
  # - HTTP 1.1 assumes the connection will be kept alive if not specified.
@@ -558,5 +564,18 @@ module Puma
558
564
  resp_info
559
565
  end
560
566
  private :str_headers
567
+
568
+ def cached_date_header
569
+ sec = Process.clock_gettime(Process::CLOCK_REALTIME, :second)
570
+ thread = Thread.current
571
+ if thread.thread_variable_get(:puma_date_sec) == sec
572
+ thread.thread_variable_get(:puma_date_header)
573
+ else
574
+ header = -"date: #{Time.at(sec).utc.httpdate}\r\n"
575
+ thread.thread_variable_set(:puma_date_sec, sec)
576
+ thread.thread_variable_set(:puma_date_header, header)
577
+ end
578
+ end
579
+ private :cached_date_header
561
580
  end
562
581
  end
data/lib/puma/runner.rb CHANGED
@@ -70,7 +70,10 @@ module Puma
70
70
  token = nil if token.empty? || token == 'none'
71
71
  end
72
72
 
73
- app = Puma::App::Status.new @launcher, token: token, data_only: @options[:control_data_only]
73
+ app = Puma::App::Status.new @launcher,
74
+ token: token,
75
+ data_only: @options[:control_data_only],
76
+ data_token: @options[:control_data_token]
74
77
 
75
78
  # A Reactor is not created and nio4r is not loaded when 'queue_requests: false'
76
79
  # Use `nil` for events, no hooks in control server
@@ -48,6 +48,7 @@ module Puma
48
48
  MAINPID = "MAINPID="
49
49
  WATCHDOG = "WATCHDOG=1"
50
50
  FDSTORE = "FDSTORE=1"
51
+ EXTEND_TIMEOUT_USEC = "EXTEND_TIMEOUT_USEC="
51
52
 
52
53
  # @note When the service manager expects watchdog pings, this sends an
53
54
  # immediate `WATCHDOG=1` in the same datagram, which closes the watchdog
@@ -88,6 +89,26 @@ module Puma
88
89
  notify(FDSTORE, unset_env)
89
90
  end
90
91
 
92
+ def self.extend_timeout(usec, unset_env=false)
93
+ notify("#{EXTEND_TIMEOUT_USEC}#{usec}", unset_env)
94
+ end
95
+
96
+ def self.extend_timeout_usec
97
+ env_seconds_to_usec("PUMA_EXTEND_TIMEOUT_SEC")
98
+ end
99
+
100
+ def self.extend_timeout_max_usec
101
+ env_seconds_to_usec("PUMA_EXTEND_TIMEOUT_MAX_SEC")
102
+ end
103
+
104
+ def self.env_seconds_to_usec(name)
105
+ seconds = Float(ENV[name])
106
+ seconds.positive? ? (seconds * 1_000_000).ceil : 0
107
+ rescue ArgumentError, TypeError, FloatDomainError
108
+ 0
109
+ end
110
+ private_class_method :env_seconds_to_usec
111
+
91
112
  # @param [Boolean] true if the service manager expects watchdog keep-alive
92
113
  # notification messages to be sent from this process.
93
114
  #