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
Chris LaRose 64c0153cd0
Remove use of json gem (#2479)
* Add basic JSON serializer

For now, it only handles Arrays of Integers, but we'll extend it to
support all of the common types

* Serialize Strings

* Escape quotes in Strings

* Escape backslashes in Strings

* Serialize Hashes with String keys

* Extract method for serializing Strings

* Add test coverage for non-Hash non-Array JSON serialization

* Add test for unexpected key types

* Serialize Hashes with Symbol keys

* Raise on unexpected value types

* Serialize boolean values

* Serialize Floats

* Add module comment to Puma::JSON

* Update integration test to use fully-qualfied JSON module reference

* Remove json gem dependency from /stats status server response

Fixes a bug where requesting `/stats` from the status server would cause
subsequent phased restarts to fail when upgrading/downgrading the json
gem.

* Run gc_stats tests on JRuby

These were disabled at some point on JRuby, but they seem to run fine.
Importantly, this test ensures that a call to `/gc-stats` returns
well-formed JSON on JRuby, where the value of `GC.stat` contains nested
structures.

* Remove json gem dependency from /gc-stats status server response

Fixes a bug where requesting `/gc-stats` from the status server would cause
subsequent phased restarts to fail when upgrading/downgrading the json
gem.

* Remove json gem from /thread-backtraces status server response

Fixes a bug where requesting `/thread-backtraces` from the status server
would cause subsequent phased restarts to fail when
upgrading/downgrading the json gem.

* Remove json gem dependency from Puma.stats

Fixes a bug where accessing `Puma.stats` would cause subsequent phased
restarts to fail when upgrading/downgrading the json gem.

* Fix test name in json test

Co-authored-by: rmacklin <1863540+rmacklin@users.noreply.github.com>

* Add History entry

* Add test for exceptions on values of unexpected types

* Update test name for additional clarity

* Reorder cases to match order in ECMA-404

* Allow all serializable inputs in Puma::JSON::serialize

The pervious implementation was based on and older JSON standard which
defined JSON texts to be either objects or arrays. Modern JSON standands
allow all JSON values to be valid JSON texts.

* Update JSON tests to test value types directly

* Reorder tests to roughly match source order

* Add test for serializing integers as JSON

* Serialize nil as null

* Use block form of gsub instead of hash form

* Escape control characters as required by ECMA-404

* Collapse handling of Symbol and String into one case

* Extract constants used in string serialization

* Remove superflous else case

* Use stringio for incremental JSON construction

* Extract test helper for testing JSON serialization

* Assert that strings generated by Puma::JSON roundtrip when using ::JSON

* Use a recent version of the json gem in tests

`::JSON.parse` doesn't handle JSON texts other than objects and arrays
in old versions

* Handle default expected_roundtrip more explicitly for clarity

Co-authored-by: rmacklin <1863540+rmacklin@users.noreply.github.com>
2020-11-10 10:16:42 -07:00

93 lines
2.5 KiB
Ruby

# frozen_string_literal: true
require 'puma/json'
module Puma
module App
# Check out {#call}'s source code to see what actions this web application
# can respond to.
class Status
OK_STATUS = '{ "status": "ok" }'.freeze
# @param launcher [::Puma::Launcher]
# @param token [String, nil] the token used for authentication
#
def initialize(launcher, token = nil)
@launcher = launcher
@auth_token = token
end
# most commands call methods in `::Puma::Launcher` based on command in
# `env['PATH_INFO']`
def call(env)
unless authenticate(env)
return rack_response(403, 'Invalid auth token', 'text/plain')
end
# resp_type is processed by following case statement, return
# is a number (status) or a string used as the body of a 200 response
resp_type =
case env['PATH_INFO'][/\/([^\/]+)$/, 1]
when 'stop'
@launcher.stop ; 200
when 'halt'
@launcher.halt ; 200
when 'restart'
@launcher.restart ; 200
when 'phased-restart'
@launcher.phased_restart ? 200 : 404
when 'reload-worker-directory'
@launcher.send(:reload_worker_directory) ? 200 : 404
when 'gc'
GC.start ; 200
when 'gc-stats'
Puma::JSON.generate GC.stat
when 'stats'
Puma::JSON.generate @launcher.stats
when 'thread-backtraces'
backtraces = []
@launcher.thread_status do |name, backtrace|
backtraces << { name: name, backtrace: backtrace }
end
Puma::JSON.generate backtraces
else
return rack_response(404, "Unsupported action", 'text/plain')
end
case resp_type
when String
rack_response 200, resp_type
when 200
rack_response 200, OK_STATUS
when 404
str = env['PATH_INFO'][/\/(\S+)/, 1].tr '-', '_'
rack_response 404, "{ \"error\": \"#{str} not available\" }"
end
end
private
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
end
end
end