unicorn-heroku-wait 4.8.0.1.g0ed2.dirty

Sign up to get free protection for your applications and to get access to all the features.
Files changed (168) hide show
  1. checksums.yaml +7 -0
  2. data/.CHANGELOG.old +25 -0
  3. data/.document +29 -0
  4. data/.gitignore +25 -0
  5. data/.mailmap +26 -0
  6. data/.manifest +166 -0
  7. data/.wrongdoc.yml +10 -0
  8. data/Application_Timeouts +77 -0
  9. data/CONTRIBUTORS +35 -0
  10. data/COPYING +674 -0
  11. data/ChangeLog +4861 -0
  12. data/DESIGN +97 -0
  13. data/Documentation/.gitignore +5 -0
  14. data/Documentation/GNUmakefile +30 -0
  15. data/Documentation/unicorn.1.txt +178 -0
  16. data/Documentation/unicorn_rails.1.txt +175 -0
  17. data/FAQ +53 -0
  18. data/GIT-VERSION-FILE +1 -0
  19. data/GIT-VERSION-GEN +39 -0
  20. data/GNUmakefile +267 -0
  21. data/HACKING +134 -0
  22. data/ISSUES +36 -0
  23. data/KNOWN_ISSUES +79 -0
  24. data/LATEST +28 -0
  25. data/LICENSE +67 -0
  26. data/Links +56 -0
  27. data/NEWS +2067 -0
  28. data/PHILOSOPHY +145 -0
  29. data/README +150 -0
  30. data/Rakefile +60 -0
  31. data/SIGNALS +123 -0
  32. data/Sandbox +103 -0
  33. data/TODO +5 -0
  34. data/TUNING +98 -0
  35. data/bin/unicorn +126 -0
  36. data/bin/unicorn_rails +209 -0
  37. data/examples/big_app_gc.rb +2 -0
  38. data/examples/echo.ru +27 -0
  39. data/examples/git.ru +13 -0
  40. data/examples/init.sh +74 -0
  41. data/examples/logger_mp_safe.rb +25 -0
  42. data/examples/logrotate.conf +29 -0
  43. data/examples/nginx.conf +156 -0
  44. data/examples/unicorn.conf.minimal.rb +13 -0
  45. data/examples/unicorn.conf.rb +102 -0
  46. data/ext/unicorn_http/CFLAGS +13 -0
  47. data/ext/unicorn_http/c_util.h +124 -0
  48. data/ext/unicorn_http/common_field_optimization.h +111 -0
  49. data/ext/unicorn_http/ext_help.h +82 -0
  50. data/ext/unicorn_http/extconf.rb +10 -0
  51. data/ext/unicorn_http/global_variables.h +97 -0
  52. data/ext/unicorn_http/httpdate.c +78 -0
  53. data/ext/unicorn_http/unicorn_http.c +4031 -0
  54. data/ext/unicorn_http/unicorn_http.rl +1036 -0
  55. data/ext/unicorn_http/unicorn_http_common.rl +76 -0
  56. data/lib/unicorn/app/exec_cgi.rb +154 -0
  57. data/lib/unicorn/app/inetd.rb +109 -0
  58. data/lib/unicorn/app/old_rails/static.rb +59 -0
  59. data/lib/unicorn/app/old_rails.rb +35 -0
  60. data/lib/unicorn/cgi_wrapper.rb +147 -0
  61. data/lib/unicorn/configurator.rb +679 -0
  62. data/lib/unicorn/const.rb +44 -0
  63. data/lib/unicorn/http_request.rb +122 -0
  64. data/lib/unicorn/http_response.rb +75 -0
  65. data/lib/unicorn/http_server.rb +803 -0
  66. data/lib/unicorn/launcher.rb +62 -0
  67. data/lib/unicorn/oob_gc.rb +71 -0
  68. data/lib/unicorn/preread_input.rb +33 -0
  69. data/lib/unicorn/socket_helper.rb +231 -0
  70. data/lib/unicorn/ssl_client.rb +11 -0
  71. data/lib/unicorn/ssl_configurator.rb +104 -0
  72. data/lib/unicorn/ssl_server.rb +42 -0
  73. data/lib/unicorn/stream_input.rb +146 -0
  74. data/lib/unicorn/tee_input.rb +126 -0
  75. data/lib/unicorn/tmpio.rb +29 -0
  76. data/lib/unicorn/util.rb +89 -0
  77. data/lib/unicorn/version.rb +1 -0
  78. data/lib/unicorn/worker.rb +152 -0
  79. data/lib/unicorn.rb +118 -0
  80. data/local.mk.sample +59 -0
  81. data/man/man1/unicorn.1 +211 -0
  82. data/man/man1/unicorn_rails.1 +210 -0
  83. data/script/isolate_for_tests +32 -0
  84. data/setup.rb +1586 -0
  85. data/t/.gitignore +5 -0
  86. data/t/GNUmakefile +82 -0
  87. data/t/README +42 -0
  88. data/t/bin/content-md5-put +36 -0
  89. data/t/bin/sha1sum.rb +17 -0
  90. data/t/bin/unused_listen +40 -0
  91. data/t/broken-app.ru +12 -0
  92. data/t/detach.ru +11 -0
  93. data/t/env.ru +3 -0
  94. data/t/fails-rack-lint.ru +5 -0
  95. data/t/heartbeat-timeout.ru +12 -0
  96. data/t/hijack.ru +42 -0
  97. data/t/listener_names.ru +4 -0
  98. data/t/my-tap-lib.sh +201 -0
  99. data/t/oob_gc.ru +20 -0
  100. data/t/oob_gc_path.ru +20 -0
  101. data/t/pid.ru +3 -0
  102. data/t/preread_input.ru +17 -0
  103. data/t/rack-input-tests.ru +21 -0
  104. data/t/sslgen.sh +71 -0
  105. data/t/t0000-http-basic.sh +50 -0
  106. data/t/t0001-reload-bad-config.sh +53 -0
  107. data/t/t0002-config-conflict.sh +49 -0
  108. data/t/t0002-parser-error.sh +94 -0
  109. data/t/t0003-working_directory.sh +51 -0
  110. data/t/t0004-heartbeat-timeout.sh +69 -0
  111. data/t/t0004-working_directory_broken.sh +24 -0
  112. data/t/t0005-working_directory_app.rb.sh +40 -0
  113. data/t/t0006-reopen-logs.sh +83 -0
  114. data/t/t0006.ru +13 -0
  115. data/t/t0007-working_directory_no_embed_cli.sh +44 -0
  116. data/t/t0008-back_out_of_upgrade.sh +110 -0
  117. data/t/t0009-broken-app.sh +56 -0
  118. data/t/t0009-winch_ttin.sh +59 -0
  119. data/t/t0010-reap-logging.sh +55 -0
  120. data/t/t0011-active-unix-socket.sh +79 -0
  121. data/t/t0012-reload-empty-config.sh +85 -0
  122. data/t/t0013-rewindable-input-false.sh +24 -0
  123. data/t/t0013.ru +12 -0
  124. data/t/t0014-rewindable-input-true.sh +24 -0
  125. data/t/t0014.ru +12 -0
  126. data/t/t0015-configurator-internals.sh +25 -0
  127. data/t/t0016-trust-x-forwarded-false.sh +30 -0
  128. data/t/t0017-trust-x-forwarded-true.sh +30 -0
  129. data/t/t0018-write-on-close.sh +23 -0
  130. data/t/t0019-max_header_len.sh +49 -0
  131. data/t/t0020-at_exit-handler.sh +49 -0
  132. data/t/t0021-process_detach.sh +29 -0
  133. data/t/t0022-listener_names-preload_app.sh +32 -0
  134. data/t/t0100-rack-input-tests.sh +124 -0
  135. data/t/t0116-client_body_buffer_size.sh +80 -0
  136. data/t/t0116.ru +16 -0
  137. data/t/t0200-rack-hijack.sh +27 -0
  138. data/t/t0300-no-default-middleware.sh +20 -0
  139. data/t/t0600-https-server-basic.sh +48 -0
  140. data/t/t9000-preread-input.sh +48 -0
  141. data/t/t9001-oob_gc.sh +47 -0
  142. data/t/t9002-oob_gc-path.sh +75 -0
  143. data/t/test-lib.sh +128 -0
  144. data/t/write-on-close.ru +11 -0
  145. data/test/aggregate.rb +15 -0
  146. data/test/benchmark/README +50 -0
  147. data/test/benchmark/dd.ru +18 -0
  148. data/test/benchmark/stack.ru +8 -0
  149. data/test/exec/README +5 -0
  150. data/test/exec/test_exec.rb +1047 -0
  151. data/test/test_helper.rb +297 -0
  152. data/test/unit/test_configurator.rb +175 -0
  153. data/test/unit/test_droplet.rb +28 -0
  154. data/test/unit/test_http_parser.rb +854 -0
  155. data/test/unit/test_http_parser_ng.rb +731 -0
  156. data/test/unit/test_http_parser_xftrust.rb +38 -0
  157. data/test/unit/test_request.rb +182 -0
  158. data/test/unit/test_response.rb +99 -0
  159. data/test/unit/test_server.rb +268 -0
  160. data/test/unit/test_signals.rb +188 -0
  161. data/test/unit/test_sni_hostnames.rb +47 -0
  162. data/test/unit/test_socket_helper.rb +197 -0
  163. data/test/unit/test_stream_input.rb +203 -0
  164. data/test/unit/test_tee_input.rb +294 -0
  165. data/test/unit/test_upload.rb +306 -0
  166. data/test/unit/test_util.rb +105 -0
  167. data/unicorn.gemspec +44 -0
  168. metadata +328 -0
@@ -0,0 +1,76 @@
1
+ %%{
2
+
3
+ machine unicorn_http_common;
4
+
5
+ #### HTTP PROTOCOL GRAMMAR
6
+ # line endings
7
+ CRLF = "\r\n";
8
+
9
+ # character types
10
+ CTL = (cntrl | 127);
11
+ safe = ("$" | "-" | "_" | ".");
12
+ extra = ("!" | "*" | "'" | "(" | ")" | ",");
13
+ reserved = (";" | "/" | "?" | ":" | "@" | "&" | "=" | "+");
14
+ sorta_safe = ("\"" | "<" | ">");
15
+ unsafe = (CTL | " " | "#" | "%" | sorta_safe);
16
+ national = any -- (alpha | digit | reserved | extra | safe | unsafe);
17
+ unreserved = (alpha | digit | safe | extra | national);
18
+ escape = ("%" xdigit xdigit);
19
+ uchar = (unreserved | escape | sorta_safe);
20
+ pchar = (uchar | ":" | "@" | "&" | "=" | "+");
21
+ tspecials = ("(" | ")" | "<" | ">" | "@" | "," | ";" | ":" | "\\" | "\"" | "/" | "[" | "]" | "?" | "=" | "{" | "}" | " " | "\t");
22
+ lws = (" " | "\t");
23
+ content = ((any -- CTL) | lws);
24
+
25
+ # elements
26
+ token = (ascii -- (CTL | tspecials));
27
+
28
+ # URI schemes and absolute paths
29
+ scheme = ( "http"i ("s"i)? ) $downcase_char >mark %scheme;
30
+ hostname = ((alnum | "-" | "." | "_")+ | ("[" (":" | xdigit)+ "]"));
31
+ host_with_port = (hostname (":" digit*)?) >mark %host;
32
+ userinfo = ((unreserved | escape | ";" | ":" | "&" | "=" | "+")+ "@")*;
33
+
34
+ path = ( pchar+ ( "/" pchar* )* ) ;
35
+ query = ( uchar | reserved )* %query_string ;
36
+ param = ( pchar | "/" )* ;
37
+ params = ( param ( ";" param )* ) ;
38
+ rel_path = (path? (";" params)? %request_path) ("?" %start_query query)?;
39
+ absolute_path = ( "/"+ rel_path );
40
+ path_uri = absolute_path > mark %request_uri;
41
+ Absolute_URI = (scheme "://" userinfo host_with_port path_uri);
42
+
43
+ Request_URI = ((absolute_path | "*") >mark %request_uri) | Absolute_URI;
44
+ Fragment = ( uchar | reserved )* >mark %fragment;
45
+ Method = (token){1,20} >mark %request_method;
46
+ GetOnly = "GET" >mark %request_method;
47
+
48
+ http_number = ( digit+ "." digit+ ) ;
49
+ HTTP_Version = ( "HTTP/" http_number ) >mark %http_version ;
50
+ Request_Line = ( Method " " Request_URI ("#" Fragment){0,1} " " HTTP_Version CRLF ) ;
51
+
52
+ field_name = ( token -- ":" )+ >start_field $snake_upcase_field %write_field;
53
+
54
+ field_value = content* >start_value %write_value;
55
+
56
+ value_cont = lws+ content* >start_value %write_cont_value;
57
+
58
+ message_header = ((field_name ":" lws* field_value)|value_cont) :> CRLF;
59
+ chunk_ext_val = token*;
60
+ chunk_ext_name = token*;
61
+ chunk_extension = ( ";" " "* chunk_ext_name ("=" chunk_ext_val)? )*;
62
+ last_chunk = "0"+ chunk_extension CRLF;
63
+ chunk_size = (xdigit* [1-9a-fA-F] xdigit*) $add_to_chunk_size;
64
+ chunk_end = CRLF;
65
+ chunk_body = any >skip_chunk_data;
66
+ chunk_begin = chunk_size chunk_extension CRLF;
67
+ chunk = chunk_begin chunk_body chunk_end;
68
+ ChunkedBody := chunk* last_chunk @end_chunked_body;
69
+ Trailers := (message_header)* CRLF @end_trailers;
70
+
71
+ FullRequest = Request_Line (message_header)* CRLF @header_done;
72
+ SimpleRequest = GetOnly " " Request_URI ("#"Fragment){0,1} CRLF @header_done;
73
+
74
+ main := FullRequest | SimpleRequest;
75
+
76
+ }%%
@@ -0,0 +1,154 @@
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
@@ -0,0 +1,109 @@
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
@@ -0,0 +1,59 @@
1
+ # -*- encoding: binary -*-
2
+ # :enddoc:
3
+ # This code is based on the original Rails handler in Mongrel
4
+ # Copyright (c) 2005 Zed A. Shaw
5
+ # Copyright (c) 2009 Eric Wong
6
+ # You can redistribute it and/or modify it under the same terms as Ruby 1.8 or
7
+ # the GPLv3
8
+
9
+ # Static file handler for Rails < 2.3. This handler is only provided
10
+ # as a convenience for developers. Performance-minded deployments should
11
+ # use nginx (or similar) for serving static files.
12
+ #
13
+ # This supports page caching directly and will try to resolve a
14
+ # request in the following order:
15
+ #
16
+ # * If the requested exact PATH_INFO exists as a file then serve it.
17
+ # * If it exists at PATH_INFO+rest_operator+".html" exists
18
+ # then serve that.
19
+ #
20
+ # This means that if you are using page caching it will actually work
21
+ # with Unicorn and you should see a decent speed boost (but not as
22
+ # fast as if you use a static server like nginx).
23
+ class Unicorn::App::OldRails::Static < Struct.new(:app, :root, :file_server)
24
+ FILE_METHODS = { 'GET' => true, 'HEAD' => true }
25
+
26
+ # avoid allocating new strings for hash lookups
27
+ REQUEST_METHOD = 'REQUEST_METHOD'
28
+ REQUEST_URI = 'REQUEST_URI'
29
+ PATH_INFO = 'PATH_INFO'
30
+
31
+ def initialize(app)
32
+ self.app = app
33
+ self.root = "#{::RAILS_ROOT}/public"
34
+ self.file_server = ::Rack::File.new(root)
35
+ end
36
+
37
+ def call(env)
38
+ # short circuit this ASAP if serving non-file methods
39
+ FILE_METHODS.include?(env[REQUEST_METHOD]) or return app.call(env)
40
+
41
+ # first try the path as-is
42
+ path_info = env[PATH_INFO].chomp("/")
43
+ if File.file?("#{root}/#{::Rack::Utils.unescape(path_info)}")
44
+ # File exists as-is so serve it up
45
+ env[PATH_INFO] = path_info
46
+ return file_server.call(env)
47
+ end
48
+
49
+ # then try the cached version:
50
+ path_info << ActionController::Base.page_cache_extension
51
+
52
+ if File.file?("#{root}/#{::Rack::Utils.unescape(path_info)}")
53
+ env[PATH_INFO] = path_info
54
+ return file_server.call(env)
55
+ end
56
+
57
+ app.call(env) # call OldRails
58
+ end
59
+ end if defined?(Unicorn::App::OldRails)
@@ -0,0 +1,35 @@
1
+ # -*- encoding: binary -*-
2
+
3
+ # :enddoc:
4
+ # This code is based on the original Rails handler in Mongrel
5
+ # Copyright (c) 2005 Zed A. Shaw
6
+ # Copyright (c) 2009 Eric Wong
7
+ # You can redistribute it and/or modify it under the same terms as Ruby 1.8 or
8
+ # the GPLv2+ (GPLv3+ preferred)
9
+ # Additional work donated by contributors. See CONTRIBUTORS for more info.
10
+ require 'unicorn/cgi_wrapper'
11
+ require 'dispatcher'
12
+
13
+ module Unicorn; module App; end; end
14
+
15
+ # Implements a handler that can run Rails.
16
+ class Unicorn::App::OldRails
17
+
18
+ autoload :Static, "unicorn/app/old_rails/static"
19
+
20
+ def call(env)
21
+ cgi = Unicorn::CGIWrapper.new(env)
22
+ begin
23
+ Dispatcher.dispatch(cgi,
24
+ ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS,
25
+ cgi.body)
26
+ rescue => e
27
+ err = env['rack.errors']
28
+ err.write("#{e} #{e.message}\n")
29
+ e.backtrace.each { |line| err.write("#{line}\n") }
30
+ end
31
+ cgi.out # finalize the response
32
+ cgi.rack_response
33
+ end
34
+
35
+ end
@@ -0,0 +1,147 @@
1
+ # -*- encoding: binary -*-
2
+
3
+ # :enddoc:
4
+ # This code is based on the original CGIWrapper from Mongrel
5
+ # Copyright (c) 2005 Zed A. Shaw
6
+ # Copyright (c) 2009 Eric Wong
7
+ # You can redistribute it and/or modify it under the same terms as Ruby 1.8 or
8
+ # the GPLv2+ (GPLv3+ preferred)
9
+ #
10
+ # Additional work donated by contributors. See CONTRIBUTORS for more info.
11
+
12
+ require 'cgi'
13
+
14
+ module Unicorn; end
15
+
16
+ # The beginning of a complete wrapper around Unicorn's internal HTTP
17
+ # processing system but maintaining the original Ruby CGI module. Use
18
+ # this only as a crutch to get existing CGI based systems working. It
19
+ # should handle everything, but please notify us if you see special
20
+ # warnings. This work is still very alpha so we need testers to help
21
+ # work out the various corner cases.
22
+ class Unicorn::CGIWrapper < ::CGI
23
+ undef_method :env_table
24
+ attr_reader :env_table
25
+ attr_reader :body
26
+
27
+ # these are stripped out of any keys passed to CGIWrapper.header function
28
+ NPH = 'nph'.freeze # Completely ignored, Unicorn outputs the date regardless
29
+ CONNECTION = 'connection'.freeze # Completely ignored. Why is CGI doing this?
30
+ CHARSET = 'charset'.freeze # this gets appended to Content-Type
31
+ COOKIE = 'cookie'.freeze # maps (Hash,Array,String) to "Set-Cookie" headers
32
+ STATUS = 'status'.freeze # stored as @status
33
+ Status = 'Status'.freeze # code + human-readable text, Rails sets this
34
+
35
+ # some of these are common strings, but this is the only module
36
+ # using them and the reason they're not in Unicorn::Const
37
+ SET_COOKIE = 'Set-Cookie'.freeze
38
+ CONTENT_TYPE = 'Content-Type'.freeze
39
+ CONTENT_LENGTH = 'Content-Length'.freeze # this is NOT Const::CONTENT_LENGTH
40
+ RACK_INPUT = 'rack.input'.freeze
41
+ RACK_ERRORS = 'rack.errors'.freeze
42
+
43
+ # this maps CGI header names to HTTP header names
44
+ HEADER_MAP = {
45
+ 'status' => Status,
46
+ 'type' => CONTENT_TYPE,
47
+ 'server' => 'Server'.freeze,
48
+ 'language' => 'Content-Language'.freeze,
49
+ 'expires' => 'Expires'.freeze,
50
+ 'length' => CONTENT_LENGTH,
51
+ }
52
+
53
+ # Takes an a Rackable environment, plus any additional CGI.new
54
+ # arguments These are used internally to create a wrapper around the
55
+ # real CGI while maintaining Rack/Unicorn's view of the world. This
56
+ # this will NOT deal well with large responses that take up a lot of
57
+ # memory, but neither does the CGI nor the original CGIWrapper from
58
+ # Mongrel...
59
+ def initialize(rack_env, *args)
60
+ @env_table = rack_env
61
+ @status = nil
62
+ @head = {}
63
+ @headv = Hash.new { |hash,key| hash[key] = [] }
64
+ @body = StringIO.new("")
65
+ super(*args)
66
+ end
67
+
68
+ # finalizes the response in a way Rack applications would expect
69
+ def rack_response
70
+ # @head[CONTENT_LENGTH] ||= @body.size
71
+ @headv[SET_COOKIE].concat(@output_cookies) if @output_cookies
72
+ @headv.each_pair do |key,value|
73
+ @head[key] ||= value.join("\n") unless value.empty?
74
+ end
75
+
76
+ # Capitalized "Status:", with human-readable status code (e.g. "200 OK")
77
+ @status ||= @head.delete(Status)
78
+
79
+ [ @status || 500, @head, [ @body.string ] ]
80
+ end
81
+
82
+ # The header is typically called to send back the header. In our case we
83
+ # collect it into a hash for later usage. This can be called multiple
84
+ # times to set different cookies.
85
+ def header(options = "text/html")
86
+ # if they pass in a string then just write the Content-Type
87
+ if String === options
88
+ @head[CONTENT_TYPE] ||= options
89
+ else
90
+ HEADER_MAP.each_pair do |from, to|
91
+ from = options.delete(from) or next
92
+ @head[to] = from.to_s
93
+ end
94
+
95
+ @head[CONTENT_TYPE] ||= "text/html"
96
+ if charset = options.delete(CHARSET)
97
+ @head[CONTENT_TYPE] << "; charset=#{charset}"
98
+ end
99
+
100
+ # lots of ways to set cookies
101
+ if cookie = options.delete(COOKIE)
102
+ set_cookies = @headv[SET_COOKIE]
103
+ case cookie
104
+ when Array
105
+ cookie.each { |c| set_cookies << c.to_s }
106
+ when Hash
107
+ cookie.each_value { |c| set_cookies << c.to_s }
108
+ else
109
+ set_cookies << cookie.to_s
110
+ end
111
+ end
112
+ @status ||= options.delete(STATUS) # all lower-case
113
+
114
+ # drop the keys we don't want anymore
115
+ options.delete(NPH)
116
+ options.delete(CONNECTION)
117
+
118
+ # finally, set the rest of the headers as-is, allowing duplicates
119
+ options.each_pair { |k,v| @headv[k] << v }
120
+ end
121
+
122
+ # doing this fakes out the cgi library to think the headers are empty
123
+ # we then do the real headers in the out function call later
124
+ ""
125
+ end
126
+
127
+ # The dumb thing is people can call header or this or both and in
128
+ # any order. So, we just reuse header and then finalize the
129
+ # HttpResponse the right way. This will have no effect if called
130
+ # the second time if the first "outputted" anything.
131
+ def out(options = "text/html")
132
+ header(options)
133
+ @body.size == 0 or return
134
+ @body << yield if block_given?
135
+ end
136
+
137
+ # Used to wrap the normal stdinput variable used inside CGI.
138
+ def stdinput
139
+ @env_table[RACK_INPUT]
140
+ end
141
+
142
+ # return a pointer to the StringIO body since it's STDOUT-like
143
+ def stdoutput
144
+ @body
145
+ end
146
+
147
+ end