puma 4.1.0 → 4.2.1
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.
- checksums.yaml +4 -4
- data/History.md +44 -9
- data/README.md +7 -16
- data/docs/plugins.md +20 -10
- data/ext/puma_http11/http11_parser.c +37 -62
- data/ext/puma_http11/http11_parser_common.rl +3 -3
- data/lib/puma.rb +6 -0
- data/lib/puma/accept_nonblock.rb +5 -1
- data/lib/puma/app/status.rb +29 -28
- data/lib/puma/binder.rb +36 -12
- data/lib/puma/cli.rb +4 -0
- data/lib/puma/client.rb +194 -206
- data/lib/puma/cluster.rb +41 -45
- data/lib/puma/const.rb +15 -18
- data/lib/puma/control_cli.rb +11 -2
- data/lib/puma/dsl.rb +17 -1
- data/lib/puma/events.rb +2 -2
- data/lib/puma/launcher.rb +87 -46
- data/lib/puma/plugin.rb +5 -2
- data/lib/puma/reactor.rb +5 -4
- data/lib/puma/runner.rb +3 -3
- data/lib/puma/server.rb +14 -11
- data/lib/puma/thread_pool.rb +9 -31
- data/lib/rack/handler/puma.rb +0 -2
- data/tools/docker/Dockerfile +16 -0
- data/tools/trickletest.rb +0 -1
- metadata +4 -4
- data/lib/puma/daemon_ext.rb +0 -33
- data/lib/puma/delegation.rb +0 -13
data/lib/puma.rb
CHANGED
data/lib/puma/accept_nonblock.rb
CHANGED
data/lib/puma/app/status.rb
CHANGED
@@ -7,26 +7,11 @@ module Puma
|
|
7
7
|
# Check out {#call}'s source code to see what actions this web application
|
8
8
|
# can respond to.
|
9
9
|
class Status
|
10
|
-
def initialize(cli)
|
11
|
-
@cli = cli
|
12
|
-
@auth_token = nil
|
13
|
-
end
|
14
10
|
OK_STATUS = '{ "status": "ok" }'.freeze
|
15
11
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
return true unless @auth_token
|
20
|
-
env['QUERY_STRING'].to_s.split(/&;/).include?("token=#{@auth_token}")
|
21
|
-
end
|
22
|
-
|
23
|
-
def rack_response(status, body, content_type='application/json')
|
24
|
-
headers = {
|
25
|
-
'Content-Type' => content_type,
|
26
|
-
'Content-Length' => body.bytesize.to_s
|
27
|
-
}
|
28
|
-
|
29
|
-
[status, headers, [body]]
|
12
|
+
def initialize(cli, token = nil)
|
13
|
+
@cli = cli
|
14
|
+
@auth_token = token
|
30
15
|
end
|
31
16
|
|
32
17
|
def call(env)
|
@@ -37,43 +22,59 @@ module Puma
|
|
37
22
|
case env['PATH_INFO']
|
38
23
|
when /\/stop$/
|
39
24
|
@cli.stop
|
40
|
-
|
25
|
+
rack_response(200, OK_STATUS)
|
41
26
|
|
42
27
|
when /\/halt$/
|
43
28
|
@cli.halt
|
44
|
-
|
29
|
+
rack_response(200, OK_STATUS)
|
45
30
|
|
46
31
|
when /\/restart$/
|
47
32
|
@cli.restart
|
48
|
-
|
33
|
+
rack_response(200, OK_STATUS)
|
49
34
|
|
50
35
|
when /\/phased-restart$/
|
51
36
|
if !@cli.phased_restart
|
52
|
-
|
37
|
+
rack_response(404, '{ "error": "phased restart not available" }')
|
53
38
|
else
|
54
|
-
|
39
|
+
rack_response(200, OK_STATUS)
|
55
40
|
end
|
56
41
|
|
57
42
|
when /\/reload-worker-directory$/
|
58
43
|
if !@cli.send(:reload_worker_directory)
|
59
|
-
|
44
|
+
rack_response(404, '{ "error": "reload_worker_directory not available" }')
|
60
45
|
else
|
61
|
-
|
46
|
+
rack_response(200, OK_STATUS)
|
62
47
|
end
|
63
48
|
|
64
49
|
when /\/gc$/
|
65
50
|
GC.start
|
66
|
-
|
51
|
+
rack_response(200, OK_STATUS)
|
67
52
|
|
68
53
|
when /\/gc-stats$/
|
69
|
-
|
54
|
+
rack_response(200, GC.stat.to_json)
|
70
55
|
|
71
56
|
when /\/stats$/
|
72
|
-
|
57
|
+
rack_response(200, @cli.stats)
|
73
58
|
else
|
74
59
|
rack_response 404, "Unsupported action", 'text/plain'
|
75
60
|
end
|
76
61
|
end
|
62
|
+
|
63
|
+
private
|
64
|
+
|
65
|
+
def authenticate(env)
|
66
|
+
return true unless @auth_token
|
67
|
+
env['QUERY_STRING'].to_s.split(/&;/).include?("token=#{@auth_token}")
|
68
|
+
end
|
69
|
+
|
70
|
+
def rack_response(status, body, content_type='application/json')
|
71
|
+
headers = {
|
72
|
+
'Content-Type' => content_type,
|
73
|
+
'Content-Length' => body.bytesize.to_s
|
74
|
+
}
|
75
|
+
|
76
|
+
[status, headers, [body]]
|
77
|
+
end
|
77
78
|
end
|
78
79
|
end
|
79
80
|
end
|
data/lib/puma/binder.rb
CHANGED
@@ -42,7 +42,7 @@ module Puma
|
|
42
42
|
@ios = []
|
43
43
|
end
|
44
44
|
|
45
|
-
attr_reader :
|
45
|
+
attr_reader :ios
|
46
46
|
|
47
47
|
def env(sock)
|
48
48
|
@envs.fetch(sock, @proto_env)
|
@@ -50,14 +50,6 @@ module Puma
|
|
50
50
|
|
51
51
|
def close
|
52
52
|
@ios.each { |i| i.close }
|
53
|
-
@unix_paths.each do |i|
|
54
|
-
# Errno::ENOENT is intermittently raised
|
55
|
-
begin
|
56
|
-
unix_socket = UNIXSocket.new i
|
57
|
-
unix_socket.close
|
58
|
-
rescue Errno::ENOENT
|
59
|
-
end
|
60
|
-
end
|
61
53
|
end
|
62
54
|
|
63
55
|
def import_from_env
|
@@ -111,7 +103,17 @@ module Puma
|
|
111
103
|
bak = params.fetch('backlog', 1024).to_i
|
112
104
|
|
113
105
|
io = add_tcp_listener uri.host, uri.port, opt, bak
|
114
|
-
|
106
|
+
|
107
|
+
@ios.each do |i|
|
108
|
+
next unless TCPServer === i
|
109
|
+
addr = if i.local_address.ipv6?
|
110
|
+
"[#{i.local_address.ip_unpack[0]}]:#{i.local_address.ip_unpack[1]}"
|
111
|
+
else
|
112
|
+
i.local_address.ip_unpack.join(':')
|
113
|
+
end
|
114
|
+
|
115
|
+
logger.log "* Listening on tcp://#{addr}"
|
116
|
+
end
|
115
117
|
end
|
116
118
|
|
117
119
|
@listeners << [str, io] if io
|
@@ -359,7 +361,7 @@ module Puma
|
|
359
361
|
# Tell the server to listen on +path+ as a UNIX domain socket.
|
360
362
|
#
|
361
363
|
def add_unix_listener(path, umask=nil, mode=nil, backlog=1024)
|
362
|
-
@unix_paths << path
|
364
|
+
@unix_paths << path unless File.exist? path
|
363
365
|
|
364
366
|
# Let anyone connect by default
|
365
367
|
umask ||= 0
|
@@ -397,7 +399,7 @@ module Puma
|
|
397
399
|
end
|
398
400
|
|
399
401
|
def inherit_unix_listener(path, fd)
|
400
|
-
@unix_paths << path
|
402
|
+
@unix_paths << path unless File.exist? path
|
401
403
|
|
402
404
|
if fd.kind_of? TCPServer
|
403
405
|
s = fd
|
@@ -413,5 +415,27 @@ module Puma
|
|
413
415
|
s
|
414
416
|
end
|
415
417
|
|
418
|
+
def close_listeners
|
419
|
+
@listeners.each do |l, io|
|
420
|
+
io.close
|
421
|
+
uri = URI.parse(l)
|
422
|
+
next unless uri.scheme == 'unix'
|
423
|
+
unix_path = "#{uri.host}#{uri.path}"
|
424
|
+
File.unlink unix_path if @unix_paths.include? unix_path
|
425
|
+
end
|
426
|
+
end
|
427
|
+
|
428
|
+
def close_unix_paths
|
429
|
+
@unix_paths.each { |up| File.unlink(up) if File.exist? up }
|
430
|
+
end
|
431
|
+
|
432
|
+
def redirects_for_restart
|
433
|
+
redirects = {:close_others => true}
|
434
|
+
@listeners.each_with_index do |(l, io), i|
|
435
|
+
ENV["PUMA_INHERIT_#{i}"] = "#{io.to_i}:#{l}"
|
436
|
+
redirects[io.to_i] = io.to_i
|
437
|
+
end
|
438
|
+
redirects
|
439
|
+
end
|
416
440
|
end
|
417
441
|
end
|
data/lib/puma/cli.rb
CHANGED
@@ -161,6 +161,10 @@ module Puma
|
|
161
161
|
user_config.prune_bundler
|
162
162
|
end
|
163
163
|
|
164
|
+
o.on "--extra-runtime-dependencies GEM1,GEM2", "Defines any extra needed gems when using --prune-bundler" do |arg|
|
165
|
+
c.extra_runtime_dependencies arg.split(',')
|
166
|
+
end
|
167
|
+
|
164
168
|
o.on "-q", "--quiet", "Do not log requests internally (default true)" do
|
165
169
|
user_config.quiet
|
166
170
|
end
|
data/lib/puma/client.rb
CHANGED
@@ -9,8 +9,8 @@ class IO
|
|
9
9
|
end
|
10
10
|
|
11
11
|
require 'puma/detect'
|
12
|
-
require 'puma/delegation'
|
13
12
|
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
|
@@ -24,11 +24,11 @@ module Puma
|
|
24
24
|
class ConnectionError < RuntimeError; end
|
25
25
|
|
26
26
|
# An instance of this class represents a unique request from a client.
|
27
|
-
# For example a web request from a browser or from CURL.
|
27
|
+
# For example, this could be a web request from a browser or from CURL.
|
28
28
|
#
|
29
29
|
# An instance of `Puma::Client` can be used as if it were an IO object
|
30
|
-
# by the reactor
|
31
|
-
# on any non-IO objects it polls. For example nio4r internally calls
|
30
|
+
# by the reactor. The reactor is expected to call `#to_io`
|
31
|
+
# on any non-IO objects it polls. For example, nio4r internally calls
|
32
32
|
# `IO::try_convert` (which may call `#to_io`) when a new socket is
|
33
33
|
# registered.
|
34
34
|
#
|
@@ -36,8 +36,12 @@ module Puma
|
|
36
36
|
# the header and body are fully buffered via the `try_to_finish` method.
|
37
37
|
# They can be used to "time out" a response via the `timeout_at` reader.
|
38
38
|
class Client
|
39
|
+
# The object used for a request with no body. All requests with
|
40
|
+
# no body share this one object since it has no state.
|
41
|
+
EmptyBody = NullIO.new
|
42
|
+
|
39
43
|
include Puma::Const
|
40
|
-
extend
|
44
|
+
extend Forwardable
|
41
45
|
|
42
46
|
def initialize(io, env=nil)
|
43
47
|
@io = io
|
@@ -79,7 +83,7 @@ module Puma
|
|
79
83
|
|
80
84
|
attr_accessor :remote_addr_header
|
81
85
|
|
82
|
-
|
86
|
+
def_delegators :@io, :closed?
|
83
87
|
|
84
88
|
def inspect
|
85
89
|
"#<Puma::Client:0x#{object_id.to_s(16)} @ready=#{@ready.inspect}>"
|
@@ -144,179 +148,6 @@ module Puma
|
|
144
148
|
end
|
145
149
|
end
|
146
150
|
|
147
|
-
# The object used for a request with no body. All requests with
|
148
|
-
# no body share this one object since it has no state.
|
149
|
-
EmptyBody = NullIO.new
|
150
|
-
|
151
|
-
def setup_chunked_body(body)
|
152
|
-
@chunked_body = true
|
153
|
-
@partial_part_left = 0
|
154
|
-
@prev_chunk = ""
|
155
|
-
|
156
|
-
@body = Tempfile.new(Const::PUMA_TMP_BASE)
|
157
|
-
@body.binmode
|
158
|
-
@tempfile = @body
|
159
|
-
|
160
|
-
return decode_chunk(body)
|
161
|
-
end
|
162
|
-
|
163
|
-
def decode_chunk(chunk)
|
164
|
-
if @partial_part_left > 0
|
165
|
-
if @partial_part_left <= chunk.size
|
166
|
-
if @partial_part_left > 2
|
167
|
-
@body << chunk[0..(@partial_part_left-3)] # skip the \r\n
|
168
|
-
end
|
169
|
-
chunk = chunk[@partial_part_left..-1]
|
170
|
-
@partial_part_left = 0
|
171
|
-
else
|
172
|
-
@body << chunk if @partial_part_left > 2 # don't include the last \r\n
|
173
|
-
@partial_part_left -= chunk.size
|
174
|
-
return false
|
175
|
-
end
|
176
|
-
end
|
177
|
-
|
178
|
-
if @prev_chunk.empty?
|
179
|
-
io = StringIO.new(chunk)
|
180
|
-
else
|
181
|
-
io = StringIO.new(@prev_chunk+chunk)
|
182
|
-
@prev_chunk = ""
|
183
|
-
end
|
184
|
-
|
185
|
-
while !io.eof?
|
186
|
-
line = io.gets
|
187
|
-
if line.end_with?("\r\n")
|
188
|
-
len = line.strip.to_i(16)
|
189
|
-
if len == 0
|
190
|
-
@in_last_chunk = true
|
191
|
-
@body.rewind
|
192
|
-
rest = io.read
|
193
|
-
last_crlf_size = "\r\n".bytesize
|
194
|
-
if rest.bytesize < last_crlf_size
|
195
|
-
@buffer = nil
|
196
|
-
@partial_part_left = last_crlf_size - rest.bytesize
|
197
|
-
return false
|
198
|
-
else
|
199
|
-
@buffer = rest[last_crlf_size..-1]
|
200
|
-
@buffer = nil if @buffer.empty?
|
201
|
-
set_ready
|
202
|
-
return true
|
203
|
-
end
|
204
|
-
end
|
205
|
-
|
206
|
-
len += 2
|
207
|
-
|
208
|
-
part = io.read(len)
|
209
|
-
|
210
|
-
unless part
|
211
|
-
@partial_part_left = len
|
212
|
-
next
|
213
|
-
end
|
214
|
-
|
215
|
-
got = part.size
|
216
|
-
|
217
|
-
case
|
218
|
-
when got == len
|
219
|
-
@body << part[0..-3] # to skip the ending \r\n
|
220
|
-
when got <= len - 2
|
221
|
-
@body << part
|
222
|
-
@partial_part_left = len - part.size
|
223
|
-
when got == len - 1 # edge where we get just \r but not \n
|
224
|
-
@body << part[0..-2]
|
225
|
-
@partial_part_left = len - part.size
|
226
|
-
end
|
227
|
-
else
|
228
|
-
@prev_chunk = line
|
229
|
-
return false
|
230
|
-
end
|
231
|
-
end
|
232
|
-
|
233
|
-
if @in_last_chunk
|
234
|
-
set_ready
|
235
|
-
true
|
236
|
-
else
|
237
|
-
false
|
238
|
-
end
|
239
|
-
end
|
240
|
-
|
241
|
-
def read_chunked_body
|
242
|
-
while true
|
243
|
-
begin
|
244
|
-
chunk = @io.read_nonblock(4096)
|
245
|
-
rescue IO::WaitReadable
|
246
|
-
return false
|
247
|
-
rescue SystemCallError, IOError
|
248
|
-
raise ConnectionError, "Connection error detected during read"
|
249
|
-
end
|
250
|
-
|
251
|
-
# No chunk means a closed socket
|
252
|
-
unless chunk
|
253
|
-
@body.close
|
254
|
-
@buffer = nil
|
255
|
-
set_ready
|
256
|
-
raise EOFError
|
257
|
-
end
|
258
|
-
|
259
|
-
return true if decode_chunk(chunk)
|
260
|
-
end
|
261
|
-
end
|
262
|
-
|
263
|
-
def setup_body
|
264
|
-
@body_read_start = Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond)
|
265
|
-
|
266
|
-
if @env[HTTP_EXPECT] == CONTINUE
|
267
|
-
# TODO allow a hook here to check the headers before
|
268
|
-
# going forward
|
269
|
-
@io << HTTP_11_100
|
270
|
-
@io.flush
|
271
|
-
end
|
272
|
-
|
273
|
-
@read_header = false
|
274
|
-
|
275
|
-
body = @parser.body
|
276
|
-
|
277
|
-
te = @env[TRANSFER_ENCODING2]
|
278
|
-
|
279
|
-
if te && CHUNKED.casecmp(te) == 0
|
280
|
-
return setup_chunked_body(body)
|
281
|
-
end
|
282
|
-
|
283
|
-
@chunked_body = false
|
284
|
-
|
285
|
-
cl = @env[CONTENT_LENGTH]
|
286
|
-
|
287
|
-
unless cl
|
288
|
-
@buffer = body.empty? ? nil : body
|
289
|
-
@body = EmptyBody
|
290
|
-
set_ready
|
291
|
-
return true
|
292
|
-
end
|
293
|
-
|
294
|
-
remain = cl.to_i - body.bytesize
|
295
|
-
|
296
|
-
if remain <= 0
|
297
|
-
@body = StringIO.new(body)
|
298
|
-
@buffer = nil
|
299
|
-
set_ready
|
300
|
-
return true
|
301
|
-
end
|
302
|
-
|
303
|
-
if remain > MAX_BODY
|
304
|
-
@body = Tempfile.new(Const::PUMA_TMP_BASE)
|
305
|
-
@body.binmode
|
306
|
-
@tempfile = @body
|
307
|
-
else
|
308
|
-
# The body[0,0] trick is to get an empty string in the same
|
309
|
-
# encoding as body.
|
310
|
-
@body = StringIO.new body[0,0]
|
311
|
-
end
|
312
|
-
|
313
|
-
@body.write body
|
314
|
-
|
315
|
-
@body_remain = remain
|
316
|
-
|
317
|
-
return false
|
318
|
-
end
|
319
|
-
|
320
151
|
def try_to_finish
|
321
152
|
return read_body unless @read_header
|
322
153
|
|
@@ -417,6 +248,84 @@ module Puma
|
|
417
248
|
true
|
418
249
|
end
|
419
250
|
|
251
|
+
def write_error(status_code)
|
252
|
+
begin
|
253
|
+
@io << ERROR_RESPONSE[status_code]
|
254
|
+
rescue StandardError
|
255
|
+
end
|
256
|
+
end
|
257
|
+
|
258
|
+
def peerip
|
259
|
+
return @peerip if @peerip
|
260
|
+
|
261
|
+
if @remote_addr_header
|
262
|
+
hdr = (@env[@remote_addr_header] || LOCALHOST_ADDR).split(/[\s,]/).first
|
263
|
+
@peerip = hdr
|
264
|
+
return hdr
|
265
|
+
end
|
266
|
+
|
267
|
+
@peerip ||= @io.peeraddr.last
|
268
|
+
end
|
269
|
+
|
270
|
+
private
|
271
|
+
|
272
|
+
def setup_body
|
273
|
+
@body_read_start = Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond)
|
274
|
+
|
275
|
+
if @env[HTTP_EXPECT] == CONTINUE
|
276
|
+
# TODO allow a hook here to check the headers before
|
277
|
+
# going forward
|
278
|
+
@io << HTTP_11_100
|
279
|
+
@io.flush
|
280
|
+
end
|
281
|
+
|
282
|
+
@read_header = false
|
283
|
+
|
284
|
+
body = @parser.body
|
285
|
+
|
286
|
+
te = @env[TRANSFER_ENCODING2]
|
287
|
+
|
288
|
+
if te && CHUNKED.casecmp(te) == 0
|
289
|
+
return setup_chunked_body(body)
|
290
|
+
end
|
291
|
+
|
292
|
+
@chunked_body = false
|
293
|
+
|
294
|
+
cl = @env[CONTENT_LENGTH]
|
295
|
+
|
296
|
+
unless cl
|
297
|
+
@buffer = body.empty? ? nil : body
|
298
|
+
@body = EmptyBody
|
299
|
+
set_ready
|
300
|
+
return true
|
301
|
+
end
|
302
|
+
|
303
|
+
remain = cl.to_i - body.bytesize
|
304
|
+
|
305
|
+
if remain <= 0
|
306
|
+
@body = StringIO.new(body)
|
307
|
+
@buffer = nil
|
308
|
+
set_ready
|
309
|
+
return true
|
310
|
+
end
|
311
|
+
|
312
|
+
if remain > MAX_BODY
|
313
|
+
@body = Tempfile.new(Const::PUMA_TMP_BASE)
|
314
|
+
@body.binmode
|
315
|
+
@tempfile = @body
|
316
|
+
else
|
317
|
+
# The body[0,0] trick is to get an empty string in the same
|
318
|
+
# encoding as body.
|
319
|
+
@body = StringIO.new body[0,0]
|
320
|
+
end
|
321
|
+
|
322
|
+
@body.write body
|
323
|
+
|
324
|
+
@body_remain = remain
|
325
|
+
|
326
|
+
return false
|
327
|
+
end
|
328
|
+
|
420
329
|
def read_body
|
421
330
|
if @chunked_body
|
422
331
|
return read_chunked_body
|
@@ -462,45 +371,124 @@ module Puma
|
|
462
371
|
false
|
463
372
|
end
|
464
373
|
|
465
|
-
def
|
466
|
-
|
467
|
-
|
374
|
+
def read_chunked_body
|
375
|
+
while true
|
376
|
+
begin
|
377
|
+
chunk = @io.read_nonblock(4096)
|
378
|
+
rescue IO::WaitReadable
|
379
|
+
return false
|
380
|
+
rescue SystemCallError, IOError
|
381
|
+
raise ConnectionError, "Connection error detected during read"
|
382
|
+
end
|
383
|
+
|
384
|
+
# No chunk means a closed socket
|
385
|
+
unless chunk
|
386
|
+
@body.close
|
387
|
+
@buffer = nil
|
388
|
+
set_ready
|
389
|
+
raise EOFError
|
390
|
+
end
|
391
|
+
|
392
|
+
return true if decode_chunk(chunk)
|
468
393
|
end
|
469
|
-
@requests_served += 1
|
470
|
-
@ready = true
|
471
394
|
end
|
472
395
|
|
473
|
-
def
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
396
|
+
def setup_chunked_body(body)
|
397
|
+
@chunked_body = true
|
398
|
+
@partial_part_left = 0
|
399
|
+
@prev_chunk = ""
|
400
|
+
|
401
|
+
@body = Tempfile.new(Const::PUMA_TMP_BASE)
|
402
|
+
@body.binmode
|
403
|
+
@tempfile = @body
|
404
|
+
|
405
|
+
return decode_chunk(body)
|
478
406
|
end
|
479
407
|
|
480
|
-
def
|
481
|
-
|
482
|
-
@
|
483
|
-
|
408
|
+
def decode_chunk(chunk)
|
409
|
+
if @partial_part_left > 0
|
410
|
+
if @partial_part_left <= chunk.size
|
411
|
+
if @partial_part_left > 2
|
412
|
+
@body << chunk[0..(@partial_part_left-3)] # skip the \r\n
|
413
|
+
end
|
414
|
+
chunk = chunk[@partial_part_left..-1]
|
415
|
+
@partial_part_left = 0
|
416
|
+
else
|
417
|
+
@body << chunk if @partial_part_left > 2 # don't include the last \r\n
|
418
|
+
@partial_part_left -= chunk.size
|
419
|
+
return false
|
420
|
+
end
|
484
421
|
end
|
485
|
-
end
|
486
422
|
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
423
|
+
if @prev_chunk.empty?
|
424
|
+
io = StringIO.new(chunk)
|
425
|
+
else
|
426
|
+
io = StringIO.new(@prev_chunk+chunk)
|
427
|
+
@prev_chunk = ""
|
491
428
|
end
|
492
|
-
end
|
493
429
|
|
494
|
-
|
495
|
-
|
430
|
+
while !io.eof?
|
431
|
+
line = io.gets
|
432
|
+
if line.end_with?("\r\n")
|
433
|
+
len = line.strip.to_i(16)
|
434
|
+
if len == 0
|
435
|
+
@in_last_chunk = true
|
436
|
+
@body.rewind
|
437
|
+
rest = io.read
|
438
|
+
last_crlf_size = "\r\n".bytesize
|
439
|
+
if rest.bytesize < last_crlf_size
|
440
|
+
@buffer = nil
|
441
|
+
@partial_part_left = last_crlf_size - rest.bytesize
|
442
|
+
return false
|
443
|
+
else
|
444
|
+
@buffer = rest[last_crlf_size..-1]
|
445
|
+
@buffer = nil if @buffer.empty?
|
446
|
+
set_ready
|
447
|
+
return true
|
448
|
+
end
|
449
|
+
end
|
496
450
|
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
451
|
+
len += 2
|
452
|
+
|
453
|
+
part = io.read(len)
|
454
|
+
|
455
|
+
unless part
|
456
|
+
@partial_part_left = len
|
457
|
+
next
|
458
|
+
end
|
459
|
+
|
460
|
+
got = part.size
|
461
|
+
|
462
|
+
case
|
463
|
+
when got == len
|
464
|
+
@body << part[0..-3] # to skip the ending \r\n
|
465
|
+
when got <= len - 2
|
466
|
+
@body << part
|
467
|
+
@partial_part_left = len - part.size
|
468
|
+
when got == len - 1 # edge where we get just \r but not \n
|
469
|
+
@body << part[0..-2]
|
470
|
+
@partial_part_left = len - part.size
|
471
|
+
end
|
472
|
+
else
|
473
|
+
@prev_chunk = line
|
474
|
+
return false
|
475
|
+
end
|
501
476
|
end
|
502
477
|
|
503
|
-
|
478
|
+
if @in_last_chunk
|
479
|
+
set_ready
|
480
|
+
true
|
481
|
+
else
|
482
|
+
false
|
483
|
+
end
|
484
|
+
end
|
485
|
+
|
486
|
+
def set_ready
|
487
|
+
if @body_read_start
|
488
|
+
@env['puma.request_body_wait'] = Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond) - @body_read_start
|
489
|
+
end
|
490
|
+
@requests_served += 1
|
491
|
+
@ready = true
|
504
492
|
end
|
505
493
|
end
|
506
494
|
end
|