2018-09-17 11:41:14 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-11-24 01:20:47 +01:00
|
|
|
require "puma/null_io"
|
2020-05-11 16:56:54 +03:00
|
|
|
require 'puma/error_logger'
|
2011-09-30 10:29:37 -05:00
|
|
|
require 'stringio'
|
|
|
|
|
|
|
|
module Puma
|
2011-12-01 15:23:14 -08: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 10:29:37 -05:00
|
|
|
class Events
|
2015-01-15 14:56:48 +02:00
|
|
|
class DefaultFormatter
|
|
|
|
def call(str)
|
|
|
|
str
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class PidFormatter
|
|
|
|
def call(str)
|
|
|
|
"[#{$$}] #{str}"
|
|
|
|
end
|
|
|
|
end
|
2011-09-30 10:29:37 -05:00
|
|
|
|
2011-12-01 15:23:14 -08:00
|
|
|
# Create an Events object that prints to +stdout+ and +stderr+.
|
|
|
|
#
|
2011-09-30 10:29:37 -05:00
|
|
|
def initialize(stdout, stderr)
|
2015-01-15 14:56:48 +02:00
|
|
|
@formatter = DefaultFormatter.new
|
2019-09-02 16:05:48 +00:00
|
|
|
@stdout = stdout
|
|
|
|
@stderr = stderr
|
2012-09-02 23:33:09 -04:00
|
|
|
|
|
|
|
@stdout.sync = true
|
|
|
|
@stderr.sync = true
|
2013-07-05 16:54:15 -07:00
|
|
|
|
2013-07-15 14:29:10 -07:00
|
|
|
@debug = ENV.key? 'PUMA_DEBUG'
|
2020-05-11 16:56:54 +03:00
|
|
|
@error_logger = ErrorLogger.new(@stderr)
|
2013-07-15 14:29:10 -07:00
|
|
|
|
2013-09-13 09:56:39 -07:00
|
|
|
@hooks = Hash.new { |h,k| h[k] = [] }
|
2011-09-30 10:29:37 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :stdout, :stderr
|
2015-01-15 14:56:48 +02:00
|
|
|
attr_accessor :formatter
|
2011-09-30 10:29:37 -05:00
|
|
|
|
2013-09-13 09:56:39 -07:00
|
|
|
# Fire callbacks for the named hook
|
|
|
|
#
|
|
|
|
def fire(hook, *args)
|
|
|
|
@hooks[hook].each { |t| t.call(*args) }
|
|
|
|
end
|
|
|
|
|
2016-11-24 19:00:51 +01:00
|
|
|
# Register a callback for a given hook
|
2013-09-13 09:56:39 -07: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 19:09:35 -03:00
|
|
|
# Write +str+ to +@stdout+
|
|
|
|
#
|
|
|
|
def log(str)
|
2015-01-15 14:56:48 +02:00
|
|
|
@stdout.puts format(str)
|
2012-07-02 19:09:35 -03:00
|
|
|
end
|
|
|
|
|
2012-08-01 11:11:27 -06:00
|
|
|
def write(str)
|
2015-01-15 14:56:48 +02:00
|
|
|
@stdout.write format(str)
|
2012-08-01 11:11:27 -06:00
|
|
|
end
|
|
|
|
|
2013-07-15 14:29:10 -07:00
|
|
|
def debug(str)
|
|
|
|
log("% #{str}") if @debug
|
|
|
|
end
|
|
|
|
|
2012-07-02 19:09:35 -03:00
|
|
|
# Write +str+ to +@stderr+
|
|
|
|
#
|
|
|
|
def error(str)
|
2020-05-11 16:56:54 +03:00
|
|
|
@error_logger.info(text: format("ERROR: #{str}"))
|
2012-07-02 19:09:35 -03:00
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
|
2015-01-15 14:56:48 +02:00
|
|
|
def format(str)
|
|
|
|
formatter.call(str)
|
|
|
|
end
|
|
|
|
|
2020-05-09 19:03:32 +03:00
|
|
|
# An HTTP connection error has occurred.
|
2020-05-11 16:56:54 +03:00
|
|
|
# +error+ a connection exception, +req+ the request,
|
|
|
|
# and +text+ additional info
|
2020-05-09 19:03:32 +03:00
|
|
|
#
|
2020-05-11 13:19:53 +03:00
|
|
|
def connection_error(error, req, text="HTTP connection error")
|
2020-05-11 16:56:54 +03:00
|
|
|
@error_logger.info(error: error, req: req, text: text)
|
2020-05-09 19:03:32 +03:00
|
|
|
end
|
|
|
|
|
2015-07-15 09:59:34 -05:00
|
|
|
# An HTTP parse error has occurred.
|
2020-05-11 16:56:54 +03:00
|
|
|
# +error+ a parsing exception,
|
|
|
|
# and +req+ the request.
|
2011-12-01 15:23:14 -08:00
|
|
|
#
|
2020-05-11 13:19:53 +03:00
|
|
|
def parse_error(error, req)
|
2020-05-11 16:56:54 +03:00
|
|
|
@error_logger.info(error: error, req: req, text: 'HTTP parse error, malformed request')
|
2011-09-30 10:29:37 -05:00
|
|
|
end
|
|
|
|
|
2015-07-15 09:59:34 -05:00
|
|
|
# An SSL error has occurred.
|
2020-05-11 16:56:54 +03:00
|
|
|
# +error+ an exception object, +peeraddr+ peer address,
|
|
|
|
# and +peercert+ any peer certificate (if present).
|
2015-01-14 05:11:26 +01:00
|
|
|
#
|
2020-05-09 19:03:32 +03:00
|
|
|
def ssl_error(error, peeraddr, peercert)
|
2015-01-14 05:11:26 +01:00
|
|
|
subject = peercert ? peercert.subject : nil
|
2020-05-11 16:56:54 +03:00
|
|
|
@error_logger.info(error: error, text: "SSL error, peer: #{peeraddr}, peer cert: #{subject}")
|
2015-01-14 05:11:26 +01:00
|
|
|
end
|
|
|
|
|
2015-07-15 09:59:34 -05:00
|
|
|
# An unknown error has occurred.
|
2020-05-11 16:56:54 +03:00
|
|
|
# +error+ an exception object, +req+ the request,
|
|
|
|
# and +text+ additional info
|
2011-12-01 15:23:14 -08:00
|
|
|
#
|
2020-05-11 16:56:54 +03: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
|
|
|
|
#
|
|
|
|
def debug_error(error, req=nil, text="")
|
|
|
|
@error_logger.debug(error: error, req: req, text: text)
|
2011-09-30 10:29:37 -05:00
|
|
|
end
|
|
|
|
|
2016-11-24 19:00:51 +01:00
|
|
|
def on_booted(&block)
|
|
|
|
register(:on_booted, &block)
|
2013-07-05 16:54:15 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def fire_on_booted!
|
2016-11-24 19:00:51 +01:00
|
|
|
fire(:on_booted)
|
2013-07-05 16:54:15 -07:00
|
|
|
end
|
|
|
|
|
2011-09-30 10:29:37 -05:00
|
|
|
DEFAULT = new(STDOUT, STDERR)
|
|
|
|
|
2015-10-17 19:28:21 +01:00
|
|
|
# Returns an Events object which writes its status to 2 StringIO
|
2011-12-01 15:23:14 -08:00
|
|
|
# objects.
|
|
|
|
#
|
2011-09-30 10:29:37 -05:00
|
|
|
def self.strings
|
|
|
|
Events.new StringIO.new, StringIO.new
|
|
|
|
end
|
2013-07-05 16:54:15 -07:00
|
|
|
|
|
|
|
def self.stdio
|
|
|
|
Events.new $stdout, $stderr
|
|
|
|
end
|
2016-02-07 14:51:54 -08:00
|
|
|
|
|
|
|
def self.null
|
|
|
|
n = NullIO.new
|
|
|
|
Events.new n, n
|
|
|
|
end
|
2011-09-30 10:29:37 -05:00
|
|
|
end
|
|
|
|
end
|