1 # -*- encoding: binary -*-
9 # Unicorn module containing all of the classes (include C extensions) for
10 # running a Unicorn web server. It contains a minimalist HTTP server with just
11 # enough functionality to service web application requests fast as possible.
14 # \Unicorn exposes very little of an user-visible API and most of its
15 # internals are subject to change. \Unicorn is designed to host Rack
16 # applications, so applications should be written against the Rack SPEC
17 # and not \Unicorn internals.
20 # Raised inside TeeInput when a client closes the socket inside the
21 # application dispatch. This is always raised with an empty backtrace
22 # since there is nothing in the application stack that is responsible
23 # for client shutdowns/disconnects. This exception is visible to Rack
24 # applications unless PrereadInput middleware is loaded.
25 class ClientShutdown < EOFError
30 # This returns a lambda to pass in as the app, this does not "build" the
31 # app (which we defer based on the outcome of "preload_app" in the
32 # Unicorn config). The returned lambda will be called when it is
33 # time to build the app.
34 def self.builder(ru, op)
35 # allow Configurator to parse cli switches embedded in the ru file
36 op = Unicorn::Configurator::RACKUP.merge!(:file => ru, :optparse => op)
38 # always called after config file parsing, may be called after forking
43 raw.sub!(/^__END__\n.*/, '')
44 eval("Rack::Builder.new {(\n#{raw}\n)}.to_app", TOPLEVEL_BINDING, ru)
47 Object.const_get(File.basename(ru, '.rb').capitalize)
50 pp({ :inner_app => inner_app }) if $DEBUG
52 # return value, matches rackup defaults based on env
53 # Unicorn does not support persistent connections, but Rainbows!
54 # and Zbatery both do. Users accustomed to the Rack::Server default
55 # middlewares will need ContentLength/Chunked middlewares.
59 use Rack::ContentLength
61 use Rack::CommonLogger, $stderr
62 use Rack::ShowExceptions
68 use Rack::ContentLength
70 use Rack::CommonLogger, $stderr
79 # returns an array of strings representing TCP listen socket addresses
80 # and Unix domain socket paths. This is useful for use with
81 # Raindrops::Middleware under Linux: http://raindrops.bogomips.org/
82 def self.listener_names
83 Unicorn::HttpServer::LISTENERS.map do |io|
84 Unicorn::SocketHelper.sock_name(io)
85 end + Unicorn::HttpServer::NEW_LISTENERS
88 def self.log_error(logger, prefix, exc)
90 message = message.dump if /[[:cntrl:]]/ =~ message
91 logger.error "#{prefix}: #{message} (#{exc.class})"
92 exc.backtrace.each { |line| logger.error(line) }
97 require 'unicorn/const'
98 require 'unicorn/socket_helper'
99 require 'unicorn/stream_input'
100 require 'unicorn/tee_input'
101 require 'unicorn/http_request'
102 require 'unicorn/configurator'
103 require 'unicorn/tmpio'
104 require 'unicorn/util'
105 require 'unicorn/http_response'
106 require 'unicorn/worker'
107 require 'unicorn/http_server'