unicorn-lb-patch 4.3.1.11.g21b8.dirty

Sign up to get free protection for your applications and to get access to all the features.
Files changed (249) hide show
  1. data/.CHANGELOG.old +25 -0
  2. data/.document +29 -0
  3. data/.gitignore +25 -0
  4. data/.mailmap +26 -0
  5. data/.wrongdoc.yml +10 -0
  6. data/Application_Timeouts +77 -0
  7. data/CONTRIBUTORS +35 -0
  8. data/COPYING +674 -0
  9. data/DESIGN +97 -0
  10. data/Documentation/.gitignore +5 -0
  11. data/Documentation/GNUmakefile +30 -0
  12. data/Documentation/unicorn.1.txt +174 -0
  13. data/Documentation/unicorn_rails.1.txt +175 -0
  14. data/FAQ +53 -0
  15. data/GIT-VERSION-GEN +40 -0
  16. data/GNUmakefile +294 -0
  17. data/HACKING +134 -0
  18. data/ISSUES +36 -0
  19. data/KNOWN_ISSUES +79 -0
  20. data/LICENSE +64 -0
  21. data/Links +56 -0
  22. data/PHILOSOPHY +145 -0
  23. data/README +149 -0
  24. data/Rakefile +97 -0
  25. data/SIGNALS +114 -0
  26. data/Sandbox +96 -0
  27. data/TODO +5 -0
  28. data/TUNING +98 -0
  29. data/bin/unicorn +121 -0
  30. data/bin/unicorn_rails +209 -0
  31. data/examples/big_app_gc.rb +2 -0
  32. data/examples/echo.ru +27 -0
  33. data/examples/git.ru +13 -0
  34. data/examples/init.sh +74 -0
  35. data/examples/logger_mp_safe.rb +25 -0
  36. data/examples/logrotate.conf +29 -0
  37. data/examples/nginx.conf +156 -0
  38. data/examples/unicorn.conf.minimal.rb +13 -0
  39. data/examples/unicorn.conf.rb +94 -0
  40. data/ext/unicorn_http/CFLAGS +13 -0
  41. data/ext/unicorn_http/c_util.h +124 -0
  42. data/ext/unicorn_http/common_field_optimization.h +111 -0
  43. data/ext/unicorn_http/ext_help.h +86 -0
  44. data/ext/unicorn_http/extconf.rb +10 -0
  45. data/ext/unicorn_http/global_variables.h +97 -0
  46. data/ext/unicorn_http/httpdate.c +82 -0
  47. data/ext/unicorn_http/unicorn_http.rl +1036 -0
  48. data/ext/unicorn_http/unicorn_http_common.rl +76 -0
  49. data/lib/unicorn.rb +107 -0
  50. data/lib/unicorn/app/exec_cgi.rb +154 -0
  51. data/lib/unicorn/app/inetd.rb +109 -0
  52. data/lib/unicorn/app/old_rails.rb +35 -0
  53. data/lib/unicorn/app/old_rails/static.rb +59 -0
  54. data/lib/unicorn/cgi_wrapper.rb +147 -0
  55. data/lib/unicorn/configurator.rb +630 -0
  56. data/lib/unicorn/const.rb +40 -0
  57. data/lib/unicorn/http_request.rb +77 -0
  58. data/lib/unicorn/http_response.rb +45 -0
  59. data/lib/unicorn/http_server.rb +755 -0
  60. data/lib/unicorn/launcher.rb +62 -0
  61. data/lib/unicorn/oob_gc.rb +71 -0
  62. data/lib/unicorn/preread_input.rb +33 -0
  63. data/lib/unicorn/socket_helper.rb +208 -0
  64. data/lib/unicorn/ssl_client.rb +11 -0
  65. data/lib/unicorn/ssl_configurator.rb +104 -0
  66. data/lib/unicorn/ssl_server.rb +42 -0
  67. data/lib/unicorn/stream_input.rb +149 -0
  68. data/lib/unicorn/tee_input.rb +126 -0
  69. data/lib/unicorn/tmpio.rb +29 -0
  70. data/lib/unicorn/util.rb +68 -0
  71. data/lib/unicorn/worker.rb +88 -0
  72. data/local.mk.sample +59 -0
  73. data/script/isolate_for_tests +50 -0
  74. data/setup.rb +1586 -0
  75. data/t/.gitignore +5 -0
  76. data/t/GNUmakefile +82 -0
  77. data/t/README +42 -0
  78. data/t/bin/content-md5-put +36 -0
  79. data/t/bin/sha1sum.rb +17 -0
  80. data/t/bin/unused_listen +40 -0
  81. data/t/bin/utee +12 -0
  82. data/t/broken-app.ru +12 -0
  83. data/t/detach.ru +11 -0
  84. data/t/env.ru +3 -0
  85. data/t/heartbeat-timeout.ru +12 -0
  86. data/t/listener_names.ru +4 -0
  87. data/t/my-tap-lib.sh +201 -0
  88. data/t/oob_gc.ru +21 -0
  89. data/t/oob_gc_path.ru +21 -0
  90. data/t/pid.ru +3 -0
  91. data/t/preread_input.ru +17 -0
  92. data/t/rack-input-tests.ru +21 -0
  93. data/t/rails3-app/.gitignore +4 -0
  94. data/t/rails3-app/Gemfile +26 -0
  95. data/t/rails3-app/Rakefile +10 -0
  96. data/t/rails3-app/app/controllers/application_controller.rb +4 -0
  97. data/t/rails3-app/app/helpers/application_helper.rb +2 -0
  98. data/t/rails3-app/app/views/layouts/application.html.erb +14 -0
  99. data/t/rails3-app/config.ru +4 -0
  100. data/t/rails3-app/config/application.rb +46 -0
  101. data/t/rails3-app/config/boot.rb +6 -0
  102. data/t/rails3-app/config/database.yml +22 -0
  103. data/t/rails3-app/config/environment.rb +5 -0
  104. data/t/rails3-app/config/environments/development.rb +19 -0
  105. data/t/rails3-app/config/environments/production.rb +42 -0
  106. data/t/rails3-app/config/environments/test.rb +32 -0
  107. data/t/rails3-app/config/initializers/backtrace_silencers.rb +7 -0
  108. data/t/rails3-app/config/initializers/inflections.rb +10 -0
  109. data/t/rails3-app/config/initializers/mime_types.rb +5 -0
  110. data/t/rails3-app/config/initializers/secret_token.rb +7 -0
  111. data/t/rails3-app/config/initializers/session_store.rb +8 -0
  112. data/t/rails3-app/config/locales/en.yml +5 -0
  113. data/t/rails3-app/config/routes.rb +58 -0
  114. data/t/rails3-app/db/seeds.rb +7 -0
  115. data/t/rails3-app/doc/README_FOR_APP +2 -0
  116. data/t/rails3-app/lib/tasks/.gitkeep +0 -0
  117. data/t/rails3-app/public/404.html +1 -0
  118. data/t/rails3-app/public/500.html +1 -0
  119. data/t/rails3-app/public/x.txt +1 -0
  120. data/t/rails3-app/script/rails +9 -0
  121. data/t/rails3-app/test/performance/browsing_test.rb +9 -0
  122. data/t/rails3-app/test/test_helper.rb +13 -0
  123. data/t/rails3-app/vendor/plugins/.gitkeep +0 -0
  124. data/t/sslgen.sh +71 -0
  125. data/t/t0000-http-basic.sh +50 -0
  126. data/t/t0001-reload-bad-config.sh +53 -0
  127. data/t/t0002-config-conflict.sh +49 -0
  128. data/t/t0002-parser-error.sh +94 -0
  129. data/t/t0003-working_directory.sh +51 -0
  130. data/t/t0004-heartbeat-timeout.sh +69 -0
  131. data/t/t0004-working_directory_broken.sh +24 -0
  132. data/t/t0005-working_directory_app.rb.sh +37 -0
  133. data/t/t0006-reopen-logs.sh +83 -0
  134. data/t/t0006.ru +13 -0
  135. data/t/t0007-working_directory_no_embed_cli.sh +44 -0
  136. data/t/t0008-back_out_of_upgrade.sh +110 -0
  137. data/t/t0009-broken-app.sh +56 -0
  138. data/t/t0009-winch_ttin.sh +59 -0
  139. data/t/t0010-reap-logging.sh +55 -0
  140. data/t/t0011-active-unix-socket.sh +79 -0
  141. data/t/t0012-reload-empty-config.sh +85 -0
  142. data/t/t0013-rewindable-input-false.sh +24 -0
  143. data/t/t0013.ru +12 -0
  144. data/t/t0014-rewindable-input-true.sh +24 -0
  145. data/t/t0014.ru +12 -0
  146. data/t/t0015-configurator-internals.sh +25 -0
  147. data/t/t0016-trust-x-forwarded-false.sh +30 -0
  148. data/t/t0017-trust-x-forwarded-true.sh +30 -0
  149. data/t/t0018-write-on-close.sh +23 -0
  150. data/t/t0019-max_header_len.sh +49 -0
  151. data/t/t0020-at_exit-handler.sh +49 -0
  152. data/t/t0021-process_detach.sh +29 -0
  153. data/t/t0022-listener-names-preload_app.sh +32 -0
  154. data/t/t0100-rack-input-tests.sh +124 -0
  155. data/t/t0116-client_body_buffer_size.sh +80 -0
  156. data/t/t0116.ru +16 -0
  157. data/t/t0300-rails3-basic.sh +28 -0
  158. data/t/t0301-rails3-missing-config-ru.sh +33 -0
  159. data/t/t0302-rails3-alt-working_directory.sh +32 -0
  160. data/t/t0303-rails3-alt-working_directory_config.ru.sh +56 -0
  161. data/t/t0304-rails3-alt-working_directory_no_embed_cli.sh +52 -0
  162. data/t/t0600-https-server-basic.sh +48 -0
  163. data/t/t9000-preread-input.sh +48 -0
  164. data/t/t9001-oob_gc.sh +47 -0
  165. data/t/t9002-oob_gc-path.sh +75 -0
  166. data/t/test-lib.sh +113 -0
  167. data/t/test-rails3.sh +27 -0
  168. data/t/write-on-close.ru +11 -0
  169. data/test/aggregate.rb +15 -0
  170. data/test/benchmark/README +50 -0
  171. data/test/benchmark/dd.ru +18 -0
  172. data/test/benchmark/stack.ru +8 -0
  173. data/test/exec/README +5 -0
  174. data/test/exec/test_exec.rb +1055 -0
  175. data/test/rails/app-1.2.3/.gitignore +2 -0
  176. data/test/rails/app-1.2.3/Rakefile +7 -0
  177. data/test/rails/app-1.2.3/app/controllers/application.rb +6 -0
  178. data/test/rails/app-1.2.3/app/controllers/foo_controller.rb +36 -0
  179. data/test/rails/app-1.2.3/app/helpers/application_helper.rb +4 -0
  180. data/test/rails/app-1.2.3/config/boot.rb +11 -0
  181. data/test/rails/app-1.2.3/config/database.yml +12 -0
  182. data/test/rails/app-1.2.3/config/environment.rb +13 -0
  183. data/test/rails/app-1.2.3/config/environments/development.rb +9 -0
  184. data/test/rails/app-1.2.3/config/environments/production.rb +5 -0
  185. data/test/rails/app-1.2.3/config/routes.rb +6 -0
  186. data/test/rails/app-1.2.3/db/.gitignore +0 -0
  187. data/test/rails/app-1.2.3/public/404.html +1 -0
  188. data/test/rails/app-1.2.3/public/500.html +1 -0
  189. data/test/rails/app-2.0.2/.gitignore +2 -0
  190. data/test/rails/app-2.0.2/Rakefile +7 -0
  191. data/test/rails/app-2.0.2/app/controllers/application.rb +4 -0
  192. data/test/rails/app-2.0.2/app/controllers/foo_controller.rb +36 -0
  193. data/test/rails/app-2.0.2/app/helpers/application_helper.rb +4 -0
  194. data/test/rails/app-2.0.2/config/boot.rb +11 -0
  195. data/test/rails/app-2.0.2/config/database.yml +12 -0
  196. data/test/rails/app-2.0.2/config/environment.rb +17 -0
  197. data/test/rails/app-2.0.2/config/environments/development.rb +8 -0
  198. data/test/rails/app-2.0.2/config/environments/production.rb +5 -0
  199. data/test/rails/app-2.0.2/config/routes.rb +6 -0
  200. data/test/rails/app-2.0.2/db/.gitignore +0 -0
  201. data/test/rails/app-2.0.2/public/404.html +1 -0
  202. data/test/rails/app-2.0.2/public/500.html +1 -0
  203. data/test/rails/app-2.1.2/.gitignore +2 -0
  204. data/test/rails/app-2.1.2/Rakefile +7 -0
  205. data/test/rails/app-2.1.2/app/controllers/application.rb +4 -0
  206. data/test/rails/app-2.1.2/app/controllers/foo_controller.rb +36 -0
  207. data/test/rails/app-2.1.2/app/helpers/application_helper.rb +4 -0
  208. data/test/rails/app-2.1.2/config/boot.rb +111 -0
  209. data/test/rails/app-2.1.2/config/database.yml +12 -0
  210. data/test/rails/app-2.1.2/config/environment.rb +17 -0
  211. data/test/rails/app-2.1.2/config/environments/development.rb +7 -0
  212. data/test/rails/app-2.1.2/config/environments/production.rb +5 -0
  213. data/test/rails/app-2.1.2/config/routes.rb +6 -0
  214. data/test/rails/app-2.1.2/db/.gitignore +0 -0
  215. data/test/rails/app-2.1.2/public/404.html +1 -0
  216. data/test/rails/app-2.1.2/public/500.html +1 -0
  217. data/test/rails/app-2.2.2/.gitignore +2 -0
  218. data/test/rails/app-2.2.2/Rakefile +7 -0
  219. data/test/rails/app-2.2.2/app/controllers/application.rb +4 -0
  220. data/test/rails/app-2.2.2/app/controllers/foo_controller.rb +36 -0
  221. data/test/rails/app-2.2.2/app/helpers/application_helper.rb +4 -0
  222. data/test/rails/app-2.2.2/config/boot.rb +111 -0
  223. data/test/rails/app-2.2.2/config/database.yml +12 -0
  224. data/test/rails/app-2.2.2/config/environment.rb +17 -0
  225. data/test/rails/app-2.2.2/config/environments/development.rb +7 -0
  226. data/test/rails/app-2.2.2/config/environments/production.rb +5 -0
  227. data/test/rails/app-2.2.2/config/routes.rb +6 -0
  228. data/test/rails/app-2.2.2/db/.gitignore +0 -0
  229. data/test/rails/app-2.2.2/public/404.html +1 -0
  230. data/test/rails/app-2.2.2/public/500.html +1 -0
  231. data/test/rails/test_rails.rb +287 -0
  232. data/test/test_helper.rb +300 -0
  233. data/test/unit/test_configurator.rb +158 -0
  234. data/test/unit/test_droplet.rb +28 -0
  235. data/test/unit/test_http_parser.rb +860 -0
  236. data/test/unit/test_http_parser_ng.rb +716 -0
  237. data/test/unit/test_http_parser_xftrust.rb +38 -0
  238. data/test/unit/test_request.rb +197 -0
  239. data/test/unit/test_response.rb +99 -0
  240. data/test/unit/test_server.rb +289 -0
  241. data/test/unit/test_signals.rb +207 -0
  242. data/test/unit/test_sni_hostnames.rb +47 -0
  243. data/test/unit/test_socket_helper.rb +192 -0
  244. data/test/unit/test_stream_input.rb +204 -0
  245. data/test/unit/test_tee_input.rb +296 -0
  246. data/test/unit/test_upload.rb +306 -0
  247. data/test/unit/test_util.rb +100 -0
  248. data/unicorn-lb-patch.gemspec +27 -0
  249. metadata +561 -0
@@ -0,0 +1,296 @@
1
+ # -*- encoding: binary -*-
2
+
3
+ require 'test/unit'
4
+ require 'digest/sha1'
5
+ require 'unicorn'
6
+
7
+ class TeeInput < Unicorn::TeeInput
8
+ attr_accessor :tmp, :len
9
+ end
10
+
11
+ class TestTeeInput < Test::Unit::TestCase
12
+
13
+ def setup
14
+ @rs = $/
15
+ @rd, @wr = Kgio::UNIXSocket.pair
16
+ @rd.sync = @wr.sync = true
17
+ @start_pid = $$
18
+ end
19
+
20
+ def teardown
21
+ return if $$ != @start_pid
22
+ $/ = @rs
23
+ @rd.close rescue nil
24
+ @wr.close rescue nil
25
+ begin
26
+ Process.wait
27
+ rescue Errno::ECHILD
28
+ break
29
+ end while true
30
+ end
31
+
32
+ def test_gets_long
33
+ r = init_request("hello", 5 + (4096 * 4 * 3) + "#$/foo#$/".size)
34
+ ti = TeeInput.new(@rd, r)
35
+ status = line = nil
36
+ pid = fork {
37
+ @rd.close
38
+ 3.times { @wr.write("ffff" * 4096) }
39
+ @wr.write "#$/foo#$/"
40
+ @wr.close
41
+ }
42
+ @wr.close
43
+ assert_nothing_raised { line = ti.gets }
44
+ assert_equal(4096 * 4 * 3 + 5 + $/.size, line.size)
45
+ assert_equal("hello" << ("ffff" * 4096 * 3) << "#$/", line)
46
+ assert_nothing_raised { line = ti.gets }
47
+ assert_equal "foo#$/", line
48
+ assert_nil ti.gets
49
+ assert_nothing_raised { pid, status = Process.waitpid2(pid) }
50
+ assert status.success?
51
+ end
52
+
53
+ def test_gets_short
54
+ r = init_request("hello", 5 + "#$/foo".size)
55
+ ti = TeeInput.new(@rd, r)
56
+ status = line = nil
57
+ pid = fork {
58
+ @rd.close
59
+ @wr.write "#$/foo"
60
+ @wr.close
61
+ }
62
+ @wr.close
63
+ assert_nothing_raised { line = ti.gets }
64
+ assert_equal("hello#$/", line)
65
+ assert_nothing_raised { line = ti.gets }
66
+ assert_equal "foo", line
67
+ assert_nil ti.gets
68
+ assert_nothing_raised { pid, status = Process.waitpid2(pid) }
69
+ assert status.success?
70
+ end
71
+
72
+ def test_small_body
73
+ r = init_request('hello')
74
+ ti = TeeInput.new(@rd, r)
75
+ assert_equal 0, @parser.content_length
76
+ assert @parser.body_eof?
77
+ assert_equal StringIO, ti.tmp.class
78
+ assert_equal 0, ti.tmp.pos
79
+ assert_equal 5, ti.size
80
+ assert_equal 'hello', ti.read
81
+ assert_equal '', ti.read
82
+ assert_nil ti.read(4096)
83
+ assert_equal 5, ti.size
84
+ end
85
+
86
+ def test_read_with_buffer
87
+ r = init_request('hello')
88
+ ti = TeeInput.new(@rd, r)
89
+ buf = ''
90
+ rv = ti.read(4, buf)
91
+ assert_equal 'hell', rv
92
+ assert_equal 'hell', buf
93
+ assert_equal rv.object_id, buf.object_id
94
+ assert_equal 'o', ti.read
95
+ assert_equal nil, ti.read(5, buf)
96
+ assert_equal 0, ti.rewind
97
+ assert_equal 'hello', ti.read(5, buf)
98
+ assert_equal 'hello', buf
99
+ end
100
+
101
+ def test_big_body
102
+ r = init_request('.' * Unicorn::Const::MAX_BODY << 'a')
103
+ ti = TeeInput.new(@rd, r)
104
+ assert_equal 0, @parser.content_length
105
+ assert @parser.body_eof?
106
+ assert_kind_of File, ti.tmp
107
+ assert_equal 0, ti.tmp.pos
108
+ assert_equal Unicorn::Const::MAX_BODY + 1, ti.size
109
+ end
110
+
111
+ def test_read_in_full_if_content_length
112
+ a, b = 300, 3
113
+ r = init_request('.' * b, 300)
114
+ assert_equal 300, @parser.content_length
115
+ ti = TeeInput.new(@rd, r)
116
+ pid = fork {
117
+ @wr.write('.' * 197)
118
+ sleep 1 # still a *potential* race here that would make the test moot...
119
+ @wr.write('.' * 100)
120
+ }
121
+ assert_equal a, ti.read(a).size
122
+ _, status = Process.waitpid2(pid)
123
+ assert status.success?
124
+ @wr.close
125
+ end
126
+
127
+ def test_big_body_multi
128
+ r = init_request('.', Unicorn::Const::MAX_BODY + 1)
129
+ ti = TeeInput.new(@rd, r)
130
+ assert_equal Unicorn::Const::MAX_BODY, @parser.content_length
131
+ assert ! @parser.body_eof?
132
+ assert_kind_of File, ti.tmp
133
+ assert_equal 0, ti.tmp.pos
134
+ assert_equal Unicorn::Const::MAX_BODY + 1, ti.size
135
+ nr = Unicorn::Const::MAX_BODY / 4
136
+ pid = fork {
137
+ @rd.close
138
+ nr.times { @wr.write('....') }
139
+ @wr.close
140
+ }
141
+ @wr.close
142
+ assert_equal '.', ti.read(1)
143
+ assert_equal Unicorn::Const::MAX_BODY + 1, ti.size
144
+ nr.times { |x|
145
+ assert_equal '....', ti.read(4), "nr=#{x}"
146
+ assert_equal Unicorn::Const::MAX_BODY + 1, ti.size
147
+ }
148
+ assert_nil ti.read(1)
149
+ status = nil
150
+ assert_nothing_raised { pid, status = Process.waitpid2(pid) }
151
+ assert status.success?
152
+ end
153
+
154
+ def test_chunked
155
+ @parser = Unicorn::HttpParser.new
156
+ @parser.buf << "POST / HTTP/1.1\r\n" \
157
+ "Host: localhost\r\n" \
158
+ "Transfer-Encoding: chunked\r\n" \
159
+ "\r\n"
160
+ assert @parser.parse
161
+ assert_equal "", @parser.buf
162
+
163
+ pid = fork {
164
+ @rd.close
165
+ 5.times { @wr.write("5\r\nabcde\r\n") }
166
+ @wr.write("0\r\n\r\n")
167
+ }
168
+ @wr.close
169
+ ti = TeeInput.new(@rd, @parser)
170
+ assert_nil @parser.content_length
171
+ assert_nil ti.len
172
+ assert ! @parser.body_eof?
173
+ assert_equal 25, ti.size
174
+ assert @parser.body_eof?
175
+ assert_equal 25, ti.len
176
+ assert_equal 0, ti.tmp.pos
177
+ assert_nothing_raised { ti.rewind }
178
+ assert_equal 0, ti.tmp.pos
179
+ assert_equal 'abcdeabcdeabcdeabcde', ti.read(20)
180
+ assert_equal 20, ti.tmp.pos
181
+ assert_nothing_raised { ti.rewind }
182
+ assert_equal 0, ti.tmp.pos
183
+ assert_kind_of File, ti.tmp
184
+ status = nil
185
+ assert_nothing_raised { pid, status = Process.waitpid2(pid) }
186
+ assert status.success?
187
+ end
188
+
189
+ def test_chunked_ping_pong
190
+ @parser = Unicorn::HttpParser.new
191
+ buf = @parser.buf
192
+ buf << "POST / HTTP/1.1\r\n" \
193
+ "Host: localhost\r\n" \
194
+ "Transfer-Encoding: chunked\r\n" \
195
+ "\r\n"
196
+ assert @parser.parse
197
+ assert_equal "", buf
198
+ chunks = %w(aa bbb cccc dddd eeee)
199
+ rd, wr = IO.pipe
200
+
201
+ pid = fork {
202
+ chunks.each do |chunk|
203
+ rd.read(1) == "." and
204
+ @wr.write("#{'%x' % [ chunk.size]}\r\n#{chunk}\r\n")
205
+ end
206
+ @wr.write("0\r\n\r\n")
207
+ }
208
+ ti = TeeInput.new(@rd, @parser)
209
+ assert_nil @parser.content_length
210
+ assert_nil ti.len
211
+ assert ! @parser.body_eof?
212
+ chunks.each do |chunk|
213
+ wr.write('.')
214
+ assert_equal chunk, ti.read(16384)
215
+ end
216
+ _, status = Process.waitpid2(pid)
217
+ assert status.success?
218
+ end
219
+
220
+ def test_chunked_with_trailer
221
+ @parser = Unicorn::HttpParser.new
222
+ buf = @parser.buf
223
+ buf << "POST / HTTP/1.1\r\n" \
224
+ "Host: localhost\r\n" \
225
+ "Trailer: Hello\r\n" \
226
+ "Transfer-Encoding: chunked\r\n" \
227
+ "\r\n"
228
+ assert @parser.parse
229
+ assert_equal "", buf
230
+
231
+ pid = fork {
232
+ @rd.close
233
+ 5.times { @wr.write("5\r\nabcde\r\n") }
234
+ @wr.write("0\r\n")
235
+ @wr.write("Hello: World\r\n\r\n")
236
+ }
237
+ @wr.close
238
+ ti = TeeInput.new(@rd, @parser)
239
+ assert_nil @parser.content_length
240
+ assert_nil ti.len
241
+ assert ! @parser.body_eof?
242
+ assert_equal 25, ti.size
243
+ assert_equal "World", @parser.env['HTTP_HELLO']
244
+ status = nil
245
+ assert_nothing_raised { pid, status = Process.waitpid2(pid) }
246
+ assert status.success?
247
+ end
248
+
249
+ def test_chunked_and_size_slow
250
+ @parser = Unicorn::HttpParser.new
251
+ buf = @parser.buf
252
+ buf << "POST / HTTP/1.1\r\n" \
253
+ "Host: localhost\r\n" \
254
+ "Trailer: Hello\r\n" \
255
+ "Transfer-Encoding: chunked\r\n" \
256
+ "\r\n"
257
+ assert @parser.parse
258
+ assert_equal "", buf
259
+
260
+ @wr.write("9\r\nabcde")
261
+ ti = TeeInput.new(@rd, @parser)
262
+ assert_nil @parser.content_length
263
+ assert_equal "abcde", ti.read(9)
264
+ assert ! @parser.body_eof?
265
+ @wr.write("fghi\r\n0\r\nHello: World\r\n\r\n")
266
+ assert_equal 9, ti.size
267
+ assert_equal "fghi", ti.read(9)
268
+ assert_equal nil, ti.read(9)
269
+ assert_equal "World", @parser.env['HTTP_HELLO']
270
+ end
271
+
272
+ def test_gets_read_mix
273
+ r = init_request("hello\nasdfasdf")
274
+ ti = Unicorn::TeeInput.new(@rd, r)
275
+ assert_equal "hello\n", ti.gets
276
+ assert_equal "asdfasdf", ti.read(9)
277
+ assert_nil ti.read(9)
278
+ end
279
+
280
+ private
281
+
282
+ def init_request(body, size = nil)
283
+ @parser = Unicorn::HttpParser.new
284
+ body = body.to_s.freeze
285
+ buf = @parser.buf
286
+ buf << "POST / HTTP/1.1\r\n" \
287
+ "Host: localhost\r\n" \
288
+ "Content-Length: #{size || body.size}\r\n" \
289
+ "\r\n#{body}"
290
+ assert @parser.parse
291
+ assert_equal body, buf
292
+ @buf = buf
293
+ @parser
294
+ end
295
+
296
+ end
@@ -0,0 +1,306 @@
1
+ # -*- encoding: binary -*-
2
+
3
+ # Copyright (c) 2009 Eric Wong
4
+ require 'test/test_helper'
5
+ require 'digest/md5'
6
+
7
+ include Unicorn
8
+
9
+ class UploadTest < Test::Unit::TestCase
10
+
11
+ def setup
12
+ @addr = ENV['UNICORN_TEST_ADDR'] || '127.0.0.1'
13
+ @port = unused_port
14
+ @hdr = {'Content-Type' => 'text/plain', 'Content-Length' => '0'}
15
+ @bs = 4096
16
+ @count = 256
17
+ @server = nil
18
+
19
+ # we want random binary data to test 1.9 encoding-aware IO craziness
20
+ @random = File.open('/dev/urandom','rb')
21
+ @sha1 = Digest::SHA1.new
22
+ @sha1_app = lambda do |env|
23
+ input = env['rack.input']
24
+ resp = {}
25
+
26
+ @sha1.reset
27
+ while buf = input.read(@bs)
28
+ @sha1.update(buf)
29
+ end
30
+ resp[:sha1] = @sha1.hexdigest
31
+
32
+ # rewind and read again
33
+ input.rewind
34
+ @sha1.reset
35
+ while buf = input.read(@bs)
36
+ @sha1.update(buf)
37
+ end
38
+
39
+ if resp[:sha1] == @sha1.hexdigest
40
+ resp[:sysread_read_byte_match] = true
41
+ end
42
+
43
+ if expect_size = env['HTTP_X_EXPECT_SIZE']
44
+ if expect_size.to_i == input.size
45
+ resp[:expect_size_match] = true
46
+ end
47
+ end
48
+ resp[:size] = input.size
49
+ resp[:content_md5] = env['HTTP_CONTENT_MD5']
50
+
51
+ [ 200, @hdr.merge({'X-Resp' => resp.inspect}), [] ]
52
+ end
53
+ end
54
+
55
+ def teardown
56
+ redirect_test_io { @server.stop(false) } if @server
57
+ @random.close
58
+ reset_sig_handlers
59
+ end
60
+
61
+ def test_put
62
+ start_server(@sha1_app)
63
+ sock = TCPSocket.new(@addr, @port)
64
+ sock.syswrite("PUT / HTTP/1.0\r\nContent-Length: #{length}\r\n\r\n")
65
+ @count.times do |i|
66
+ buf = @random.sysread(@bs)
67
+ @sha1.update(buf)
68
+ sock.syswrite(buf)
69
+ end
70
+ read = sock.read.split(/\r\n/)
71
+ assert_equal "HTTP/1.1 200 OK", read[0]
72
+ resp = eval(read.grep(/^X-Resp: /).first.sub!(/X-Resp: /, ''))
73
+ assert_equal length, resp[:size]
74
+ assert_equal @sha1.hexdigest, resp[:sha1]
75
+ end
76
+
77
+ def test_put_content_md5
78
+ md5 = Digest::MD5.new
79
+ start_server(@sha1_app)
80
+ sock = TCPSocket.new(@addr, @port)
81
+ sock.syswrite("PUT / HTTP/1.0\r\nTransfer-Encoding: chunked\r\n" \
82
+ "Trailer: Content-MD5\r\n\r\n")
83
+ @count.times do |i|
84
+ buf = @random.sysread(@bs)
85
+ @sha1.update(buf)
86
+ md5.update(buf)
87
+ sock.syswrite("#{'%x' % buf.size}\r\n")
88
+ sock.syswrite(buf << "\r\n")
89
+ end
90
+ sock.syswrite("0\r\n")
91
+
92
+ content_md5 = [ md5.digest! ].pack('m').strip.freeze
93
+ sock.syswrite("Content-MD5: #{content_md5}\r\n\r\n")
94
+ read = sock.read.split(/\r\n/)
95
+ assert_equal "HTTP/1.1 200 OK", read[0]
96
+ resp = eval(read.grep(/^X-Resp: /).first.sub!(/X-Resp: /, ''))
97
+ assert_equal length, resp[:size]
98
+ assert_equal @sha1.hexdigest, resp[:sha1]
99
+ assert_equal content_md5, resp[:content_md5]
100
+ end
101
+
102
+ def test_put_trickle_small
103
+ @count, @bs = 2, 128
104
+ start_server(@sha1_app)
105
+ assert_equal 256, length
106
+ sock = TCPSocket.new(@addr, @port)
107
+ hdr = "PUT / HTTP/1.0\r\nContent-Length: #{length}\r\n\r\n"
108
+ @count.times do
109
+ buf = @random.sysread(@bs)
110
+ @sha1.update(buf)
111
+ hdr << buf
112
+ sock.syswrite(hdr)
113
+ hdr = ''
114
+ sleep 0.6
115
+ end
116
+ read = sock.read.split(/\r\n/)
117
+ assert_equal "HTTP/1.1 200 OK", read[0]
118
+ resp = eval(read.grep(/^X-Resp: /).first.sub!(/X-Resp: /, ''))
119
+ assert_equal length, resp[:size]
120
+ assert_equal @sha1.hexdigest, resp[:sha1]
121
+ end
122
+
123
+ def test_put_keepalive_truncates_small_overwrite
124
+ start_server(@sha1_app)
125
+ sock = TCPSocket.new(@addr, @port)
126
+ to_upload = length + 1
127
+ sock.syswrite("PUT / HTTP/1.0\r\nContent-Length: #{to_upload}\r\n\r\n")
128
+ @count.times do
129
+ buf = @random.sysread(@bs)
130
+ @sha1.update(buf)
131
+ sock.syswrite(buf)
132
+ end
133
+ sock.syswrite('12345') # write 4 bytes more than we expected
134
+ @sha1.update('1')
135
+
136
+ buf = sock.readpartial(4096)
137
+ while buf !~ /\r\n\r\n/
138
+ buf << sock.readpartial(4096)
139
+ end
140
+ read = buf.split(/\r\n/)
141
+ assert_equal "HTTP/1.1 200 OK", read[0]
142
+ resp = eval(read.grep(/^X-Resp: /).first.sub!(/X-Resp: /, ''))
143
+ assert_equal to_upload, resp[:size]
144
+ assert_equal @sha1.hexdigest, resp[:sha1]
145
+ end
146
+
147
+ def test_put_excessive_overwrite_closed
148
+ tmp = Tempfile.new('overwrite_check')
149
+ tmp.sync = true
150
+ start_server(lambda { |env|
151
+ nr = 0
152
+ while buf = env['rack.input'].read(65536)
153
+ nr += buf.size
154
+ end
155
+ tmp.write(nr.to_s)
156
+ [ 200, @hdr, [] ]
157
+ })
158
+ sock = TCPSocket.new(@addr, @port)
159
+ buf = ' ' * @bs
160
+ sock.syswrite("PUT / HTTP/1.0\r\nContent-Length: #{length}\r\n\r\n")
161
+
162
+ @count.times { sock.syswrite(buf) }
163
+ assert_raise(Errno::ECONNRESET, Errno::EPIPE) do
164
+ ::Unicorn::Const::CHUNK_SIZE.times { sock.syswrite(buf) }
165
+ end
166
+ assert_nothing_raised { sock.gets }
167
+ tmp.rewind
168
+ assert_equal length, tmp.read.to_i
169
+ end
170
+
171
+ # Despite reading numerous articles and inspecting the 1.9.1-p0 C
172
+ # source, Eric Wong will never trust that we're always handling
173
+ # encoding-aware IO objects correctly. Thus this test uses shell
174
+ # utilities that should always operate on files/sockets on a
175
+ # byte-level.
176
+ def test_uncomfortable_with_onenine_encodings
177
+ # POSIX doesn't require all of these to be present on a system
178
+ which('curl') or return
179
+ which('sha1sum') or return
180
+ which('dd') or return
181
+
182
+ start_server(@sha1_app)
183
+
184
+ tmp = Tempfile.new('dd_dest')
185
+ assert(system("dd", "if=#{@random.path}", "of=#{tmp.path}",
186
+ "bs=#{@bs}", "count=#{@count}"),
187
+ "dd #@random to #{tmp}")
188
+ sha1_re = %r!\b([a-f0-9]{40})\b!
189
+ sha1_out = `sha1sum #{tmp.path}`
190
+ assert $?.success?, 'sha1sum ran OK'
191
+
192
+ assert_match(sha1_re, sha1_out)
193
+ sha1 = sha1_re.match(sha1_out)[1]
194
+ resp = `curl -isSfN -T#{tmp.path} http://#@addr:#@port/`
195
+ assert $?.success?, 'curl ran OK'
196
+ assert_match(%r!\b#{sha1}\b!, resp)
197
+ assert_match(/sysread_read_byte_match/, resp)
198
+
199
+ # small StringIO path
200
+ assert(system("dd", "if=#{@random.path}", "of=#{tmp.path}",
201
+ "bs=1024", "count=1"),
202
+ "dd #@random to #{tmp}")
203
+ sha1_re = %r!\b([a-f0-9]{40})\b!
204
+ sha1_out = `sha1sum #{tmp.path}`
205
+ assert $?.success?, 'sha1sum ran OK'
206
+
207
+ assert_match(sha1_re, sha1_out)
208
+ sha1 = sha1_re.match(sha1_out)[1]
209
+ resp = `curl -isSfN -T#{tmp.path} http://#@addr:#@port/`
210
+ assert $?.success?, 'curl ran OK'
211
+ assert_match(%r!\b#{sha1}\b!, resp)
212
+ assert_match(/sysread_read_byte_match/, resp)
213
+ end
214
+
215
+ def test_chunked_upload_via_curl
216
+ # POSIX doesn't require all of these to be present on a system
217
+ which('curl') or return
218
+ which('sha1sum') or return
219
+ which('dd') or return
220
+
221
+ start_server(@sha1_app)
222
+
223
+ tmp = Tempfile.new('dd_dest')
224
+ assert(system("dd", "if=#{@random.path}", "of=#{tmp.path}",
225
+ "bs=#{@bs}", "count=#{@count}"),
226
+ "dd #@random to #{tmp}")
227
+ sha1_re = %r!\b([a-f0-9]{40})\b!
228
+ sha1_out = `sha1sum #{tmp.path}`
229
+ assert $?.success?, 'sha1sum ran OK'
230
+
231
+ assert_match(sha1_re, sha1_out)
232
+ sha1 = sha1_re.match(sha1_out)[1]
233
+ cmd = "curl -H 'X-Expect-Size: #{tmp.size}' --tcp-nodelay \
234
+ -isSf --no-buffer -T- " \
235
+ "http://#@addr:#@port/"
236
+ resp = Tempfile.new('resp')
237
+ resp.sync = true
238
+
239
+ rd, wr = IO.pipe
240
+ wr.sync = rd.sync = true
241
+ pid = fork {
242
+ STDIN.reopen(rd)
243
+ rd.close
244
+ wr.close
245
+ STDOUT.reopen(resp)
246
+ exec cmd
247
+ }
248
+ rd.close
249
+
250
+ tmp.rewind
251
+ @count.times { |i|
252
+ wr.write(tmp.read(@bs))
253
+ sleep(rand / 10) if 0 == i % 8
254
+ }
255
+ wr.close
256
+ pid, status = Process.waitpid2(pid)
257
+
258
+ resp.rewind
259
+ resp = resp.read
260
+ assert status.success?, 'curl ran OK'
261
+ assert_match(%r!\b#{sha1}\b!, resp)
262
+ assert_match(/sysread_read_byte_match/, resp)
263
+ assert_match(/expect_size_match/, resp)
264
+ end
265
+
266
+ def test_curl_chunked_small
267
+ # POSIX doesn't require all of these to be present on a system
268
+ which('curl') or return
269
+ which('sha1sum') or return
270
+ which('dd') or return
271
+
272
+ start_server(@sha1_app)
273
+
274
+ tmp = Tempfile.new('dd_dest')
275
+ # small StringIO path
276
+ assert(system("dd", "if=#{@random.path}", "of=#{tmp.path}",
277
+ "bs=1024", "count=1"),
278
+ "dd #@random to #{tmp}")
279
+ sha1_re = %r!\b([a-f0-9]{40})\b!
280
+ sha1_out = `sha1sum #{tmp.path}`
281
+ assert $?.success?, 'sha1sum ran OK'
282
+
283
+ assert_match(sha1_re, sha1_out)
284
+ sha1 = sha1_re.match(sha1_out)[1]
285
+ resp = `curl -H 'X-Expect-Size: #{tmp.size}' --tcp-nodelay \
286
+ -isSf --no-buffer -T- http://#@addr:#@port/ < #{tmp.path}`
287
+ assert $?.success?, 'curl ran OK'
288
+ assert_match(%r!\b#{sha1}\b!, resp)
289
+ assert_match(/sysread_read_byte_match/, resp)
290
+ assert_match(/expect_size_match/, resp)
291
+ end
292
+
293
+ private
294
+
295
+ def length
296
+ @bs * @count
297
+ end
298
+
299
+ def start_server(app)
300
+ redirect_test_io do
301
+ @server = HttpServer.new(app, :listeners => [ "#{@addr}:#{@port}" ] )
302
+ @server.start
303
+ end
304
+ end
305
+
306
+ end