Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 95 lines (84 sloc) 2.8 kB
ed86dfd @chneukirchen Allow handlers to register themselves with Rack::Handler.
chneukirchen authored
1 module Rack
2 # *Handlers* connect web servers with Rack.
3 #
b487f02 @kuroda Use Thin instead of Mongrel
kuroda authored
4 # Rack includes Handlers for Thin, WEBrick, FastCGI, CGI, SCGI
ed86dfd @chneukirchen Allow handlers to register themselves with Rack::Handler.
chneukirchen authored
5 # and LiteSpeed.
6 #
7 # Handlers usually are activated by calling <tt>MyHandler.run(myapp)</tt>.
8 # A second optional hash can be passed to include server-specific
9 # configuration.
10 module Handler
11 def self.get(server)
12 return unless server
24d33c6 @manveru Refactor Handler::get and add Handler::try_require
manveru authored
13 server = server.to_s
1ce5400 @rkh If handler is not to be found, raise initial load error.
rkh authored
14
15 unless @handlers.include? server
16 load_error = try_require('rack/handler', server)
17 end
ed86dfd @chneukirchen Allow handlers to register themselves with Rack::Handler.
chneukirchen authored
18
19 if klass = @handlers[server]
33e080e @rkh If autoloading a rack handler by guessing the corresponding ruby file…
rkh authored
20 klass.split("::").inject(Object) { |o, x| o.const_get(x) }
ed86dfd @chneukirchen Allow handlers to register themselves with Rack::Handler.
chneukirchen authored
21 else
24d33c6 @manveru Refactor Handler::get and add Handler::try_require
manveru authored
22 const_get(server)
ed86dfd @chneukirchen Allow handlers to register themselves with Rack::Handler.
chneukirchen authored
23 end
1ce5400 @rkh If handler is not to be found, raise initial load error.
rkh authored
24
25 rescue NameError => name_error
26 raise load_error || name_error
ed86dfd @chneukirchen Allow handlers to register themselves with Rack::Handler.
chneukirchen authored
27 end
28
5db5d4e Refactor rackup into Rack::Server
Yehuda Katz + Carl Lerche authored
29 def self.default(options = {})
30 # Guess.
31 if ENV.include?("PHP_FCGI_CHILDREN")
32 # We already speak FastCGI
33 options.delete :File
34 options.delete :Port
35
36 Rack::Handler::FastCGI
37 elsif ENV.include?("REQUEST_METHOD")
38 Rack::Handler::CGI
39 else
40 begin
b487f02 @kuroda Use Thin instead of Mongrel
kuroda authored
41 Rack::Handler::Thin
cbcae3a @tenderlove fixing unused variable warnings in 1.9.3
tenderlove authored
42 rescue LoadError
5db5d4e Refactor rackup into Rack::Server
Yehuda Katz + Carl Lerche authored
43 Rack::Handler::WEBrick
44 end
45 end
46 end
47
24d33c6 @manveru Refactor Handler::get and add Handler::try_require
manveru authored
48 # Transforms server-name constants to their canonical form as filenames,
49 # then tries to require them but silences the LoadError if not found
50 #
51 # Naming convention:
52 #
53 # Foo # => 'foo'
54 # FooBar # => 'foo_bar.rb'
55 # FooBAR # => 'foobar.rb'
56 # FOObar # => 'foobar.rb'
57 # FOOBAR # => 'foobar.rb'
58 # FooBarBaz # => 'foo_bar_baz.rb'
59 def self.try_require(prefix, const_name)
60 file = const_name.gsub(/^[A-Z]+/) { |pre| pre.downcase }.
61 gsub(/[A-Z]+[^A-Z]/, '_\&').downcase
62
63 require(::File.join(prefix, file))
1ce5400 @rkh If handler is not to be found, raise initial load error.
rkh authored
64 nil
65 rescue LoadError => error
66 error
24d33c6 @manveru Refactor Handler::get and add Handler::try_require
manveru authored
67 end
68
ed86dfd @chneukirchen Allow handlers to register themselves with Rack::Handler.
chneukirchen authored
69 def self.register(server, klass)
70 @handlers ||= {}
33e080e @rkh If autoloading a rack handler by guessing the corresponding ruby file…
rkh authored
71 @handlers[server.to_s] = klass.to_s
ed86dfd @chneukirchen Allow handlers to register themselves with Rack::Handler.
chneukirchen authored
72 end
73
74 autoload :CGI, "rack/handler/cgi"
75 autoload :FastCGI, "rack/handler/fastcgi"
76 autoload :Mongrel, "rack/handler/mongrel"
77 autoload :EventedMongrel, "rack/handler/evented_mongrel"
0ae1246 @chneukirchen Update emongrel and add swiftiplied mongrel
chneukirchen authored
78 autoload :SwiftipliedMongrel, "rack/handler/swiftiplied_mongrel"
ed86dfd @chneukirchen Allow handlers to register themselves with Rack::Handler.
chneukirchen authored
79 autoload :WEBrick, "rack/handler/webrick"
80 autoload :LSWS, "rack/handler/lsws"
81 autoload :SCGI, "rack/handler/scgi"
539cf9a @macournoyer Add autoload for Thin handler
macournoyer authored
82 autoload :Thin, "rack/handler/thin"
ed86dfd @chneukirchen Allow handlers to register themselves with Rack::Handler.
chneukirchen authored
83
84 register 'cgi', 'Rack::Handler::CGI'
85 register 'fastcgi', 'Rack::Handler::FastCGI'
86 register 'mongrel', 'Rack::Handler::Mongrel'
87 register 'emongrel', 'Rack::Handler::EventedMongrel'
0ae1246 @chneukirchen Update emongrel and add swiftiplied mongrel
chneukirchen authored
88 register 'smongrel', 'Rack::Handler::SwiftipliedMongrel'
ed86dfd @chneukirchen Allow handlers to register themselves with Rack::Handler.
chneukirchen authored
89 register 'webrick', 'Rack::Handler::WEBrick'
90 register 'lsws', 'Rack::Handler::LSWS'
91 register 'scgi', 'Rack::Handler::SCGI'
539cf9a @macournoyer Add autoload for Thin handler
macournoyer authored
92 register 'thin', 'Rack::Handler::Thin'
ed86dfd @chneukirchen Allow handlers to register themselves with Rack::Handler.
chneukirchen authored
93 end
94 end
Something went wrong with that request. Please try again.