unicorn 4.9.0 → 6.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (108) hide show
  1. checksums.yaml +5 -5
  2. data/.gitattributes +5 -0
  3. data/.manifest +14 -15
  4. data/.olddoc.yml +16 -6
  5. data/Application_Timeouts +7 -7
  6. data/CONTRIBUTORS +6 -2
  7. data/DESIGN +2 -4
  8. data/Documentation/.gitignore +1 -3
  9. data/Documentation/unicorn.1 +222 -0
  10. data/Documentation/unicorn_rails.1 +207 -0
  11. data/FAQ +17 -8
  12. data/GIT-VERSION-FILE +1 -1
  13. data/GIT-VERSION-GEN +1 -1
  14. data/GNUmakefile +121 -56
  15. data/HACKING +2 -10
  16. data/ISSUES +40 -43
  17. data/KNOWN_ISSUES +11 -11
  18. data/LATEST +16 -22
  19. data/LICENSE +2 -2
  20. data/Links +24 -25
  21. data/NEWS +771 -0
  22. data/PHILOSOPHY +0 -6
  23. data/README +46 -40
  24. data/SIGNALS +2 -2
  25. data/Sandbox +11 -10
  26. data/TODO +0 -2
  27. data/TUNING +30 -9
  28. data/archive/slrnpull.conf +1 -1
  29. data/bin/unicorn +4 -2
  30. data/bin/unicorn_rails +3 -3
  31. data/examples/big_app_gc.rb +1 -1
  32. data/examples/init.sh +36 -8
  33. data/examples/logrotate.conf +17 -2
  34. data/examples/nginx.conf +14 -14
  35. data/examples/unicorn.conf.minimal.rb +2 -2
  36. data/examples/unicorn.conf.rb +3 -6
  37. data/examples/unicorn.socket +11 -0
  38. data/examples/unicorn@.service +40 -0
  39. data/ext/unicorn_http/c_util.h +5 -13
  40. data/ext/unicorn_http/common_field_optimization.h +22 -5
  41. data/ext/unicorn_http/epollexclusive.h +124 -0
  42. data/ext/unicorn_http/ext_help.h +0 -44
  43. data/ext/unicorn_http/extconf.rb +32 -5
  44. data/ext/unicorn_http/global_variables.h +2 -2
  45. data/ext/unicorn_http/httpdate.c +3 -2
  46. data/ext/unicorn_http/unicorn_http.c +926 -638
  47. data/ext/unicorn_http/unicorn_http.rl +159 -170
  48. data/ext/unicorn_http/unicorn_http_common.rl +1 -1
  49. data/lib/unicorn/configurator.rb +110 -44
  50. data/lib/unicorn/const.rb +2 -25
  51. data/lib/unicorn/http_request.rb +110 -31
  52. data/lib/unicorn/http_response.rb +17 -31
  53. data/lib/unicorn/http_server.rb +255 -179
  54. data/lib/unicorn/launcher.rb +1 -1
  55. data/lib/unicorn/oob_gc.rb +6 -6
  56. data/lib/unicorn/select_waiter.rb +6 -0
  57. data/lib/unicorn/socket_helper.rb +58 -78
  58. data/lib/unicorn/stream_input.rb +8 -7
  59. data/lib/unicorn/tee_input.rb +8 -10
  60. data/lib/unicorn/tmpio.rb +8 -7
  61. data/lib/unicorn/util.rb +5 -4
  62. data/lib/unicorn/version.rb +1 -1
  63. data/lib/unicorn/worker.rb +36 -23
  64. data/lib/unicorn.rb +64 -46
  65. data/man/man1/unicorn.1 +123 -119
  66. data/man/man1/unicorn_rails.1 +106 -107
  67. data/t/GNUmakefile +3 -72
  68. data/t/README +4 -4
  69. data/t/t0011-active-unix-socket.sh +1 -1
  70. data/t/t0012-reload-empty-config.sh +2 -1
  71. data/t/t0301-no-default-middleware-ignored-in-config.sh +25 -0
  72. data/t/t0301.ru +13 -0
  73. data/t/test-lib.sh +4 -3
  74. data/test/benchmark/README +14 -4
  75. data/test/benchmark/ddstream.ru +50 -0
  76. data/test/benchmark/readinput.ru +40 -0
  77. data/test/benchmark/uconnect.perl +66 -0
  78. data/test/exec/test_exec.rb +73 -19
  79. data/test/test_helper.rb +40 -31
  80. data/test/unit/test_ccc.rb +91 -0
  81. data/test/unit/test_droplet.rb +1 -1
  82. data/test/unit/test_http_parser.rb +46 -16
  83. data/test/unit/test_http_parser_ng.rb +97 -114
  84. data/test/unit/test_request.rb +10 -10
  85. data/test/unit/test_response.rb +28 -16
  86. data/test/unit/test_server.rb +86 -12
  87. data/test/unit/test_signals.rb +8 -8
  88. data/test/unit/test_socket_helper.rb +14 -10
  89. data/test/unit/test_upload.rb +9 -14
  90. data/test/unit/test_util.rb +31 -5
  91. data/test/unit/test_waiter.rb +34 -0
  92. data/unicorn.gemspec +27 -19
  93. metadata +28 -45
  94. data/Documentation/GNUmakefile +0 -30
  95. data/Documentation/unicorn.1.txt +0 -185
  96. data/Documentation/unicorn_rails.1.txt +0 -175
  97. data/examples/git.ru +0 -13
  98. data/lib/unicorn/app/exec_cgi.rb +0 -154
  99. data/lib/unicorn/app/inetd.rb +0 -109
  100. data/lib/unicorn/ssl_client.rb +0 -11
  101. data/lib/unicorn/ssl_configurator.rb +0 -104
  102. data/lib/unicorn/ssl_server.rb +0 -42
  103. data/t/hijack.ru +0 -42
  104. data/t/t0016-trust-x-forwarded-false.sh +0 -30
  105. data/t/t0017-trust-x-forwarded-true.sh +0 -30
  106. data/t/t0200-rack-hijack.sh +0 -27
  107. data/test/unit/test_http_parser_xftrust.rb +0 -38
  108. data/test/unit/test_sni_hostnames.rb +0 -47
@@ -1,185 +0,0 @@
1
- % UNICORN(1) Unicorn User Manual
2
- % The Unicorn Community <unicorn-public@bogomips.org>
3
- % September 15, 2009
4
-
5
- # NAME
6
-
7
- unicorn - a rackup-like command to launch the Unicorn HTTP server
8
-
9
- # SYNOPSIS
10
-
11
- unicorn [-c CONFIG_FILE] [-E RACK_ENV] [-D] [RACKUP_FILE]
12
-
13
- # DESCRIPTION
14
-
15
- A rackup(1)-like command to launch Rack applications using Unicorn.
16
- It is expected to be started in your application root (APP_ROOT),
17
- but the "working_directory" directive may be used in the CONFIG_FILE.
18
-
19
- While unicorn takes a myriad of command-line options for
20
- compatibility with ruby(1) and rackup(1), it is recommended to stick
21
- to the few command-line options specified in the SYNOPSIS and use
22
- the CONFIG_FILE as much as possible.
23
-
24
- # RACKUP FILE
25
-
26
- This defaults to \"config.ru\" in APP_ROOT. It should be the same
27
- file used by rackup(1) and other Rack launchers, it uses the
28
- *Rack::Builder* DSL.
29
-
30
- Embedded command-line options are mostly parsed for compatibility
31
- with rackup(1) but strongly discouraged.
32
-
33
- # UNICORN OPTIONS
34
- -c, \--config-file CONFIG_FILE
35
- : Path to the Unicorn-specific config file. The config file is
36
- implemented as a Ruby DSL, so Ruby code may executed.
37
- See the RDoc/ri for the *Unicorn::Configurator* class for the full
38
- list of directives available from the DSL.
39
- Using an absolute path for for CONFIG_FILE is recommended as it
40
- makes multiple instances of Unicorn easily distinguishable when
41
- viewing ps(1) output.
42
-
43
- -D, \--daemonize
44
- : Run daemonized in the background. The process is detached from
45
- the controlling terminal and stdin is redirected to "/dev/null".
46
- Unlike many common UNIX daemons, we do not chdir to \"/\"
47
- upon daemonization to allow more control over the startup/upgrade
48
- process.
49
- Unless specified in the CONFIG_FILE, stderr and stdout will
50
- also be redirected to "/dev/null".
51
-
52
- -E, \--env RACK_ENV
53
- : Run under the given RACK_ENV. See the RACK ENVIRONMENT section
54
- for more details.
55
-
56
- -l, \--listen ADDRESS
57
- : Listens on a given ADDRESS. ADDRESS may be in the form of
58
- HOST:PORT or PATH, HOST:PORT is taken to mean a TCP socket
59
- and PATH is meant to be a path to a UNIX domain socket.
60
- Defaults to "0.0.0.0:8080" (all addresses on TCP port 8080)
61
- For production deployments, specifying the "listen" directive in
62
- CONFIG_FILE is recommended as it allows fine-tuning of socket
63
- options.
64
- -N, \--no-default-middleware
65
- : Disables loading middleware implied by RACK_ENV. This bypasses the
66
- configuration documented in the RACK ENVIRONMENT section, but still
67
- allows RACK_ENV to be used for application/framework-specific purposes.
68
-
69
- # RACKUP COMPATIBILITY OPTIONS
70
- -o, \--host HOST
71
- : Listen on a TCP socket belonging to HOST, default is
72
- "0.0.0.0" (all addresses).
73
- If specified multiple times on the command-line, only the
74
- last-specified value takes effect.
75
- This option only exists for compatibility with the rackup(1) command,
76
- use of "-l"/"\--listen" switch is recommended instead.
77
-
78
- -p, \--port PORT
79
- : Listen on the specified TCP PORT, default is 8080.
80
- If specified multiple times on the command-line, only the last-specified
81
- value takes effect.
82
- This option only exists for compatibility with the rackup(1) command,
83
- use of "-l"/"\--listen" switch is recommended instead.
84
-
85
- -s, \--server SERVER
86
- : No-op, this exists only for compatibility with rackup(1).
87
-
88
- # RUBY OPTIONS
89
- -e, \--eval LINE
90
- : Evaluate a LINE of Ruby code. This evaluation happens
91
- immediately as the command-line is being parsed.
92
-
93
- -d, \--debug
94
- : Turn on debug mode, the $DEBUG variable is set to true.
95
-
96
- -w, \--warn
97
- : Turn on verbose warnings, the $VERBOSE variable is set to true.
98
-
99
- -I, \--include PATH
100
- : specify $LOAD_PATH. PATH will be prepended to $LOAD_PATH.
101
- The \':\' character may be used to delimit multiple directories.
102
- This directive may be used more than once. Modifications to
103
- $LOAD_PATH take place immediately and in the order they were
104
- specified on the command-line.
105
-
106
- -r, \--require LIBRARY
107
- : require a specified LIBRARY before executing the application. The
108
- \"require\" statement will be executed immediately and in the order
109
- they were specified on the command-line.
110
-
111
- # SIGNALS
112
-
113
- The following UNIX signals may be sent to the master process:
114
-
115
- * HUP - reload config file, app, and gracefully restart all workers
116
- * INT/TERM - quick shutdown, kills all workers immediately
117
- * QUIT - graceful shutdown, waits for workers to finish their
118
- current request before finishing.
119
- * USR1 - reopen all logs owned by the master and all workers
120
- See Unicorn::Util.reopen_logs for what is considered a log.
121
- * USR2 - reexecute the running binary. A separate QUIT
122
- should be sent to the original process once the child is verified to
123
- be up and running.
124
- * WINCH - gracefully stops workers but keep the master running.
125
- This will only work for daemonized processes.
126
- * TTIN - increment the number of worker processes by one
127
- * TTOU - decrement the number of worker processes by one
128
-
129
- See the [SIGNALS][4] document for full description of all signals
130
- used by Unicorn.
131
-
132
- # RACK ENVIRONMENT
133
-
134
- Accepted values of RACK_ENV and the middleware they automatically load
135
- (outside of RACKUP_FILE) are exactly as those in rackup(1):
136
-
137
- * development - loads Rack::CommonLogger, Rack::ShowExceptions, and
138
- Rack::Lint middleware
139
- * deployment - loads Rack::CommonLogger middleware
140
- * none - loads no middleware at all, relying
141
- entirely on RACKUP_FILE
142
-
143
- All unrecognized values for RACK_ENV are assumed to be
144
- "none". Production deployments are strongly encouraged to use
145
- "deployment" or "none" for maximum performance.
146
-
147
- As of Unicorn 0.94.0, RACK_ENV is exported as a process-wide environment
148
- variable as well. While not current a part of the Rack specification as
149
- of Rack 1.0.1, this has become a de facto standard in the Rack world.
150
-
151
- Note the Rack::ContentLength and Rack::Chunked middlewares are also
152
- loaded by "deployment" and "development", but no other values of
153
- RACK_ENV. If needed, they must be individually specified in the
154
- RACKUP_FILE, some frameworks do not require them.
155
-
156
- # ENVIRONMENT VARIABLES
157
-
158
- The RACK_ENV variable is set by the aforementioned \-E switch.
159
- All application or library-specific environment variables (e.g. TMPDIR)
160
- may always be set in the Unicorn CONFIG_FILE in addition to the spawning
161
- shell. When transparently upgrading Unicorn, all environment variables
162
- set in the old master process are inherited by the new master process.
163
- Unicorn only uses (and will overwrite) the UNICORN_FD environment
164
- variable internally when doing transparent upgrades.
165
-
166
- UNICORN_FD is a comma-delimited list of one or more file descriptors
167
- used to implement USR2 upgrades. Init systems may bind listen sockets
168
- itself and spawn unicorn with UNICORN_FD set to the file descriptor
169
- numbers of the listen socket(s). The unicorn CONFIG_FILE must still
170
- have the inherited listen socket parameters defined as in a normal
171
- startup, otherwise the socket will be closed.
172
-
173
- # SEE ALSO
174
-
175
- * unicorn_rails(1)
176
- * *Rack::Builder* ri/RDoc
177
- * *Unicorn::Configurator* ri/RDoc
178
- * [Unicorn RDoc][1]
179
- * [Rack RDoc][2]
180
- * [Rackup HowTo][3]
181
-
182
- [1]: http://unicorn.bogomips.org/
183
- [2]: http://rdoc.info/gems/r#/gems/rack/frames
184
- [3]: http://wiki.github.com/rack/rack/tutorial-rackup-howto
185
- [4]: http://unicorn.bogomips.org/SIGNALS.html
@@ -1,175 +0,0 @@
1
- % UNICORN_RAILS(1) Unicorn User Manual
2
- % The Unicorn Community <unicorn-public@bogomips.org>
3
- % September 17, 2009
4
-
5
- # NAME
6
-
7
- unicorn_rails - a script/server-like command to launch the Unicorn HTTP server
8
-
9
- # SYNOPSIS
10
-
11
- unicorn_rails [-c CONFIG_FILE] [-E RAILS_ENV] [-D] [RACKUP_FILE]
12
-
13
- # DESCRIPTION
14
-
15
- A rackup(1)-like command to launch Rails applications using Unicorn. It
16
- is expected to be started in your Rails application root (RAILS_ROOT),
17
- but the "working_directory" directive may be used in the CONFIG_FILE.
18
-
19
- It is designed to help Rails 1.x and 2.y users transition to Rack, but
20
- it is NOT needed for Rails 3 applications. Rails 3 users are encouraged
21
- to use unicorn(1) instead of unicorn_rails(1). Users of Rails 1.x/2.y
22
- may also use unicorn(1) instead of unicorn_rails(1).
23
-
24
- The outward interface resembles rackup(1), the internals and default
25
- middleware loading is designed like the `script/server` command
26
- distributed with Rails.
27
-
28
- While Unicorn takes a myriad of command-line options for compatibility
29
- with ruby(1) and rackup(1), it is recommended to stick to the few
30
- command-line options specified in the SYNOPSIS and use the CONFIG_FILE
31
- as much as possible.
32
-
33
- # UNICORN OPTIONS
34
- -c, \--config-file CONFIG_FILE
35
- : Path to the Unicorn-specific config file. The config file is
36
- implemented as a Ruby DSL, so Ruby code may executed.
37
- See the RDoc/ri for the *Unicorn::Configurator* class for the full
38
- list of directives available from the DSL.
39
- Using an absolute path for for CONFIG_FILE is recommended as it
40
- makes multiple instances of Unicorn easily distinguishable when
41
- viewing ps(1) output.
42
-
43
- -D, \--daemonize
44
- : Run daemonized in the background. The process is detached from
45
- the controlling terminal and stdin is redirected to "/dev/null".
46
- Unlike many common UNIX daemons, we do not chdir to \"/\"
47
- upon daemonization to allow more control over the startup/upgrade
48
- process.
49
- Unless specified in the CONFIG_FILE, stderr and stdout will
50
- also be redirected to "/dev/null".
51
- Daemonization will _skip_ loading of the *Rails::Rack::LogTailer*
52
- middleware under Rails \>\= 2.3.x.
53
- By default, unicorn\_rails(1) will create a PID file in
54
- _\"RAILS\_ROOT/tmp/pids/unicorn.pid\"_. You may override this
55
- by specifying the "pid" directive to override this Unicorn config file.
56
-
57
- -E, \--env RAILS_ENV
58
- : Run under the given RAILS_ENV. This sets the RAILS_ENV environment
59
- variable. Acceptable values are exactly those you expect in your Rails
60
- application, typically "development" or "production".
61
-
62
- -l, \--listen ADDRESS
63
- : Listens on a given ADDRESS. ADDRESS may be in the form of
64
- HOST:PORT or PATH, HOST:PORT is taken to mean a TCP socket
65
- and PATH is meant to be a path to a UNIX domain socket.
66
- Defaults to "0.0.0.0:8080" (all addresses on TCP port 8080).
67
- For production deployments, specifying the "listen" directive in
68
- CONFIG_FILE is recommended as it allows fine-tuning of socket
69
- options.
70
-
71
- # RACKUP COMPATIBILITY OPTIONS
72
- -o, \--host HOST
73
- : Listen on a TCP socket belonging to HOST, default is
74
- "0.0.0.0" (all addresses).
75
- If specified multiple times on the command-line, only the
76
- last-specified value takes effect.
77
- This option only exists for compatibility with the rackup(1) command,
78
- use of "-l"/"\--listen" switch is recommended instead.
79
-
80
- -p, \--port PORT
81
- : Listen on the specified TCP PORT, default is 8080.
82
- If specified multiple times on the command-line, only the last-specified
83
- value takes effect.
84
- This option only exists for compatibility with the rackup(1) command,
85
- use of "-l"/"\--listen" switch is recommended instead.
86
-
87
- \--path PATH
88
- : Mounts the Rails application at the given PATH (instead of "/").
89
- This is equivalent to setting the RAILS_RELATIVE_URL_ROOT
90
- environment variable. This is only supported under Rails 2.3
91
- or later at the moment.
92
-
93
- # RUBY OPTIONS
94
- -e, \--eval LINE
95
- : Evaluate a LINE of Ruby code. This evaluation happens
96
- immediately as the command-line is being parsed.
97
-
98
- -d, \--debug
99
- : Turn on debug mode, the $DEBUG variable is set to true.
100
- For Rails \>\= 2.3.x, this loads the *Rails::Rack::Debugger*
101
- middleware.
102
-
103
- -w, \--warn
104
- : Turn on verbose warnings, the $VERBOSE variable is set to true.
105
-
106
- -I, \--include PATH
107
- : specify $LOAD_PATH. PATH will be prepended to $LOAD_PATH.
108
- The \':\' character may be used to delimit multiple directories.
109
- This directive may be used more than once. Modifications to
110
- $LOAD_PATH take place immediately and in the order they were
111
- specified on the command-line.
112
-
113
- -r, \--require LIBRARY
114
- : require a specified LIBRARY before executing the application. The
115
- \"require\" statement will be executed immediately and in the order
116
- they were specified on the command-line.
117
-
118
- # RACKUP FILE
119
-
120
- This defaults to \"config.ru\" in RAILS_ROOT. It should be the same
121
- file used by rackup(1) and other Rack launchers, it uses the
122
- *Rack::Builder* DSL. Unlike many other Rack applications, RACKUP_FILE
123
- is completely _optional_ for Rails, but may be used to disable some
124
- of the default middleware for performance.
125
-
126
- Embedded command-line options are mostly parsed for compatibility
127
- with rackup(1) but strongly discouraged.
128
-
129
- # ENVIRONMENT VARIABLES
130
-
131
- The RAILS_ENV variable is set by the aforementioned \-E switch. The
132
- RAILS_RELATIVE_URL_ROOT is set by the aforementioned \--path switch.
133
- Either of these variables may also be set in the shell or the Unicorn
134
- CONFIG_FILE. All application or library-specific environment variables
135
- (e.g. TMPDIR, RAILS_ASSET_ID) may always be set in the Unicorn
136
- CONFIG_FILE in addition to the spawning shell. When transparently
137
- upgrading Unicorn, all environment variables set in the old master
138
- process are inherited by the new master process. Unicorn only uses (and
139
- will overwrite) the UNICORN_FD environment variable internally when
140
- doing transparent upgrades.
141
-
142
- # SIGNALS
143
-
144
- The following UNIX signals may be sent to the master process:
145
-
146
- * HUP - reload config file, app, and gracefully restart all workers
147
- * INT/TERM - quick shutdown, kills all workers immediately
148
- * QUIT - graceful shutdown, waits for workers to finish their
149
- current request before finishing.
150
- * USR1 - reopen all logs owned by the master and all workers
151
- See Unicorn::Util.reopen_logs for what is considered a log.
152
- * USR2 - reexecute the running binary. A separate QUIT
153
- should be sent to the original process once the child is verified to
154
- be up and running.
155
- * WINCH - gracefully stops workers but keep the master running.
156
- This will only work for daemonized processes.
157
- * TTIN - increment the number of worker processes by one
158
- * TTOU - decrement the number of worker processes by one
159
-
160
- See the [SIGNALS][4] document for full description of all signals
161
- used by Unicorn.
162
-
163
- # SEE ALSO
164
-
165
- * unicorn(1)
166
- * *Rack::Builder* ri/RDoc
167
- * *Unicorn::Configurator* ri/RDoc
168
- * [Unicorn RDoc][1]
169
- * [Rack RDoc][2]
170
- * [Rackup HowTo][3]
171
-
172
- [1]: http://unicorn.bogomips.org/
173
- [2]: http://rdoc.info/gems/r#/gems/rack/frames
174
- [3]: http://wiki.github.com/rack/rack/tutorial-rackup-howto
175
- [4]: http://unicorn.bogomips.org/SIGNALS.html
data/examples/git.ru DELETED
@@ -1,13 +0,0 @@
1
- #\-E none
2
-
3
- # See http://thread.gmane.org/gmane.comp.web.curl.general/10473/raw on
4
- # how to setup git for this. A better version of the above patch was
5
- # accepted and committed on June 15, 2009, so you can pull the latest
6
- # curl CVS snapshot to try this out.
7
- require 'unicorn/app/inetd'
8
-
9
- use Rack::Lint
10
- use Rack::Chunked # important!
11
- run Unicorn::App::Inetd.new(
12
- *%w(git daemon --verbose --inetd --export-all --base-path=/home/ew/unicorn)
13
- )
@@ -1,154 +0,0 @@
1
- # -*- encoding: binary -*-
2
- # :enddoc:
3
- require 'unicorn'
4
-
5
- module Unicorn::App
6
-
7
- # This class is highly experimental (even more so than the rest of Unicorn)
8
- # and has never run anything other than cgit.
9
- class ExecCgi < Struct.new(:args)
10
-
11
- CHUNK_SIZE = 16384
12
- PASS_VARS = %w(
13
- CONTENT_LENGTH
14
- CONTENT_TYPE
15
- GATEWAY_INTERFACE
16
- AUTH_TYPE
17
- PATH_INFO
18
- PATH_TRANSLATED
19
- QUERY_STRING
20
- REMOTE_ADDR
21
- REMOTE_HOST
22
- REMOTE_IDENT
23
- REMOTE_USER
24
- REQUEST_METHOD
25
- SERVER_NAME
26
- SERVER_PORT
27
- SERVER_PROTOCOL
28
- SERVER_SOFTWARE
29
- ).map { |x| x.freeze } # frozen strings are faster for Hash assignments
30
-
31
- class Body < Unicorn::TmpIO
32
- def body_offset=(n)
33
- sysseek(@body_offset = n)
34
- end
35
-
36
- def each
37
- sysseek @body_offset
38
- # don't use a preallocated buffer for sysread since we can't
39
- # guarantee an actual socket is consuming the yielded string
40
- # (or if somebody is pushing to an array for eventual concatenation
41
- begin
42
- yield sysread(CHUNK_SIZE)
43
- rescue EOFError
44
- break
45
- end while true
46
- end
47
- end
48
-
49
- # Intializes the app, example of usage in a config.ru
50
- # map "/cgit" do
51
- # run Unicorn::App::ExecCgi.new("/path/to/cgit.cgi")
52
- # end
53
- def initialize(*args)
54
- self.args = args
55
- first = args[0] or
56
- raise ArgumentError, "need path to executable"
57
- first[0] == ?/ or args[0] = ::File.expand_path(first)
58
- File.executable?(args[0]) or
59
- raise ArgumentError, "#{args[0]} is not executable"
60
- end
61
-
62
- # Calls the app
63
- def call(env)
64
- out, err = Body.new, Unicorn::TmpIO.new
65
- inp = force_file_input(env)
66
- pid = fork { run_child(inp, out, err, env) }
67
- inp.close
68
- pid, status = Process.waitpid2(pid)
69
- write_errors(env, err, status) if err.stat.size > 0
70
- err.close
71
-
72
- return parse_output!(out) if status.success?
73
- out.close
74
- [ 500, { 'Content-Length' => '0', 'Content-Type' => 'text/plain' }, [] ]
75
- end
76
-
77
- private
78
-
79
- def run_child(inp, out, err, env)
80
- PASS_VARS.each do |key|
81
- val = env[key] or next
82
- ENV[key] = val
83
- end
84
- ENV['SCRIPT_NAME'] = args[0]
85
- ENV['GATEWAY_INTERFACE'] = 'CGI/1.1'
86
- env.keys.grep(/^HTTP_/) { |key| ENV[key] = env[key] }
87
-
88
- $stdin.reopen(inp)
89
- $stdout.reopen(out)
90
- $stderr.reopen(err)
91
- exec(*args)
92
- end
93
-
94
- # Extracts headers from CGI out, will change the offset of out.
95
- # This returns a standard Rack-compatible return value:
96
- # [ 200, HeadersHash, body ]
97
- def parse_output!(out)
98
- size = out.stat.size
99
- out.sysseek(0)
100
- head = out.sysread(CHUNK_SIZE)
101
- offset = 2
102
- head, body = head.split(/\n\n/, 2)
103
- if body.nil?
104
- head, body = head.split(/\r\n\r\n/, 2)
105
- offset = 4
106
- end
107
- offset += head.length
108
- out.body_offset = offset
109
- size -= offset
110
- prev = nil
111
- headers = Rack::Utils::HeaderHash.new
112
- head.split(/\r?\n/).each do |line|
113
- case line
114
- when /^([A-Za-z0-9-]+):\s*(.*)$/ then headers[prev = $1] = $2
115
- when /^[ \t]/ then headers[prev] << "\n#{line}" if prev
116
- end
117
- end
118
- status = headers.delete("Status") || 200
119
- headers['Content-Length'] = size.to_s
120
- [ status, headers, out ]
121
- end
122
-
123
- # ensures rack.input is a file handle that we can redirect stdin to
124
- def force_file_input(env)
125
- inp = env['rack.input']
126
- # inp could be a StringIO or StringIO-like object
127
- if inp.respond_to?(:size) && inp.size == 0
128
- ::File.open('/dev/null', 'rb')
129
- else
130
- tmp = Unicorn::TmpIO.new
131
-
132
- buf = inp.read(CHUNK_SIZE)
133
- begin
134
- tmp.syswrite(buf)
135
- end while inp.read(CHUNK_SIZE, buf)
136
- tmp.sysseek(0)
137
- tmp
138
- end
139
- end
140
-
141
- # rack.errors this may not be an IO object, so we couldn't
142
- # just redirect the CGI executable to that earlier.
143
- def write_errors(env, err, status)
144
- err.seek(0)
145
- dst = env['rack.errors']
146
- pid = status.pid
147
- dst.write("#{pid}: #{args.inspect} status=#{status} stderr:\n")
148
- err.each_line { |line| dst.write("#{pid}: #{line}") }
149
- dst.flush
150
- end
151
-
152
- end
153
-
154
- end
@@ -1,109 +0,0 @@
1
- # -*- encoding: binary -*-
2
- # :enddoc:
3
- # Copyright (c) 2009 Eric Wong
4
- # You can redistribute it and/or modify it under the same terms as Ruby 1.8 or
5
- # the GPLv2+ (GPLv3+ preferred)
6
-
7
- # this class *must* be used with Rack::Chunked
8
- module Unicorn::App
9
- class Inetd < Struct.new(:cmd)
10
-
11
- class CatBody < Struct.new(:errors, :err_rd, :out_rd, :pid_map)
12
- def initialize(env, cmd)
13
- self.errors = env['rack.errors']
14
- in_rd, in_wr = IO.pipe
15
- self.err_rd, err_wr = IO.pipe
16
- self.out_rd, out_wr = IO.pipe
17
-
18
- cmd_pid = fork {
19
- inp, out, err = (0..2).map { |i| IO.new(i) }
20
- inp.reopen(in_rd)
21
- out.reopen(out_wr)
22
- err.reopen(err_wr)
23
- [ in_rd, in_wr, err_rd, err_wr, out_rd, out_wr ].each { |i| i.close }
24
- exec(*cmd)
25
- }
26
- [ in_rd, err_wr, out_wr ].each { |io| io.close }
27
- [ in_wr, err_rd, out_rd ].each { |io| io.binmode }
28
- in_wr.sync = true
29
-
30
- # Unfortunately, input here must be processed inside a seperate
31
- # thread/process using blocking I/O since env['rack.input'] is not
32
- # IO.select-able and attempting to make it so would trip Rack::Lint
33
- inp_pid = fork {
34
- input = env['rack.input']
35
- [ err_rd, out_rd ].each { |io| io.close }
36
-
37
- # this is dependent on input.read having readpartial semantics:
38
- buf = input.read(16384)
39
- begin
40
- in_wr.write(buf)
41
- end while input.read(16384, buf)
42
- }
43
- in_wr.close
44
- self.pid_map = {
45
- inp_pid => 'input streamer',
46
- cmd_pid => cmd.inspect,
47
- }
48
- end
49
-
50
- def each
51
- begin
52
- rd, = IO.select([err_rd, out_rd])
53
- rd && rd.first or next
54
-
55
- if rd.include?(err_rd)
56
- begin
57
- errors.write(err_rd.read_nonblock(16384))
58
- rescue Errno::EINTR
59
- rescue Errno::EAGAIN
60
- break
61
- end while true
62
- end
63
-
64
- rd.include?(out_rd) or next
65
-
66
- begin
67
- yield out_rd.read_nonblock(16384)
68
- rescue Errno::EINTR
69
- rescue Errno::EAGAIN
70
- break
71
- end while true
72
- rescue EOFError,Errno::EPIPE,Errno::EBADF,Errno::EINVAL
73
- break
74
- end while true
75
-
76
- self
77
- end
78
-
79
- def close
80
- pid_map.each { |pid, str|
81
- begin
82
- pid, status = Process.waitpid2(pid)
83
- status.success? or
84
- errors.write("#{str}: #{status.inspect} (PID:#{pid})\n")
85
- rescue Errno::ECHILD
86
- errors.write("Failed to reap #{str} (PID:#{pid})\n")
87
- end
88
- }
89
- out_rd.close
90
- err_rd.close
91
- end
92
-
93
- end
94
-
95
- def initialize(*cmd)
96
- self.cmd = cmd
97
- end
98
-
99
- def call(env)
100
- /\A100-continue\z/i =~ env[Unicorn::Const::HTTP_EXPECT] and
101
- return [ 100, {} , [] ]
102
-
103
- [ 200, { 'Content-Type' => 'application/octet-stream' },
104
- CatBody.new(env, cmd) ]
105
- end
106
-
107
- end
108
-
109
- end
@@ -1,11 +0,0 @@
1
- # -*- encoding: binary -*-
2
- # :stopdoc:
3
- class Unicorn::SSLClient < Kgio::SSL
4
- alias write kgio_write
5
- alias close kgio_close
6
-
7
- # this is no-op for now, to be fixed in kgio-monkey if people care
8
- # about SSL support...
9
- def shutdown(how = nil)
10
- end
11
- end