1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
| | # -*- encoding: binary -*-
require 'test/unit'
require 'digest/sha1'
require 'unicorn'
class TeeInput < Unicorn::TeeInput
attr_accessor :tmp, :len
end
class TestTeeInput < Test::Unit::TestCase
def setup
@rd, @wr = Kgio::UNIXSocket.pair
@rd.sync = @wr.sync = true
@start_pid = $$
@rs = "\n"
$/ == "\n" or abort %q{test broken if \$/ != "\\n"}
end
def teardown
return if $$ != @start_pid
@rd.close rescue nil
@wr.close rescue nil
begin
Process.wait
rescue Errno::ECHILD
break
end while true
end
def check_tempfiles
tmp = @parser.env["rack.tempfiles"]
assert_instance_of Array, tmp
assert_operator tmp.size, :>=, 1
assert_instance_of Unicorn::TmpIO, tmp[0]
end
def test_gets_long
r = init_request("hello", 5 + (4096 * 4 * 3) + "#{@rs}foo#{@rs}".size)
ti = TeeInput.new(@rd, r)
status = line = nil
pid = fork {
@rd.close
3.times { @wr.write("ffff" * 4096) }
@wr.write "#{@rs}foo#{@rs}"
@wr.close
}
@wr.close
line = ti.gets
assert_equal(4096 * 4 * 3 + 5 + $/.size, line.size)
assert_equal("hello" << ("ffff" * 4096 * 3) << "#{@rs}", line)
line = ti.gets
assert_equal "foo#{@rs}", line
assert_nil ti.gets
pid, status = Process.waitpid2(pid)
assert status.success?
end
def test_gets_short
r = init_request("hello", 5 + "#{@rs}foo".size)
ti = TeeInput.new(@rd, r)
status = line = nil
pid = fork {
@rd.close
@wr.write "#{@rs}foo"
@wr.close
}
@wr.close
line = ti.gets
assert_equal("hello#{@rs}", line)
line = ti.gets
assert_equal "foo", line
assert_nil ti.gets
pid, status = Process.waitpid2(pid)
assert status.success?
end
def test_small_body
r = init_request('hello')
ti = TeeInput.new(@rd, r)
assert_equal 0, @parser.content_length
assert @parser.body_eof?
assert_equal StringIO, ti.tmp.class
assert_equal 0, ti.tmp.pos
assert_equal 5, ti.size
assert_equal 'hello', ti.read
assert_equal '', ti.read
assert_nil ti.read(4096)
assert_equal 5, ti.size
end
def test_read_with_buffer
r = init_request('hello')
ti = TeeInput.new(@rd, r)
buf = ''
rv = ti.read(4, buf)
assert_equal 'hell', rv
assert_equal 'hell', buf
assert_equal rv.object_id, buf.object_id
assert_equal 'o', ti.read
assert_equal nil, ti.read(5, buf)
assert_equal 0, ti.rewind
assert_equal 'hello', ti.read(5, buf)
assert_equal 'hello', buf
end
def test_big_body
r = init_request('.' * Unicorn::Const::MAX_BODY << 'a')
ti = TeeInput.new(@rd, r)
assert_equal 0, @parser.content_length
assert @parser.body_eof?
assert_kind_of File, ti.tmp
assert_equal 0, ti.tmp.pos
assert_equal Unicorn::Const::MAX_BODY + 1, ti.size
check_tempfiles
end
def test_read_in_full_if_content_length
a, b = 300, 3
r = init_request('.' * b, 300)
assert_equal 300, @parser.content_length
ti = TeeInput.new(@rd, r)
pid = fork {
@wr.write('.' * 197)
sleep 1 # still a *potential* race here that would make the test moot...
@wr.write('.' * 100)
}
assert_equal a, ti.read(a).size
_, status = Process.waitpid2(pid)
assert status.success?
@wr.close
end
def test_big_body_multi
r = init_request('.', Unicorn::Const::MAX_BODY + 1)
ti = TeeInput.new(@rd, r)
assert_equal Unicorn::Const::MAX_BODY, @parser.content_length
assert ! @parser.body_eof?
assert_kind_of File, ti.tmp
assert_equal 0, ti.tmp.pos
assert_equal Unicorn::Const::MAX_BODY + 1, ti.size
nr = Unicorn::Const::MAX_BODY / 4
pid = fork {
@rd.close
nr.times { @wr.write('....') }
@wr.close
}
@wr.close
assert_equal '.', ti.read(1)
assert_equal Unicorn::Const::MAX_BODY + 1, ti.size
nr.times { |x|
assert_equal '....', ti.read(4), "nr=#{x}"
assert_equal Unicorn::Const::MAX_BODY + 1, ti.size
}
assert_nil ti.read(1)
pid, status = Process.waitpid2(pid)
assert status.success?
check_tempfiles
end
def test_chunked
@parser = Unicorn::HttpParser.new
@parser.buf << "POST / HTTP/1.1\r\n" \
"Host: localhost\r\n" \
"Transfer-Encoding: chunked\r\n" \
"\r\n"
assert @parser.parse
assert_equal "", @parser.buf
pid = fork {
@rd.close
5.times { @wr.write("5\r\nabcde\r\n") }
@wr.write("0\r\n\r\n")
}
@wr.close
ti = TeeInput.new(@rd, @parser)
assert_nil @parser.content_length
assert_nil ti.len
assert ! @parser.body_eof?
assert_equal 25, ti.size
assert @parser.body_eof?
assert_equal 25, ti.len
assert_equal 0, ti.tmp.pos
ti.rewind
assert_equal 0, ti.tmp.pos
assert_equal 'abcdeabcdeabcdeabcde', ti.read(20)
assert_equal 20, ti.tmp.pos
ti.rewind
assert_equal 0, ti.tmp.pos
assert_kind_of File, ti.tmp
status = nil
pid, status = Process.waitpid2(pid)
assert status.success?
check_tempfiles
end
def test_chunked_ping_pong
@parser = Unicorn::HttpParser.new
buf = @parser.buf
buf << "POST / HTTP/1.1\r\n" \
"Host: localhost\r\n" \
"Transfer-Encoding: chunked\r\n" \
"\r\n"
assert @parser.parse
assert_equal "", buf
chunks = %w(aa bbb cccc dddd eeee)
rd, wr = IO.pipe
pid = fork {
chunks.each do |chunk|
rd.read(1) == "." and
@wr.write("#{'%x' % [ chunk.size]}\r\n#{chunk}\r\n")
end
@wr.write("0\r\n\r\n")
}
ti = TeeInput.new(@rd, @parser)
assert_nil @parser.content_length
assert_nil ti.len
assert ! @parser.body_eof?
chunks.each do |chunk|
wr.write('.')
assert_equal chunk, ti.read(16384)
end
_, status = Process.waitpid2(pid)
assert status.success?
end
def test_chunked_with_trailer
@parser = Unicorn::HttpParser.new
buf = @parser.buf
buf << "POST / HTTP/1.1\r\n" \
"Host: localhost\r\n" \
"Trailer: Hello\r\n" \
"Transfer-Encoding: chunked\r\n" \
"\r\n"
assert @parser.parse
assert_equal "", buf
pid = fork {
@rd.close
5.times { @wr.write("5\r\nabcde\r\n") }
@wr.write("0\r\n")
@wr.write("Hello: World\r\n\r\n")
}
@wr.close
ti = TeeInput.new(@rd, @parser)
assert_nil @parser.content_length
assert_nil ti.len
assert ! @parser.body_eof?
assert_equal 25, ti.size
assert_equal "World", @parser.env['HTTP_HELLO']
pid, status = Process.waitpid2(pid)
assert status.success?
end
def test_chunked_and_size_slow
@parser = Unicorn::HttpParser.new
buf = @parser.buf
buf << "POST / HTTP/1.1\r\n" \
"Host: localhost\r\n" \
"Trailer: Hello\r\n" \
"Transfer-Encoding: chunked\r\n" \
"\r\n"
assert @parser.parse
assert_equal "", buf
@wr.write("9\r\nabcde")
ti = TeeInput.new(@rd, @parser)
assert_nil @parser.content_length
assert_equal "abcde", ti.read(9)
assert ! @parser.body_eof?
@wr.write("fghi\r\n0\r\nHello: World\r\n\r\n")
assert_equal 9, ti.size
assert_equal "fghi", ti.read(9)
assert_equal nil, ti.read(9)
assert_equal "World", @parser.env['HTTP_HELLO']
end
def test_gets_read_mix
r = init_request("hello\nasdfasdf")
ti = Unicorn::TeeInput.new(@rd, r)
assert_equal "hello\n", ti.gets
assert_equal "asdfasdf", ti.read(9)
assert_nil ti.read(9)
end
private
def init_request(body, size = nil)
@parser = Unicorn::HttpParser.new
body = body.to_s.freeze
buf = @parser.buf
buf << "POST / HTTP/1.1\r\n" \
"Host: localhost\r\n" \
"Content-Length: #{size || body.size}\r\n" \
"\r\n#{body}"
assert @parser.parse
assert_equal body, buf
@buf = buf
@parser
end
end
|