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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
| | # Copyright (C) 2013-2016 all contributors <yahns-public@yhbt.net>
# License: GPL-3.0+ (https://www.gnu.org/licenses/gpl-3.0.txt)
# frozen_string_literal: true
require_relative 'server_helper'
require 'digest/md5'
require 'rack/file'
class TestOutputBuffering < Testcase
ENV["N"].to_i > 1 and parallelize_me!
include ServerHelper
alias setup server_helper_setup
alias teardown server_helper_teardown
GPLv3 = File.read("COPYING")
RAND = IO.binread("/dev/urandom", 666) * 119
dig = Digest::MD5.new
NR = 1337
MD5 = Thread.new do
NR.times { dig << RAND }
dig.hexdigest
end
class BigBody
def each
NR.times { yield RAND }
end
end
def test_output_buffer_false_curl
output_buffer(false, :curl)
end
def test_output_buffer_false_http09
output_buffer(false, :http09)
end
def test_output_buffer_true_curl
output_buffer(true, :curl)
end
def test_output_buffer_true_http09
output_buffer(true, :http09)
end
def output_buffer(btype, check_type)
err = @err
cfg = Yahns::Config.new
host, port = @srv.addr[3], @srv.addr[1]
len = (RAND.size * NR).to_s
cfg.instance_eval do
ru = lambda do |e|
[ 200, {'Content-Length'=>len}, BigBody.new ]
end
GTL.synchronize do
app(:rack, ru) do
listen "#{host}:#{port}"
output_buffering btype
end
end
logger(Logger.new(err.path))
end
pid = mkserver(cfg)
case check_type
when :curl
# curl is faster for piping gigantic wads of data than Net::HTTP,
# and we need to be able to throttle a bit to force output buffering
c = IO.popen("curl -N -sSf http://#{host}:#{port}/")
wait_for_full(c)
dig, nr = md5sum(c)
c.close
assert_equal MD5.value, dig.hexdigest
when :http09
# HTTP/0.9
c = get_tcp_client(host, port)
c.write("GET /\r\n\r\n")
wait_for_full(c)
dig, nr = md5sum(c)
assert_equal(NR * RAND.size, nr)
c.shutdown
c.close
assert_equal MD5.value, dig.hexdigest
else
raise "TESTBUG"
end
ensure
quit_wait(pid)
end
def md5sum(c)
dig = Digest::MD5.new
buf = ''.dup
nr = 0
while c.read(8192, buf)
dig << buf
nr += buf.bytesize
end
[ dig, nr ]
end
class BigHeader
A = "A" * 8192
def initialize(h)
@h = h
end
def each
NR.times do |n|
yield("X-#{n}", A)
end
@h.each { |k,v| yield(k,v) }
end
end
def test_big_header
err = @err
cfg = Yahns::Config.new
host, port = @srv.addr[3], @srv.addr[1]
cfg.instance_eval do
ru = lambda do |e|
case e["PATH_INFO"]
when "/COPYING"
Rack::File.new(Dir.pwd).call(e)
gplv3 = File.open("COPYING")
def gplv3.each
raise "SHOULD NOT BE CALLED"
end
size = gplv3.size
len = size.to_s
ranges = Rack::Utils.respond_to?(:get_byte_ranges) ?
Rack::Utils.get_byte_ranges(e['HTTP_RANGE'], size) :
Rack::Utils.byte_ranges(e, size)
status = 200
h = { "Content-Type" => "text/plain", "Content-Length" => len }
if ranges && ranges.size == 1
status = 206
range = ranges[0]
h["Content-Range"] = "bytes #{range.begin}-#{range.end}/#{size}"
size = range.end - range.begin + 1
len.replace(size.to_s)
end
[ status , BigHeader.new(h), gplv3 ]
when "/"
h = { "Content-Type" => "text/plain", "Content-Length" => "4" }
[ 200, BigHeader.new(h), ["BIG\n"] ]
else
raise "WTF"
end
end
GTL.synchronize do
app(:rack, ru) do
listen "#{host}:#{port}"
end
end
logger(Logger.new(err.path))
end
pid = mkserver(cfg)
threads = []
# start with just a big header
threads << Thread.new do
c = get_tcp_client(host, port)
c.write "GET / HTTP/1.0\r\n\r\n"
wait_for_full(c)
nr = 0
last = nil
c.each_line do |line|
case line
when %r{\AX-} then nr += 1
else
last = line
end
end
assert_equal NR, nr
assert_equal "BIG\n", last
c.close
end
threads << Thread.new do
c = get_tcp_client(host, port)
c.write "GET /COPYING HTTP/1.0\r\n\r\n"
wait_for_full(c)
nr = 0
c.each_line do |line|
case line
when %r{\AX-} then nr += 1
else
break if line == "\r\n"
end
end
assert_equal NR, nr
assert_equal GPLv3, c.read
c.close
end
threads << Thread.new do
c = get_tcp_client(host, port)
c.write "GET /COPYING HTTP/1.0\r\nRange: bytes=5-46\r\n\r\n"
wait_for_full(c)
nr = 0
c.each_line do |line|
case line
when %r{\AX-} then nr += 1
else
break if line == "\r\n"
end
end
assert_equal NR, nr
assert_equal GPLv3[5..46], c.read
c.close
end
threads.each do |t|
assert_equal t, t.join(30)
assert_nil t.value
end
ensure
quit_wait(pid)
end
def test_client_timeout
err = @err
apperr = tmpfile(%w(app .err))
cfg = Yahns::Config.new
size = RAND.size * NR
host, port = @srv.addr[3], @srv.addr[1]
re = /timeout on :wait_writable after 0\.1s$/
cfg.instance_eval do
ru = lambda do |e|
if e["PATH_INFO"] == "/bh"
h = { "Content-Type" => "text/plain", "Content-Length" => "4" }
[ 200, BigHeader.new(h), ["BIG\n"] ]
else
[ 200, {'Content-Length' => size.to_s }, BigBody.new ]
end
end
GTL.synchronize do
app(:rack, ru) do
listen "#{host}:#{port}"
output_buffering false
client_timeout 0.1
logger(Logger.new(apperr.path))
end
end
logger(Logger.new(err.path))
end
pid = mkserver(cfg)
wait_for_timeout_msg = lambda do
Timeout.timeout(6) do
sleep(0.01) until File.readlines(apperr.path).grep(re).size == 2
end
end
threads = []
threads << Thread.new do
c = get_tcp_client(host, port)
c.write("GET / HTTP/1.1\r\nHost: example.com\r\n\r\n")
wait_for_full(c)
wait_for_timeout_msg.call
assert_operator c.nread, :>, 0
c
end
threads << Thread.new do
c = get_tcp_client(host, port)
c.write("GET /bh HTTP/1.1\r\nHost: example.com\r\n\r\n")
wait_for_full(c)
wait_for_timeout_msg.call
assert_operator c.nread, :>, 0
c
end
threads.each { |t| t.join(10) }
assert_operator size, :>, threads[0].value.read.size
assert_operator size, :>, threads[1].value.read.size
msg = File.readlines(apperr.path)
msg = msg.grep(re)
assert_equal 2, msg.size
threads.each { |t| t.value.close }
ensure
apperr.close! if apperr
quit_wait(pid)
end
def test_hijacked
err, cfg, host, port = @err, Yahns::Config.new, @srv.addr[3], @srv.addr[1]
cfg.instance_eval do
ru = lambda do |e|
h = {
"Content-Type" => "text/plain",
"Content-Length" => "4",
"rack.hijack" => proc { |x| x.write("HIHI"); x.close },
}
body = Object.new
def body.each; abort "body#each should not be used"; end
[ 200, BigHeader.new(h), body ]
end
GTL.synchronize do
app(:rack, ru) do
listen "#{host}:#{port}"
end
end
stderr_path err.path
end
pid = mkserver(cfg)
c = get_tcp_client(host, port)
c.write "GET / HTTP/1.0\r\n\r\n"
wait_for_full(c)
nr = 0
last = nil
c.each_line do |line|
case line
when %r{\AX-} then nr += 1
else
last = line
end
end
assert_equal NR, nr
assert_equal "HIHI", last
c.close
ensure
quit_wait(pid)
end
end if `which curl 2>/dev/null`.strip =~ /curl/
|