module Rack::Handler

Handlers connect web servers with Rack.

Rack includes Handlers for Thin, WEBrick, FastCGI, CGI, SCGI and LiteSpeed.

Handlers usually are activated by calling MyHandler.run(myapp). A second optional hash can be passed to include server-specific configuration.

Public Class Methods

default(options = {}) click to toggle source
# File lib/rack/handler.rb, line 29
def self.default(options = {})
  # Guess.
  if ENV.include?("PHP_FCGI_CHILDREN")
    # We already speak FastCGI
    options.delete :File
    options.delete :Port

    Rack::Handler::FastCGI
  elsif ENV.include?("REQUEST_METHOD")
    Rack::Handler::CGI
  else
    begin
      Rack::Handler::Thin
    rescue LoadError
      Rack::Handler::WEBrick
    end
  end
end
get(server) click to toggle source
# File lib/rack/handler.rb, line 11
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]
    klass.split("::").inject(Object) { |o, x| o.const_get(x) }
  else
    const_get(server)
  end

rescue NameError => name_error
  raise load_error || name_error
end
register(server, klass) click to toggle source
# File lib/rack/handler.rb, line 69
def self.register(server, klass)
  @handlers ||= {}
  @handlers[server.to_s] = klass.to_s
end
try_require(prefix, const_name) click to toggle source

Transforms server-name constants to their canonical form as filenames, then tries to require them but silences the LoadError if not found

Naming convention:

Foo # => 'foo'
FooBar # => 'foo_bar.rb'
FooBAR # => 'foobar.rb'
FOObar # => 'foobar.rb'
FOOBAR # => 'foobar.rb'
FooBarBaz # => 'foo_bar_baz.rb'
# File lib/rack/handler.rb, line 59
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