2018-09-17 12:41:14 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-11-23 19:20:47 -05:00
|
|
|
require "puma/null_io"
|
2020-05-11 09:56:54 -04:00
|
|
|
require 'puma/error_logger'
|
2011-09-30 11:29:37 -04:00
|
|
|
require 'stringio'
|
|
|
|
|
|
|
|
module Puma
|
2011-12-01 18:23:14 -05:00
|
|
|
# The default implement of an event sink object used by Server
|
|
|
|
# for when certain kinds of events occur in the life of the server.
|
|
|
|
#
|
|
|
|
# The methods available are the events that the Server fires.
|
|
|
|
#
|
2011-09-30 11:29:37 -04:00
|
|
|
class Events
|
2015-01-15 07:56:48 -05:00
|
|
|
class DefaultFormatter
|
|
|
|
def call(str)
|
|
|
|
str
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class PidFormatter
|
|
|
|
def call(str)
|
|
|
|
"[#{$$}] #{str}"
|
|
|
|
end
|
|
|
|
end
|
2011-09-30 11:29:37 -04:00
|
|
|
|
2011-12-01 18:23:14 -05:00
|
|
|
# Create an Events object that prints to +stdout+ and +stderr+.
|
|
|
|
#
|
2011-09-30 11:29:37 -04:00
|
|
|
def initialize(stdout, stderr)
|
2015-01-15 07:56:48 -05:00
|
|
|
@formatter = DefaultFormatter.new
|
2019-09-02 12:05:48 -04:00
|
|
|
@stdout = stdout
|
|
|
|
@stderr = stderr
|
2012-09-02 23:33:09 -04:00
|
|
|
|
|
|
|
@stdout.sync = true
|
|
|
|
@stderr.sync = true
|
2013-07-05 19:54:15 -04:00
|
|
|
|
2013-07-15 17:29:10 -04:00
|
|
|
@debug = ENV.key? 'PUMA_DEBUG'
|
2020-05-11 09:56:54 -04:00
|
|
|
@error_logger = ErrorLogger.new(@stderr)
|
2013-07-15 17:29:10 -04:00
|
|
|
|
2013-09-13 12:56:39 -04:00
|
|
|
@hooks = Hash.new { |h,k| h[k] = [] }
|
2011-09-30 11:29:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :stdout, :stderr
|
2015-01-15 07:56:48 -05:00
|
|
|
attr_accessor :formatter
|
2011-09-30 11:29:37 -04:00
|
|
|
|
2013-09-13 12:56:39 -04:00
|
|
|
# Fire callbacks for the named hook
|
|
|
|
#
|
|
|
|
def fire(hook, *args)
|
|
|
|
@hooks[hook].each { |t| t.call(*args) }
|
|
|
|
end
|
|
|
|
|
2016-11-24 13:00:51 -05:00
|
|
|
# Register a callback for a given hook
|
2013-09-13 12:56:39 -04:00
|
|
|
#
|
|
|
|
def register(hook, obj=nil, &blk)
|
|
|
|
if obj and blk
|
|
|
|
raise "Specify either an object or a block, not both"
|
|
|
|
end
|
|
|
|
|
|
|
|
h = obj || blk
|
|
|
|
|
|
|
|
@hooks[hook] << h
|
|
|
|
|
|
|
|
h
|
|
|
|
end
|
|
|
|
|
2012-07-02 18:09:35 -04:00
|
|
|
# Write +str+ to +@stdout+
|
|
|
|
#
|
|
|
|
def log(str)
|
2020-07-19 18:06:10 -04:00
|
|
|
@stdout.puts format(str) if @stdout.respond_to? :puts
|
|
|
|
rescue Errno::EPIPE
|
2012-07-02 18:09:35 -04:00
|
|
|
end
|
|
|
|
|
2012-08-01 13:11:27 -04:00
|
|
|
def write(str)
|
2015-01-15 07:56:48 -05:00
|
|
|
@stdout.write format(str)
|
2012-08-01 13:11:27 -04:00
|
|
|
end
|
|
|
|
|
2013-07-15 17:29:10 -04:00
|
|
|
def debug(str)
|
|
|
|
log("% #{str}") if @debug
|
|
|
|
end
|
|
|
|
|
2012-07-02 18:09:35 -04:00
|
|
|
# Write +str+ to +@stderr+
|
|
|
|
#
|
|
|
|
def error(str)
|
2020-05-11 09:56:54 -04:00
|
|
|
@error_logger.info(text: format("ERROR: #{str}"))
|
2012-07-02 18:09:35 -04:00
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
|
2015-01-15 07:56:48 -05:00
|
|
|
def format(str)
|
|
|
|
formatter.call(str)
|
|
|
|
end
|
|
|
|
|
2020-05-09 12:03:32 -04:00
|
|
|
# An HTTP connection error has occurred.
|
2020-05-11 09:56:54 -04:00
|
|
|
# +error+ a connection exception, +req+ the request,
|
|
|
|
# and +text+ additional info
|
2020-09-17 11:15:19 -04:00
|
|
|
# @version 5.0.0
|
2020-05-09 12:03:32 -04:00
|
|
|
#
|
2020-05-11 06:19:53 -04:00
|
|
|
def connection_error(error, req, text="HTTP connection error")
|
2020-05-11 09:56:54 -04:00
|
|
|
@error_logger.info(error: error, req: req, text: text)
|
2020-05-09 12:03:32 -04:00
|
|
|
end
|
|
|
|
|
2015-07-15 10:59:34 -04:00
|
|
|
# An HTTP parse error has occurred.
|
2020-05-11 09:56:54 -04:00
|
|
|
# +error+ a parsing exception,
|
|
|
|
# and +req+ the request.
|
2011-12-01 18:23:14 -05:00
|
|
|
#
|
2020-05-11 06:19:53 -04:00
|
|
|
def parse_error(error, req)
|
2020-05-11 09:56:54 -04:00
|
|
|
@error_logger.info(error: error, req: req, text: 'HTTP parse error, malformed request')
|
2011-09-30 11:29:37 -04:00
|
|
|
end
|
|
|
|
|
2015-07-15 10:59:34 -04:00
|
|
|
# An SSL error has occurred.
|
2020-09-23 10:29:45 -04:00
|
|
|
# @param error <Puma::MiniSSL::SSLError>
|
|
|
|
# @param ssl_socket <Puma::MiniSSL::Socket>
|
2015-01-13 23:11:26 -05:00
|
|
|
#
|
2020-09-23 10:29:45 -04:00
|
|
|
def ssl_error(error, ssl_socket)
|
|
|
|
peeraddr = ssl_socket.peeraddr.last rescue "<unknown>"
|
|
|
|
peercert = ssl_socket.peercert
|
2015-01-13 23:11:26 -05:00
|
|
|
subject = peercert ? peercert.subject : nil
|
2020-05-11 09:56:54 -04:00
|
|
|
@error_logger.info(error: error, text: "SSL error, peer: #{peeraddr}, peer cert: #{subject}")
|
2015-01-13 23:11:26 -05:00
|
|
|
end
|
|
|
|
|
2015-07-15 10:59:34 -04:00
|
|
|
# An unknown error has occurred.
|
2020-05-11 09:56:54 -04:00
|
|
|
# +error+ an exception object, +req+ the request,
|
|
|
|
# and +text+ additional info
|
2011-12-01 18:23:14 -05:00
|
|
|
#
|
2020-05-11 09:56:54 -04:00
|
|
|
def unknown_error(error, req=nil, text="Unknown error")
|
|
|
|
@error_logger.info(error: error, req: req, text: text)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Log occurred error debug dump.
|
|
|
|
# +error+ an exception object, +req+ the request,
|
|
|
|
# and +text+ additional info
|
2020-09-17 11:15:19 -04:00
|
|
|
# @version 5.0.0
|
2020-05-11 09:56:54 -04:00
|
|
|
#
|
|
|
|
def debug_error(error, req=nil, text="")
|
|
|
|
@error_logger.debug(error: error, req: req, text: text)
|
2011-09-30 11:29:37 -04:00
|
|
|
end
|
|
|
|
|
2016-11-24 13:00:51 -05:00
|
|
|
def on_booted(&block)
|
|
|
|
register(:on_booted, &block)
|
2013-07-05 19:54:15 -04:00
|
|
|
end
|
|
|
|
|
2020-10-26 18:02:31 -04:00
|
|
|
def on_restart(&block)
|
|
|
|
register(:on_restart, &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_stopped(&block)
|
|
|
|
register(:on_stopped, &block)
|
|
|
|
end
|
|
|
|
|
2013-07-05 19:54:15 -04:00
|
|
|
def fire_on_booted!
|
2016-11-24 13:00:51 -05:00
|
|
|
fire(:on_booted)
|
2013-07-05 19:54:15 -04:00
|
|
|
end
|
|
|
|
|
2020-10-26 18:02:31 -04:00
|
|
|
def fire_on_restart!
|
|
|
|
fire(:on_restart)
|
|
|
|
end
|
|
|
|
|
|
|
|
def fire_on_stopped!
|
|
|
|
fire(:on_stopped)
|
|
|
|
end
|
|
|
|
|
2011-09-30 11:29:37 -04:00
|
|
|
DEFAULT = new(STDOUT, STDERR)
|
|
|
|
|
2015-10-17 14:28:21 -04:00
|
|
|
# Returns an Events object which writes its status to 2 StringIO
|
2011-12-01 18:23:14 -05:00
|
|
|
# objects.
|
|
|
|
#
|
2011-09-30 11:29:37 -04:00
|
|
|
def self.strings
|
|
|
|
Events.new StringIO.new, StringIO.new
|
|
|
|
end
|
2013-07-05 19:54:15 -04:00
|
|
|
|
|
|
|
def self.stdio
|
|
|
|
Events.new $stdout, $stderr
|
|
|
|
end
|
2016-02-07 17:51:54 -05:00
|
|
|
|
|
|
|
def self.null
|
|
|
|
n = NullIO.new
|
|
|
|
Events.new n, n
|
|
|
|
end
|
2011-09-30 11:29:37 -04:00
|
|
|
end
|
|
|
|
end
|