license: allow all future versions of the GNU GPL
[unicorn.git] / lib / unicorn.rb
blob25351597dc781d5e8e00f06654f823cbf55b547a
1 # -*- encoding: binary -*-
2 require 'fcntl'
3 require 'etc'
4 require 'stringio'
5 require 'rack'
6 require 'kgio'
8 # :stopdoc:
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.
12 # :startdoc:
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.
18 module Unicorn
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
26   end
28   # :stopdoc:
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     # Op is going to get cleared before the returned lambda is called, so
39     # save this value so that it's still there when we need it:
40     no_default_middleware = op[:no_default_middleware]
42     # always called after config file parsing, may be called after forking
43     lambda do ||
44       inner_app = case ru
45       when /\.ru$/
46         raw = File.read(ru)
47         raw.sub!(/^__END__\n.*/, '')
48         eval("Rack::Builder.new {(\n#{raw}\n)}.to_app", TOPLEVEL_BINDING, ru)
49       else
50         require ru
51         Object.const_get(File.basename(ru, '.rb').capitalize)
52       end
54       pp({ :inner_app => inner_app }) if $DEBUG
56       return inner_app if no_default_middleware
58       # return value, matches rackup defaults based on env
59       # Unicorn does not support persistent connections, but Rainbows!
60       # and Zbatery both do.  Users accustomed to the Rack::Server default
61       # middlewares will need ContentLength/Chunked middlewares.
62       case ENV["RACK_ENV"]
63       when "development"
64         Rack::Builder.new do
65           use Rack::ContentLength
66           use Rack::Chunked
67           use Rack::CommonLogger, $stderr
68           use Rack::ShowExceptions
69           use Rack::Lint
70           run inner_app
71         end.to_app
72       when "deployment"
73         Rack::Builder.new do
74           use Rack::ContentLength
75           use Rack::Chunked
76           use Rack::CommonLogger, $stderr
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
100   # :startdoc:
102 # :enddoc:
103 require 'unicorn/const'
104 require 'unicorn/socket_helper'
105 require 'unicorn/stream_input'
106 require 'unicorn/tee_input'
107 require 'unicorn/http_request'
108 require 'unicorn/configurator'
109 require 'unicorn/tmpio'
110 require 'unicorn/util'
111 require 'unicorn/http_response'
112 require 'unicorn/worker'
113 require 'unicorn/http_server'