1
0
Fork 0
mirror of https://github.com/puma/puma.git synced 2022-11-09 13:48:40 -05:00
puma--puma/lib/puma/app/status.rb
Jesús Burgos Maciá 2db1ef10f7 Fix: Invalid JSON on gc-stats (#1801)
* Fix: Invalid JSON on gc-stats

Credits of this patch should be for @jdsundberg, who reported the issue
but never sent a patch. Link to his bug report is below.

Closes #1687.

* Update tests so JSON response is correctly parsed

JSON without a whitespace between key and values wasn't being correctly
parsed (for example: `key: 'value'` was processed ok, but `key:'value'`
wasn't).
2019-05-21 07:43:18 -05:00

75 lines
1.9 KiB
Ruby

require 'json'
module Puma
module App
class Status
def initialize(cli)
@cli = cli
@auth_token = nil
end
OK_STATUS = '{ "status": "ok" }'.freeze
attr_accessor :auth_token
def authenticate(env)
return true unless @auth_token
env['QUERY_STRING'].to_s.split(/&;/).include?("token=#{@auth_token}")
end
def rack_response(status, body, content_type='application/json')
headers = {
'Content-Type' => content_type,
'Content-Length' => body.bytesize.to_s
}
[status, headers, [body]]
end
def call(env)
unless authenticate(env)
return rack_response(403, 'Invalid auth token', 'text/plain')
end
case env['PATH_INFO']
when /\/stop$/
@cli.stop
return rack_response(200, OK_STATUS)
when /\/halt$/
@cli.halt
return rack_response(200, OK_STATUS)
when /\/restart$/
@cli.restart
return rack_response(200, OK_STATUS)
when /\/phased-restart$/
if !@cli.phased_restart
return rack_response(404, '{ "error": "phased restart not available" }')
else
return rack_response(200, OK_STATUS)
end
when /\/reload-worker-directory$/
if !@cli.send(:reload_worker_directory)
return rack_response(404, '{ "error": "reload_worker_directory not available" }')
else
return rack_response(200, OK_STATUS)
end
when /\/gc$/
GC.start
return rack_response(200, OK_STATUS)
when /\/gc-stats$/
return rack_response(200, GC.stat.to_json)
when /\/stats$/
return rack_response(200, @cli.stats)
else
rack_response 404, "Unsupported action", 'text/plain'
end
end
end
end
end