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,38 @@
1
+ # -*- encoding: binary -*-
2
+ require 'test/test_helper'
3
+
4
+ include Unicorn
5
+
6
+ class HttpParserXFTrustTest < Test::Unit::TestCase
7
+ def setup
8
+ assert HttpParser.trust_x_forwarded?
9
+ end
10
+
11
+ def test_xf_trust_false_xfp
12
+ HttpParser.trust_x_forwarded = false
13
+ parser = HttpParser.new
14
+ parser.buf << "GET / HTTP/1.1\r\nHost: foo:\r\n" \
15
+ "X-Forwarded-Proto: https\r\n\r\n"
16
+ env = parser.parse
17
+ assert_kind_of Hash, env
18
+ assert_equal 'foo', env['SERVER_NAME']
19
+ assert_equal '80', env['SERVER_PORT']
20
+ assert_equal 'http', env['rack.url_scheme']
21
+ end
22
+
23
+ def test_xf_trust_false_xfs
24
+ HttpParser.trust_x_forwarded = false
25
+ parser = HttpParser.new
26
+ parser.buf << "GET / HTTP/1.1\r\nHost: foo:\r\n" \
27
+ "X-Forwarded-SSL: on\r\n\r\n"
28
+ env = parser.parse
29
+ assert_kind_of Hash, env
30
+ assert_equal 'foo', env['SERVER_NAME']
31
+ assert_equal '80', env['SERVER_PORT']
32
+ assert_equal 'http', env['rack.url_scheme']
33
+ end
34
+
35
+ def teardown
36
+ HttpParser.trust_x_forwarded = true
37
+ end
38
+ end
@@ -0,0 +1,182 @@
1
+ # -*- encoding: binary -*-
2
+
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
+ require 'test/test_helper'
8
+
9
+ include Unicorn
10
+
11
+ class RequestTest < Test::Unit::TestCase
12
+
13
+ class MockRequest < StringIO
14
+ alias_method :readpartial, :sysread
15
+ alias_method :kgio_read!, :sysread
16
+ alias_method :read_nonblock, :sysread
17
+ def kgio_addr
18
+ '127.0.0.1'
19
+ end
20
+ end
21
+
22
+ def setup
23
+ @request = HttpRequest.new
24
+ @app = lambda do |env|
25
+ [ 200, { 'Content-Length' => '0', 'Content-Type' => 'text/plain' }, [] ]
26
+ end
27
+ @lint = Rack::Lint.new(@app)
28
+ end
29
+
30
+ def test_options
31
+ client = MockRequest.new("OPTIONS * HTTP/1.1\r\n" \
32
+ "Host: foo\r\n\r\n")
33
+ env = @request.read(client)
34
+ assert_equal '', env['REQUEST_PATH']
35
+ assert_equal '', env['PATH_INFO']
36
+ assert_equal '*', env['REQUEST_URI']
37
+ res = @lint.call(env)
38
+ end
39
+
40
+ def test_absolute_uri_with_query
41
+ client = MockRequest.new("GET http://e:3/x?y=z HTTP/1.1\r\n" \
42
+ "Host: foo\r\n\r\n")
43
+ env = @request.read(client)
44
+ assert_equal '/x', env['REQUEST_PATH']
45
+ assert_equal '/x', env['PATH_INFO']
46
+ assert_equal 'y=z', env['QUERY_STRING']
47
+ res = @lint.call(env)
48
+ end
49
+
50
+ def test_absolute_uri_with_fragment
51
+ client = MockRequest.new("GET http://e:3/x#frag HTTP/1.1\r\n" \
52
+ "Host: foo\r\n\r\n")
53
+ env = @request.read(client)
54
+ assert_equal '/x', env['REQUEST_PATH']
55
+ assert_equal '/x', env['PATH_INFO']
56
+ assert_equal '', env['QUERY_STRING']
57
+ assert_equal 'frag', env['FRAGMENT']
58
+ res = @lint.call(env)
59
+ end
60
+
61
+ def test_absolute_uri_with_query_and_fragment
62
+ client = MockRequest.new("GET http://e:3/x?a=b#frag HTTP/1.1\r\n" \
63
+ "Host: foo\r\n\r\n")
64
+ env = @request.read(client)
65
+ assert_equal '/x', env['REQUEST_PATH']
66
+ assert_equal '/x', env['PATH_INFO']
67
+ assert_equal 'a=b', env['QUERY_STRING']
68
+ assert_equal 'frag', env['FRAGMENT']
69
+ res = @lint.call(env)
70
+ end
71
+
72
+ def test_absolute_uri_unsupported_schemes
73
+ %w(ssh+http://e/ ftp://e/x http+ssh://e/x).each do |abs_uri|
74
+ client = MockRequest.new("GET #{abs_uri} HTTP/1.1\r\n" \
75
+ "Host: foo\r\n\r\n")
76
+ assert_raises(HttpParserError) { @request.read(client) }
77
+ end
78
+ end
79
+
80
+ def test_x_forwarded_proto_https
81
+ client = MockRequest.new("GET / HTTP/1.1\r\n" \
82
+ "X-Forwarded-Proto: https\r\n" \
83
+ "Host: foo\r\n\r\n")
84
+ env = @request.read(client)
85
+ assert_equal "https", env['rack.url_scheme']
86
+ res = @lint.call(env)
87
+ end
88
+
89
+ def test_x_forwarded_proto_http
90
+ client = MockRequest.new("GET / HTTP/1.1\r\n" \
91
+ "X-Forwarded-Proto: http\r\n" \
92
+ "Host: foo\r\n\r\n")
93
+ env = @request.read(client)
94
+ assert_equal "http", env['rack.url_scheme']
95
+ res = @lint.call(env)
96
+ end
97
+
98
+ def test_x_forwarded_proto_invalid
99
+ client = MockRequest.new("GET / HTTP/1.1\r\n" \
100
+ "X-Forwarded-Proto: ftp\r\n" \
101
+ "Host: foo\r\n\r\n")
102
+ env = @request.read(client)
103
+ assert_equal "http", env['rack.url_scheme']
104
+ res = @lint.call(env)
105
+ end
106
+
107
+ def test_rack_lint_get
108
+ client = MockRequest.new("GET / HTTP/1.1\r\nHost: foo\r\n\r\n")
109
+ env = @request.read(client)
110
+ assert_equal "http", env['rack.url_scheme']
111
+ assert_equal '127.0.0.1', env['REMOTE_ADDR']
112
+ res = @lint.call(env)
113
+ end
114
+
115
+ def test_no_content_stringio
116
+ client = MockRequest.new("GET / HTTP/1.1\r\nHost: foo\r\n\r\n")
117
+ env = @request.read(client)
118
+ assert_equal StringIO, env['rack.input'].class
119
+ end
120
+
121
+ def test_zero_content_stringio
122
+ client = MockRequest.new("PUT / HTTP/1.1\r\n" \
123
+ "Content-Length: 0\r\n" \
124
+ "Host: foo\r\n\r\n")
125
+ env = @request.read(client)
126
+ assert_equal StringIO, env['rack.input'].class
127
+ end
128
+
129
+ def test_real_content_not_stringio
130
+ client = MockRequest.new("PUT / HTTP/1.1\r\n" \
131
+ "Content-Length: 1\r\n" \
132
+ "Host: foo\r\n\r\n")
133
+ env = @request.read(client)
134
+ assert_equal Unicorn::TeeInput, env['rack.input'].class
135
+ end
136
+
137
+ def test_rack_lint_put
138
+ client = MockRequest.new(
139
+ "PUT / HTTP/1.1\r\n" \
140
+ "Host: foo\r\n" \
141
+ "Content-Length: 5\r\n" \
142
+ "\r\n" \
143
+ "abcde")
144
+ env = @request.read(client)
145
+ assert ! env.include?(:http_body)
146
+ res = @lint.call(env)
147
+ end
148
+
149
+ def test_rack_lint_big_put
150
+ count = 100
151
+ bs = 0x10000
152
+ buf = (' ' * bs).freeze
153
+ length = bs * count
154
+ client = Tempfile.new('big_put')
155
+ def client.kgio_addr; '127.0.0.1'; end
156
+ def client.kgio_read(*args)
157
+ readpartial(*args)
158
+ rescue EOFError
159
+ end
160
+ def client.kgio_read!(*args)
161
+ readpartial(*args)
162
+ end
163
+ client.syswrite(
164
+ "PUT / HTTP/1.1\r\n" \
165
+ "Host: foo\r\n" \
166
+ "Content-Length: #{length}\r\n" \
167
+ "\r\n")
168
+ count.times { assert_equal bs, client.syswrite(buf) }
169
+ assert_equal 0, client.sysseek(0)
170
+ env = @request.read(client)
171
+ assert ! env.include?(:http_body)
172
+ assert_equal length, env['rack.input'].size
173
+ count.times {
174
+ tmp = env['rack.input'].read(bs)
175
+ tmp << env['rack.input'].read(bs - tmp.size) if tmp.size != bs
176
+ assert_equal buf, tmp
177
+ }
178
+ assert_nil env['rack.input'].read(bs)
179
+ env['rack.input'].rewind
180
+ res = @lint.call(env)
181
+ end
182
+ end
@@ -0,0 +1,99 @@
1
+ # -*- encoding: binary -*-
2
+
3
+ # Copyright (c) 2005 Zed A. Shaw
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
+ # Additional work donated by contributors. See http://mongrel.rubyforge.org/attributions.html
8
+ # for more information.
9
+
10
+ require 'test/test_helper'
11
+ require 'time'
12
+
13
+ include Unicorn
14
+
15
+ class ResponseTest < Test::Unit::TestCase
16
+ include Unicorn::HttpResponse
17
+
18
+ def test_httpdate
19
+ before = Time.now.to_i - 1
20
+ str = httpdate
21
+ assert_kind_of(String, str)
22
+ middle = Time.parse(str).to_i
23
+ after = Time.now.to_i
24
+ assert before <= middle
25
+ assert middle <= after
26
+ end
27
+
28
+ def test_response_headers
29
+ out = StringIO.new
30
+ http_response_write(out, 200, {"X-Whatever" => "stuff"}, ["cool"])
31
+ assert ! out.closed?
32
+
33
+ assert out.length > 0, "output didn't have data"
34
+ end
35
+
36
+ def test_response_string_status
37
+ out = StringIO.new
38
+ http_response_write(out,'200', {}, [])
39
+ assert ! out.closed?
40
+ assert out.length > 0, "output didn't have data"
41
+ assert_equal 1, out.string.split(/\r\n/).grep(/^Status: 200 OK/).size
42
+ end
43
+
44
+ def test_response_200
45
+ io = StringIO.new
46
+ http_response_write(io, 200, {}, [])
47
+ assert ! io.closed?
48
+ assert io.length > 0, "output didn't have data"
49
+ end
50
+
51
+ def test_response_with_default_reason
52
+ code = 400
53
+ io = StringIO.new
54
+ http_response_write(io, code, {}, [])
55
+ assert ! io.closed?
56
+ lines = io.string.split(/\r\n/)
57
+ assert_match(/.* Bad Request$/, lines.first,
58
+ "wrong default reason phrase")
59
+ end
60
+
61
+ def test_rack_multivalue_headers
62
+ out = StringIO.new
63
+ http_response_write(out,200, {"X-Whatever" => "stuff\nbleh"}, [])
64
+ assert ! out.closed?
65
+ assert_match(/^X-Whatever: stuff\r\nX-Whatever: bleh\r\n/, out.string)
66
+ end
67
+
68
+ # Even though Rack explicitly forbids "Status" in the header hash,
69
+ # some broken clients still rely on it
70
+ def test_status_header_added
71
+ out = StringIO.new
72
+ http_response_write(out,200, {"X-Whatever" => "stuff"}, [])
73
+ assert ! out.closed?
74
+ assert_equal 1, out.string.split(/\r\n/).grep(/^Status: 200 OK/i).size
75
+ end
76
+
77
+ def test_body_closed
78
+ expect_body = %w(1 2 3 4).join("\n")
79
+ body = StringIO.new(expect_body)
80
+ body.rewind
81
+ out = StringIO.new
82
+ http_response_write(out,200, {}, body)
83
+ assert ! out.closed?
84
+ assert body.closed?
85
+ assert_match(expect_body, out.string.split(/\r\n/).last)
86
+ end
87
+
88
+ def test_unknown_status_pass_through
89
+ out = StringIO.new
90
+ http_response_write(out,"666 I AM THE BEAST", {}, [] )
91
+ assert ! out.closed?
92
+ headers = out.string.split(/\r\n\r\n/).first.split(/\r\n/)
93
+ assert %r{\AHTTP/\d\.\d 666 I AM THE BEAST\z}.match(headers[0])
94
+ status = headers.grep(/\AStatus:/i).first
95
+ assert status
96
+ assert_equal "Status: 666 I AM THE BEAST", status
97
+ end
98
+
99
+ end
@@ -0,0 +1,268 @@
1
+ # -*- encoding: binary -*-
2
+
3
+ # Copyright (c) 2005 Zed A. Shaw
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
+ # Additional work donated by contributors. See http://mongrel.rubyforge.org/attributions.html
8
+ # for more information.
9
+
10
+ require 'test/test_helper'
11
+
12
+ include Unicorn
13
+
14
+ class TestHandler
15
+
16
+ def call(env)
17
+ while env['rack.input'].read(4096)
18
+ end
19
+ [200, { 'Content-Type' => 'text/plain' }, ['hello!\n']]
20
+ rescue Unicorn::ClientShutdown, Unicorn::HttpParserError => e
21
+ $stderr.syswrite("#{e.class}: #{e.message} #{e.backtrace.empty?}\n")
22
+ raise e
23
+ end
24
+ end
25
+
26
+
27
+ class WebServerTest < Test::Unit::TestCase
28
+
29
+ def setup
30
+ @valid_request = "GET / HTTP/1.1\r\nHost: www.zedshaw.com\r\nContent-Type: text/plain\r\n\r\n"
31
+ @port = unused_port
32
+ @tester = TestHandler.new
33
+ redirect_test_io do
34
+ @server = HttpServer.new(@tester, :listeners => [ "127.0.0.1:#{@port}" ] )
35
+ @server.start
36
+ end
37
+ end
38
+
39
+ def teardown
40
+ redirect_test_io do
41
+ wait_workers_ready("test_stderr.#$$.log", 1)
42
+ File.truncate("test_stderr.#$$.log", 0)
43
+ @server.stop(false)
44
+ end
45
+ reset_sig_handlers
46
+ end
47
+
48
+ def test_preload_app_config
49
+ teardown
50
+ tmp = Tempfile.new('test_preload_app_config')
51
+ ObjectSpace.undefine_finalizer(tmp)
52
+ app = lambda { ||
53
+ tmp.sysseek(0)
54
+ tmp.truncate(0)
55
+ tmp.syswrite($$)
56
+ lambda { |env| [ 200, { 'Content-Type' => 'text/plain' }, [ "#$$\n" ] ] }
57
+ }
58
+ redirect_test_io do
59
+ @server = HttpServer.new(app, :listeners => [ "127.0.0.1:#@port"] )
60
+ @server.start
61
+ end
62
+ results = hit(["http://localhost:#@port/"])
63
+ worker_pid = results[0].to_i
64
+ assert worker_pid != 0
65
+ tmp.sysseek(0)
66
+ loader_pid = tmp.sysread(4096).to_i
67
+ assert loader_pid != 0
68
+ assert_equal worker_pid, loader_pid
69
+ teardown
70
+
71
+ redirect_test_io do
72
+ @server = HttpServer.new(app, :listeners => [ "127.0.0.1:#@port"],
73
+ :preload_app => true)
74
+ @server.start
75
+ end
76
+ results = hit(["http://localhost:#@port/"])
77
+ worker_pid = results[0].to_i
78
+ assert worker_pid != 0
79
+ tmp.sysseek(0)
80
+ loader_pid = tmp.sysread(4096).to_i
81
+ assert_equal $$, loader_pid
82
+ assert worker_pid != loader_pid
83
+ ensure
84
+ tmp.close!
85
+ end
86
+
87
+ def test_broken_app
88
+ teardown
89
+ app = lambda { |env| raise RuntimeError, "hello" }
90
+ # [200, {}, []] }
91
+ redirect_test_io do
92
+ @server = HttpServer.new(app, :listeners => [ "127.0.0.1:#@port"] )
93
+ @server.start
94
+ end
95
+ sock = TCPSocket.new('127.0.0.1', @port)
96
+ sock.syswrite("GET / HTTP/1.0\r\n\r\n")
97
+ assert_match %r{\AHTTP/1.[01] 500\b}, sock.sysread(4096)
98
+ assert_nil sock.close
99
+ end
100
+
101
+ def test_simple_server
102
+ results = hit(["http://localhost:#{@port}/test"])
103
+ assert_equal 'hello!\n', results[0], "Handler didn't really run"
104
+ end
105
+
106
+ def test_client_shutdown_writes
107
+ bs = 15609315 * rand
108
+ sock = TCPSocket.new('127.0.0.1', @port)
109
+ sock.syswrite("PUT /hello HTTP/1.1\r\n")
110
+ sock.syswrite("Host: example.com\r\n")
111
+ sock.syswrite("Transfer-Encoding: chunked\r\n")
112
+ sock.syswrite("Trailer: X-Foo\r\n")
113
+ sock.syswrite("\r\n")
114
+ sock.syswrite("%x\r\n" % [ bs ])
115
+ sock.syswrite("F" * bs)
116
+ sock.syswrite("\r\n0\r\nX-")
117
+ "Foo: bar\r\n\r\n".each_byte do |x|
118
+ sock.syswrite x.chr
119
+ sleep 0.05
120
+ end
121
+ # we wrote the entire request before shutting down, server should
122
+ # continue to process our request and never hit EOFError on our sock
123
+ sock.shutdown(Socket::SHUT_WR)
124
+ buf = sock.read
125
+ assert_equal 'hello!\n', buf.split(/\r\n\r\n/).last
126
+ next_client = Net::HTTP.get(URI.parse("http://127.0.0.1:#@port/"))
127
+ assert_equal 'hello!\n', next_client
128
+ lines = File.readlines("test_stderr.#$$.log")
129
+ assert lines.grep(/^Unicorn::ClientShutdown: /).empty?
130
+ assert_nil sock.close
131
+ end
132
+
133
+ def test_client_shutdown_write_truncates
134
+ bs = 15609315 * rand
135
+ sock = TCPSocket.new('127.0.0.1', @port)
136
+ sock.syswrite("PUT /hello HTTP/1.1\r\n")
137
+ sock.syswrite("Host: example.com\r\n")
138
+ sock.syswrite("Transfer-Encoding: chunked\r\n")
139
+ sock.syswrite("Trailer: X-Foo\r\n")
140
+ sock.syswrite("\r\n")
141
+ sock.syswrite("%x\r\n" % [ bs ])
142
+ sock.syswrite("F" * (bs / 2.0))
143
+
144
+ # shutdown prematurely, this will force the server to abort
145
+ # processing on us even during app dispatch
146
+ sock.shutdown(Socket::SHUT_WR)
147
+ IO.select([sock], nil, nil, 60) or raise "Timed out"
148
+ buf = sock.read
149
+ assert_equal "", buf
150
+ next_client = Net::HTTP.get(URI.parse("http://127.0.0.1:#@port/"))
151
+ assert_equal 'hello!\n', next_client
152
+ lines = File.readlines("test_stderr.#$$.log")
153
+ lines = lines.grep(/^Unicorn::ClientShutdown: bytes_read=\d+/)
154
+ assert_equal 1, lines.size
155
+ assert_match %r{\AUnicorn::ClientShutdown: bytes_read=\d+ true$}, lines[0]
156
+ assert_nil sock.close
157
+ end
158
+
159
+ def test_client_malformed_body
160
+ bs = 15653984
161
+ sock = TCPSocket.new('127.0.0.1', @port)
162
+ sock.syswrite("PUT /hello HTTP/1.1\r\n")
163
+ sock.syswrite("Host: example.com\r\n")
164
+ sock.syswrite("Transfer-Encoding: chunked\r\n")
165
+ sock.syswrite("Trailer: X-Foo\r\n")
166
+ sock.syswrite("\r\n")
167
+ sock.syswrite("%x\r\n" % [ bs ])
168
+ sock.syswrite("F" * bs)
169
+ begin
170
+ File.open("/dev/urandom", "rb") { |fp| sock.syswrite(fp.sysread(16384)) }
171
+ rescue
172
+ end
173
+ assert_nil sock.close
174
+ next_client = Net::HTTP.get(URI.parse("http://127.0.0.1:#@port/"))
175
+ assert_equal 'hello!\n', next_client
176
+ lines = File.readlines("test_stderr.#$$.log")
177
+ lines = lines.grep(/^Unicorn::HttpParserError: .* true$/)
178
+ assert_equal 1, lines.size
179
+ end
180
+
181
+ def do_test(string, chunk, close_after=nil, shutdown_delay=0)
182
+ # Do not use instance variables here, because it needs to be thread safe
183
+ socket = TCPSocket.new("127.0.0.1", @port);
184
+ request = StringIO.new(string)
185
+ chunks_out = 0
186
+
187
+ while data = request.read(chunk)
188
+ chunks_out += socket.write(data)
189
+ socket.flush
190
+ sleep 0.2
191
+ if close_after and chunks_out > close_after
192
+ socket.close
193
+ sleep 1
194
+ end
195
+ end
196
+ sleep(shutdown_delay)
197
+ socket.write(" ") # Some platforms only raise the exception on attempted write
198
+ socket.flush
199
+ end
200
+
201
+ def test_trickle_attack
202
+ do_test(@valid_request, 3)
203
+ end
204
+
205
+ def test_close_client
206
+ assert_raises IOError do
207
+ do_test(@valid_request, 10, 20)
208
+ end
209
+ end
210
+
211
+ def test_bad_client
212
+ redirect_test_io do
213
+ do_test("GET /test HTTP/BAD", 3)
214
+ end
215
+ end
216
+
217
+ def test_logger_set
218
+ assert_equal @server.logger, Unicorn::HttpRequest::DEFAULTS["rack.logger"]
219
+ end
220
+
221
+ def test_logger_changed
222
+ tmp = Logger.new($stdout)
223
+ @server.logger = tmp
224
+ assert_equal tmp, Unicorn::HttpRequest::DEFAULTS["rack.logger"]
225
+ end
226
+
227
+ def test_bad_client_400
228
+ sock = TCPSocket.new('127.0.0.1', @port)
229
+ sock.syswrite("GET / HTTP/1.0\r\nHost: foo\rbar\r\n\r\n")
230
+ assert_match %r{\AHTTP/1.[01] 400\b}, sock.sysread(4096)
231
+ assert_nil sock.close
232
+ end
233
+
234
+ def test_http_0_9
235
+ sock = TCPSocket.new('127.0.0.1', @port)
236
+ sock.syswrite("GET /hello\r\n")
237
+ assert_match 'hello!\n', sock.sysread(4096)
238
+ assert_nil sock.close
239
+ end
240
+
241
+ def test_header_is_too_long
242
+ redirect_test_io do
243
+ long = "GET /test HTTP/1.1\r\n" + ("X-Big: stuff\r\n" * 15000) + "\r\n"
244
+ assert_raises Errno::ECONNRESET, Errno::EPIPE, Errno::ECONNABORTED, Errno::EINVAL, IOError do
245
+ do_test(long, long.length/2, 10)
246
+ end
247
+ end
248
+ end
249
+
250
+ def test_file_streamed_request
251
+ body = "a" * (Unicorn::Const::MAX_BODY * 2)
252
+ long = "PUT /test HTTP/1.1\r\nContent-length: #{body.length}\r\n\r\n" + body
253
+ do_test(long, Unicorn::Const::CHUNK_SIZE * 2 - 400)
254
+ end
255
+
256
+ def test_file_streamed_request_bad_body
257
+ body = "a" * (Unicorn::Const::MAX_BODY * 2)
258
+ long = "GET /test HTTP/1.1\r\nContent-ength: #{body.length}\r\n\r\n" + body
259
+ assert_raises(EOFError,Errno::ECONNRESET,Errno::EPIPE,Errno::EINVAL,
260
+ Errno::EBADF) {
261
+ do_test(long, Unicorn::Const::CHUNK_SIZE * 2 - 400)
262
+ }
263
+ end
264
+
265
+ def test_listener_names
266
+ assert_equal [ "127.0.0.1:#@port" ], Unicorn.listener_names
267
+ end
268
+ end