PageRenderTime 24ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/ruby/1.9.1/gems/unicorn-4.8.3/lib/unicorn.rb

https://github.com/evenwestvang/skoolgate
Ruby | 118 lines | 71 code | 13 blank | 34 comment | 3 complexity | db2fc6c62595e30aca87fa442f6b3965 MD5 | raw file
  1. # -*- encoding: binary -*-
  2. require 'fcntl'
  3. require 'etc'
  4. require 'stringio'
  5. require 'rack'
  6. require 'kgio'
  7. # :stopdoc:
  8. # Unicorn module containing all of the classes (include C extensions) for
  9. # running a Unicorn web server. It contains a minimalist HTTP server with just
  10. # enough functionality to service web application requests fast as possible.
  11. # :startdoc:
  12. # \Unicorn exposes very little of an user-visible API and most of its
  13. # internals are subject to change. \Unicorn is designed to host Rack
  14. # applications, so applications should be written against the Rack SPEC
  15. # and not \Unicorn internals.
  16. module Unicorn
  17. # Raised inside TeeInput when a client closes the socket inside the
  18. # application dispatch. This is always raised with an empty backtrace
  19. # since there is nothing in the application stack that is responsible
  20. # for client shutdowns/disconnects. This exception is visible to Rack
  21. # applications unless PrereadInput middleware is loaded.
  22. class ClientShutdown < EOFError
  23. end
  24. # :stopdoc:
  25. # This returns a lambda to pass in as the app, this does not "build" the
  26. # app (which we defer based on the outcome of "preload_app" in the
  27. # Unicorn config). The returned lambda will be called when it is
  28. # time to build the app.
  29. def self.builder(ru, op)
  30. # allow Configurator to parse cli switches embedded in the ru file
  31. op = Unicorn::Configurator::RACKUP.merge!(:file => ru, :optparse => op)
  32. # Op is going to get cleared before the returned lambda is called, so
  33. # save this value so that it's still there when we need it:
  34. no_default_middleware = op[:no_default_middleware]
  35. # always called after config file parsing, may be called after forking
  36. lambda do ||
  37. inner_app = case ru
  38. when /\.ru$/
  39. raw = File.read(ru)
  40. raw.sub!(/^__END__\n.*/, '')
  41. eval("Rack::Builder.new {(\n#{raw}\n)}.to_app", TOPLEVEL_BINDING, ru)
  42. else
  43. require ru
  44. Object.const_get(File.basename(ru, '.rb').capitalize)
  45. end
  46. pp({ :inner_app => inner_app }) if $DEBUG
  47. return inner_app if no_default_middleware
  48. # return value, matches rackup defaults based on env
  49. # Unicorn does not support persistent connections, but Rainbows!
  50. # and Zbatery both do. Users accustomed to the Rack::Server default
  51. # middlewares will need ContentLength/Chunked middlewares.
  52. case ENV["RACK_ENV"]
  53. when "development"
  54. Rack::Builder.new do
  55. use Rack::ContentLength
  56. use Rack::Chunked
  57. use Rack::CommonLogger, $stderr
  58. use Rack::ShowExceptions
  59. use Rack::Lint
  60. run inner_app
  61. end.to_app
  62. when "deployment"
  63. Rack::Builder.new do
  64. use Rack::ContentLength
  65. use Rack::Chunked
  66. use Rack::CommonLogger, $stderr
  67. run inner_app
  68. end.to_app
  69. else
  70. inner_app
  71. end
  72. end
  73. end
  74. # returns an array of strings representing TCP listen socket addresses
  75. # and Unix domain socket paths. This is useful for use with
  76. # Raindrops::Middleware under Linux: http://raindrops.bogomips.org/
  77. def self.listener_names
  78. Unicorn::HttpServer::LISTENERS.map do |io|
  79. Unicorn::SocketHelper.sock_name(io)
  80. end + Unicorn::HttpServer::NEW_LISTENERS
  81. end
  82. def self.log_error(logger, prefix, exc)
  83. message = exc.message
  84. message = message.dump if /[[:cntrl:]]/ =~ message
  85. logger.error "#{prefix}: #{message} (#{exc.class})"
  86. exc.backtrace.each { |line| logger.error(line) }
  87. end
  88. # remove this when we only support Ruby >= 2.0
  89. def self.pipe # :nodoc:
  90. Kgio::Pipe.new.each { |io| io.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC) }
  91. end
  92. # :startdoc:
  93. end
  94. # :enddoc:
  95. require 'unicorn/const'
  96. require 'unicorn/socket_helper'
  97. require 'unicorn/stream_input'
  98. require 'unicorn/tee_input'
  99. require 'unicorn/http_request'
  100. require 'unicorn/configurator'
  101. require 'unicorn/tmpio'
  102. require 'unicorn/util'
  103. require 'unicorn/http_response'
  104. require 'unicorn/worker'
  105. require 'unicorn/http_server'