1 # -*- encoding: binary -*-
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.
13 # unicorn exposes very little of an user-visible API and most of its
14 # internals are subject to change. unicorn is designed to host Rack
15 # applications, so applications should be written against the Rack SPEC
16 # and not unicorn internals.
19 # Raised inside TeeInput when a client closes the socket inside the
20 # application dispatch. This is always raised with an empty backtrace
21 # since there is nothing in the application stack that is responsible
22 # for client shutdowns/disconnects. This exception is visible to Rack
23 # applications unless PrereadInput middleware is loaded.
24 ClientShutdown = Class.new(EOFError)
28 # This returns a lambda to pass in as the app, this does not "build" the
29 # app (which we defer based on the outcome of "preload_app" in the
30 # Unicorn config). The returned lambda will be called when it is
31 # time to build the app.
32 def self.builder(ru, op)
33 # allow Configurator to parse cli switches embedded in the ru file
34 op = Unicorn::Configurator::RACKUP.merge!(:file => ru, :optparse => op)
36 # Op is going to get cleared before the returned lambda is called, so
37 # save this value so that it's still there when we need it:
38 no_default_middleware = op[:no_default_middleware]
40 # always called after config file parsing, may be called after forking
45 raw.sub!(/^__END__\n.*/, '')
46 eval("Rack::Builder.new {(\n#{raw}\n)}.to_app", TOPLEVEL_BINDING, ru)
49 Object.const_get(File.basename(ru, '.rb').capitalize)
52 pp({ :inner_app => inner_app }) if $DEBUG
54 return inner_app if no_default_middleware
56 # return value, matches rackup defaults based on env
57 # Unicorn does not support persistent connections, but Rainbows!
58 # and Zbatery both do. Users accustomed to the Rack::Server default
59 # middlewares will need ContentLength/Chunked middlewares.
63 use Rack::ContentLength
65 use Rack::CommonLogger, $stderr
66 use Rack::ShowExceptions
68 use Rack::TempfileReaper if Rack.const_defined?(:TempfileReaper)
73 use Rack::ContentLength
75 use Rack::CommonLogger, $stderr
76 use Rack::TempfileReaper if Rack.const_defined?(:TempfileReaper)
85 # returns an array of strings representing TCP listen socket addresses
86 # and Unix domain socket paths. This is useful for use with
87 # Raindrops::Middleware under Linux: http://raindrops.bogomips.org/
88 def self.listener_names
89 Unicorn::HttpServer::LISTENERS.map do |io|
90 Unicorn::SocketHelper.sock_name(io)
91 end + Unicorn::HttpServer::NEW_LISTENERS
94 def self.log_error(logger, prefix, exc)
96 message = message.dump if /[[:cntrl:]]/ =~ message
97 logger.error "#{prefix}: #{message} (#{exc.class})"
98 exc.backtrace.each { |line| logger.error(line) }
101 # remove this when we only support Ruby >= 2.0
102 def self.pipe # :nodoc:
103 Kgio::Pipe.new.each { |io| io.close_on_exec = true }
109 %w(const socket_helper stream_input tee_input http_request configurator
110 tmpio util http_response worker http_server).each do |s|
111 require_relative "unicorn/#{s}"