Handlers connect web servers with Rack.
Rack includes Handlers for Mongrel, 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.
# File lib/rack/handler.rb, line 25 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::Mongrel rescue LoadError => e Rack::Handler::WEBrick end end end
# File lib/rack/handler.rb, line 11 def self.get(server) return unless server server = server.to_s if klass = @handlers[server] obj = Object klass.split("::").each { |x| obj = obj.const_get(x) } obj else try_require('rack/handler', server) const_get(server) end end
# File lib/rack/handler.rb, line 63 def self.register(server, klass) @handlers ||= {} @handlers[server] = klass end
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 55 def self.try_require(prefix, const_name) file = const_name.gsub(%r^[A-Z]+/) { |pre| pre.downcase }. gsub(%r[A-Z]+[^A-Z]/, '_\&').downcase require(::File.join(prefix, file)) rescue LoadError end