module Rack::Handler

def self.default

def self.default
  # Guess.
  if ENV.include?("PHP_FCGI_CHILDREN")
    Rack::Handler::FastCGI
  elsif ENV.include?(REQUEST_METHOD)
    Rack::Handler::CGI
  elsif ENV.include?("RACK_HANDLER")
    self.get(ENV["RACK_HANDLER"])
  else
    pick SERVER_NAMES
  end
end

def self.get(server)

def self.get(server)
  return unless server
  server = server.to_s
  unless @handlers.include? server
    load_error = try_require('rack/handler', server)
  end
  if klass = @handlers[server]
    const_get(klass)
  else
    const_get(server, false)
  end
rescue NameError => name_error
  raise load_error || name_error
end

def self.pick(server_names)

=> Rack::Handler::WEBrick
> pick ['thin', 'webrick']

Raises `LoadError` if no handler was found.
Select first available Rack handler given an `Array` of server names.
def self.pick(server_names)
  server_names = Array(server_names)
  server_names.each do |server_name|
    begin
      return get(server_name.to_s)
    rescue LoadError, NameError
    end
  end
  raise LoadError, "Couldn't find handler for: #{server_names.join(', ')}."
end

def self.register(server, klass)

def self.register(server, klass)
  @handlers ||= {}
  @handlers[server.to_s] = klass.to_s
end

def self.try_require(prefix, const_name)

FooBarBaz # => 'foo_bar_baz.rb'
FOOBAR # => 'foobar.rb'
FOObar # => 'foobar.rb'
FooBAR # => 'foobar.rb'
FooBar # => 'foo_bar.rb'
Foo # => 'foo'

Naming convention:

then tries to require them but silences the LoadError if not found
Transforms server-name constants to their canonical form as filenames,
def self.try_require(prefix, const_name)
  file = const_name.gsub(/^[A-Z]+/) { |pre| pre.downcase }.
    gsub(/[A-Z]+[^A-Z]/, '_\&').downcase
  require(::File.join(prefix, file))
  nil
rescue LoadError => error
  error
end