Module: Rack::Handler

Defined in:
lib/rack/handler.rb,
lib/rack/handler/cgi.rb,
lib/rack/handler/lsws.rb,
lib/rack/handler/scgi.rb,
lib/rack/handler/thin.rb,
lib/rack/handler/webrick.rb,
lib/rack/handler/fastcgi.rb

Overview

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.

Defined Under Namespace

Classes: CGI, FastCGI, LSWS, SCGI, Thin, WEBrick

Class Method Summary collapse

Class Method Details

.defaultObject



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rack/handler.rb', line 51

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

.get(server) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rack/handler.rb', line 13

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

.pick(server_names) ⇒ Object

Select first available Rack handler given an `Array` of server names. Raises `LoadError` if no handler was found.

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

Raises:

  • (LoadError)


36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rack/handler.rb', line 36

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

.register(server, klass) ⇒ Object



85
86
87
88
# File 'lib/rack/handler.rb', line 85

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

.try_require(prefix, const_name) ⇒ Object

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'


75
76
77
78
79
80
81
82
83
# File 'lib/rack/handler.rb', line 75

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