handler.rb 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. module Rack
  2. # *Handlers* connect web servers with Rack.
  3. #
  4. # Rack includes Handlers for Thin, WEBrick, FastCGI, CGI, SCGI
  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
  13. server = server.to_s
  14. unless @handlers.include? server
  15. load_error = try_require('rack/handler', server)
  16. end
  17. if klass = @handlers[server]
  18. klass.split("::").inject(Object) { |o, x| o.const_get(x) }
  19. else
  20. const_get(server)
  21. end
  22. rescue NameError => name_error
  23. raise load_error || name_error
  24. end
  25. def self.default(options = {})
  26. # Guess.
  27. if ENV.include?("PHP_FCGI_CHILDREN")
  28. # We already speak FastCGI
  29. options.delete :File
  30. options.delete :Port
  31. Rack::Handler::FastCGI
  32. elsif ENV.include?("REQUEST_METHOD")
  33. Rack::Handler::CGI
  34. else
  35. begin
  36. Rack::Handler::Thin
  37. rescue LoadError
  38. Rack::Handler::WEBrick
  39. end
  40. end
  41. end
  42. # Transforms server-name constants to their canonical form as filenames,
  43. # then tries to require them but silences the LoadError if not found
  44. #
  45. # Naming convention:
  46. #
  47. # Foo # => 'foo'
  48. # FooBar # => 'foo_bar.rb'
  49. # FooBAR # => 'foobar.rb'
  50. # FOObar # => 'foobar.rb'
  51. # FOOBAR # => 'foobar.rb'
  52. # FooBarBaz # => 'foo_bar_baz.rb'
  53. def self.try_require(prefix, const_name)
  54. file = const_name.gsub(/^[A-Z]+/) { |pre| pre.downcase }.
  55. gsub(/[A-Z]+[^A-Z]/, '_\&').downcase
  56. require(::File.join(prefix, file))
  57. nil
  58. rescue LoadError => error
  59. error
  60. end
  61. def self.register(server, klass)
  62. @handlers ||= {}
  63. @handlers[server.to_s] = klass.to_s
  64. end
  65. autoload :CGI, "rack/handler/cgi"
  66. autoload :FastCGI, "rack/handler/fastcgi"
  67. autoload :Mongrel, "rack/handler/mongrel"
  68. autoload :EventedMongrel, "rack/handler/evented_mongrel"
  69. autoload :SwiftipliedMongrel, "rack/handler/swiftiplied_mongrel"
  70. autoload :WEBrick, "rack/handler/webrick"
  71. autoload :LSWS, "rack/handler/lsws"
  72. autoload :SCGI, "rack/handler/scgi"
  73. autoload :Thin, "rack/handler/thin"
  74. register 'cgi', 'Rack::Handler::CGI'
  75. register 'fastcgi', 'Rack::Handler::FastCGI'
  76. register 'mongrel', 'Rack::Handler::Mongrel'
  77. register 'emongrel', 'Rack::Handler::EventedMongrel'
  78. register 'smongrel', 'Rack::Handler::SwiftipliedMongrel'
  79. register 'webrick', 'Rack::Handler::WEBrick'
  80. register 'lsws', 'Rack::Handler::LSWS'
  81. register 'scgi', 'Rack::Handler::SCGI'
  82. register 'thin', 'Rack::Handler::Thin'
  83. end
  84. end