stream_input: read with zero length returns ''
[unicorn.git] / lib / unicorn.rb
blob7891d671c3dbaf32080014fa462c408b77c6edce
1 # -*- encoding: binary -*-
2 require 'fcntl'
3 require 'etc'
4 require 'stringio'
5 require 'rack'
6 require 'kgio'
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 module Unicorn
12   def self.run(app, options = {})
13     Unicorn::HttpServer.new(app, options).start.join
14   end
16   # This returns a lambda to pass in as the app, this does not "build" the
17   # app (which we defer based on the outcome of "preload_app" in the
18   # Unicorn config).  The returned lambda will be called when it is
19   # time to build the app.
20   def self.builder(ru, opts)
21     # allow Configurator to parse cli switches embedded in the ru file
22     Unicorn::Configurator::RACKUP.update(:file => ru, :optparse => opts)
24     # always called after config file parsing, may be called after forking
25     lambda do ||
26       inner_app = case ru
27       when /\.ru$/
28         raw = File.read(ru)
29         raw.sub!(/^__END__\n.*/, '')
30         eval("Rack::Builder.new {(#{raw}\n)}.to_app", TOPLEVEL_BINDING, ru)
31       else
32         require ru
33         Object.const_get(File.basename(ru, '.rb').capitalize)
34       end
36       pp({ :inner_app => inner_app }) if $DEBUG
38       # return value, matches rackup defaults based on env
39       case ENV["RACK_ENV"]
40       when "development"
41         Rack::Builder.new do
42           use Rack::CommonLogger, $stderr
43           use Rack::ShowExceptions
44           use Rack::Lint
45           run inner_app
46         end.to_app
47       when "deployment"
48         Rack::Builder.new do
49           use Rack::CommonLogger, $stderr
50           run inner_app
51         end.to_app
52       else
53         inner_app
54       end
55     end
56   end
58   # returns an array of strings representing TCP listen socket addresses
59   # and Unix domain socket paths.  This is useful for use with
60   # Raindrops::Middleware under Linux: http://raindrops.bogomips.org/
61   def self.listener_names
62     Unicorn::HttpServer::LISTENERS.map do |io|
63       Unicorn::SocketHelper.sock_name(io)
64     end
65   end
66 end
68 # raised inside TeeInput when a client closes the socket inside the
69 # application dispatch.  This is always raised with an empty backtrace
70 # since there is nothing in the application stack that is responsible
71 # for client shutdowns/disconnects.
72 class Unicorn::ClientShutdown < EOFError; end
74 require 'unicorn/const'
75 require 'unicorn/socket_helper'
76 require 'unicorn/stream_input'
77 require 'unicorn/tee_input'
78 require 'unicorn/http_request'
79 require 'unicorn/configurator'
80 require 'unicorn/tmpio'
81 require 'unicorn/util'
82 require 'unicorn/http_response'
83 require 'unicorn/worker'
84 require 'unicorn/http_server'