123456789_123456789_123456789_123456789_123456789_

Class: Puma::Events

Relationships & Source Files
Namespace Children
Classes:
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
self, Const
Inherits: Object
Defined in: lib/puma/events.rb

Constant Summary

Const - Included

CGI_VER, CHUNKED, CHUNK_SIZE, CLOSE, CLOSE_CHUNKED, CODE_NAME, COLON, CONNECTION_CLOSE, CONNECTION_KEEP_ALIVE, CONTENT_LENGTH, CONTENT_LENGTH2, CONTENT_LENGTH_S, CONTINUE, EARLY_HINTS, ERROR_RESPONSE, FAST_TRACK_KA_TIMEOUT, FIRST_DATA_TIMEOUT, GATEWAY_INTERFACE, HALT_COMMAND, HEAD, HIJACK, HIJACK_IO, HIJACK_P, HTTP, HTTPS, HTTPS_KEY, HTTP_10_200, HTTP_11, HTTP_11_100, HTTP_11_200, HTTP_CONNECTION, HTTP_EXPECT, HTTP_HOST, HTTP_VERSION, HTTP_X_FORWARDED_FOR, HTTP_X_FORWARDED_PROTO, HTTP_X_FORWARDED_SCHEME, HTTP_X_FORWARDED_SSL, KEEP_ALIVE, LINE_END, LOCALHOST, LOCALHOST_ADDR, LOCALHOST_IP, MAX_BODY, MAX_HEADER, NEWLINE, PATH_INFO, PERSISTENT_TIMEOUT, PORT_443, PORT_80, PUMA_CONFIG, PUMA_PEERCERT, PUMA_SERVER_STRING, PUMA_SOCKET, PUMA_TMP_BASE, PUMA_VERSION, QUERY_STRING, RACK_AFTER_REPLY, RACK_INPUT, RACK_URL_SCHEME, REMOTE_ADDR, REQUEST_METHOD, REQUEST_PATH, REQUEST_URI, RESTART_COMMAND, SERVER_NAME, SERVER_PORT, SERVER_PROTOCOL, SERVER_SOFTWARE, STOP_COMMAND, TRANSFER_ENCODING, TRANSFER_ENCODING2, TRANSFER_ENCODING_CHUNKED, WORKER_CHECK_INTERVAL, WRITE_TIMEOUT

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(stdout, stderr) ⇒ Events

Create an Events object that prints to #stdout and #stderr.

[ GitHub ]

  
# File 'lib/puma/events.rb', line 30

def initialize(stdout, stderr)
  @formatter = DefaultFormatter.new
  @stdout = stdout
  @stderr = stderr

  @stdout.sync = true
  @stderr.sync = true

  @debug = ENV.key? 'PUMA_DEBUG'

  @hooks = Hash.new { |h,k| h[k] = [] }
end

Class Method Details

.null

[ GitHub ]

  
# File 'lib/puma/events.rb', line 151

def self.null
  n = NullIO.new
  Events.new n, n
end

.stdio

[ GitHub ]

  
# File 'lib/puma/events.rb', line 147

def self.stdio
  Events.new $stdout, $stderr
end

.strings

Returns an Events object which writes its status to 2 StringIO objects.

[ GitHub ]

  
# File 'lib/puma/events.rb', line 143

def self.strings
  Events.new StringIO.new, StringIO.new
end

Instance Attribute Details

#formatter (rw)

[ GitHub ]

  
# File 'lib/puma/events.rb', line 44

attr_accessor :formatter

#stderr (readonly)

[ GitHub ]

  
# File 'lib/puma/events.rb', line 43

attr_reader :stdout, :stderr

#stdout (readonly)

[ GitHub ]

  
# File 'lib/puma/events.rb', line 43

attr_reader :stdout, :stderr

Instance Method Details

#debug(str)

[ GitHub ]

  
# File 'lib/puma/events.rb', line 76

def debug(str)
  log("% #{str}") if @debug
end

#error(str)

Write str to @stderr

[ GitHub ]

  
# File 'lib/puma/events.rb', line 82

def error(str)
  @stderr.puts format("ERROR: #{str}")
  exit 1
end

#fire(hook, *args)

Fire callbacks for the named hook

[ GitHub ]

  
# File 'lib/puma/events.rb', line 48

def fire(hook, *args)
  @hooks[hook].each { |t| t.call(*args) }
end

#fire_on_booted!

[ GitHub ]

  
# File 'lib/puma/events.rb', line 134

def fire_on_booted!
  fire(:on_booted)
end

#format(str)

[ GitHub ]

  
# File 'lib/puma/events.rb', line 87

def format(str)
  formatter.call(str)
end

#log(str)

Write str to @stdout

[ GitHub ]

  
# File 'lib/puma/events.rb', line 68

def log(str)
  @stdout.puts format(str)
end

#on_booted(&block)

[ GitHub ]

  
# File 'lib/puma/events.rb', line 130

def on_booted(&block)
  register(:on_booted, &block)
end

#parse_error(server, env, error)

An HTTP parse error has occurred. server is the Server object, env the request, and #error a parsing exception.

[ GitHub ]

  
# File 'lib/puma/events.rb', line 95

def parse_error(server, env, error)
  @stderr.puts "#{Time.now}: HTTP parse error, malformed request " \
    "(#{env[HTTP_X_FORWARDED_FOR] || env[REMOTE_ADDR]}#{env[REQUEST_PATH]}): " \
    "#{error.inspect}" \
    "\n---\n"
end

#register(hook, obj = nil, &blk)

Register a callback for a given hook

[ GitHub ]

  
# File 'lib/puma/events.rb', line 54

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

#ssl_error(server, peeraddr, peercert, error)

An SSL error has occurred. server is the Server object, peeraddr peer address, peercert any peer certificate (if present), and #error an exception object.

[ GitHub ]

  
# File 'lib/puma/events.rb', line 106

def ssl_error(server, peeraddr, peercert, error)
  subject = peercert ? peercert.subject : nil
  @stderr.puts "#{Time.now}: SSL error, peer: #{peeraddr}, peer cert: #{subject}, #{error.inspect}"
end

#unknown_error(server, error, kind = "Unknown", env = nil)

An unknown error has occurred. server is the Server object, #error an exception object, kind some additional info, and env the request.

[ GitHub ]

  
# File 'lib/puma/events.rb', line 115

def unknown_error(server, error, kind="Unknown", env=nil)
  if error.respond_to? :render
    error.render "#{Time.now}: #{kind} error", @stderr
  else
    if env
      string_block = [ "#{Time.now}: #{kind} error handling request { #{env['REQUEST_METHOD']} #{env['PATH_INFO']} }" ]
      string_block << error.inspect
    else
      string_block = [ "#{Time.now}: #{kind} error: #{error.inspect}" ]
    end
    string_block << error.backtrace
    @stderr.puts string_block.join("\n")
  end
end

#write(str)

[ GitHub ]

  
# File 'lib/puma/events.rb', line 72

def write(str)
  @stdout.write format(str)
end