doc: remove references to old servers
[unicorn.git] / lib / unicorn.rb
blobb0e6bd15e58c3e3e9e26e4154bdea6d014abd5a4
1 # -*- encoding: binary -*-
2 require 'etc'
3 require 'stringio'
4 require 'rack'
5 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:
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.
17 module Unicorn
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)
26   # :stopdoc:
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
41     lambda do ||
42       inner_app = case ru
43       when /\.ru$/
44         raw = File.read(ru)
45         raw.sub!(/^__END__\n.*/, '')
46         eval("Rack::Builder.new {(\n#{raw}\n)}.to_app", TOPLEVEL_BINDING, ru)
47       else
48         require ru
49         Object.const_get(File.basename(ru, '.rb').capitalize)
50       end
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.
60       case ENV["RACK_ENV"]
61       when "development"
62         Rack::Builder.new do
63           use Rack::ContentLength
64           use Rack::Chunked
65           use Rack::CommonLogger, $stderr
66           use Rack::ShowExceptions
67           use Rack::Lint
68           use Rack::TempfileReaper if Rack.const_defined?(:TempfileReaper)
69           run inner_app
70         end.to_app
71       when "deployment"
72         Rack::Builder.new do
73           use Rack::ContentLength
74           use Rack::Chunked
75           use Rack::CommonLogger, $stderr
76           use Rack::TempfileReaper if Rack.const_defined?(:TempfileReaper)
77           run inner_app
78         end.to_app
79       else
80         inner_app
81       end
82     end
83   end
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
92   end
94   def self.log_error(logger, prefix, exc)
95     message = exc.message
96     message = message.dump if /[[:cntrl:]]/ =~ message
97     logger.error "#{prefix}: #{message} (#{exc.class})"
98     exc.backtrace.each { |line| logger.error(line) }
99   end
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 }
104   end
105   # :startdoc:
107 # :enddoc:
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}"