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
| | # Copyright (C) 2013, Eric Wong <normalperson@yhbt.net> and all contributors
# License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt)
require_relative 'server_helper'
require 'rack/file'
class TestServeStatic < Testcase
ENV["N"].to_i > 1 and parallelize_me!
include ServerHelper
alias setup server_helper_setup
alias teardown server_helper_teardown
def test_serve_static
tmpdir = Dir.mktmpdir
sock = "#{tmpdir}/sock"
err = @err
cfg = Yahns::Config.new
host, port = @srv.addr[3], @srv.addr[1]
cfg.instance_eval do
GTL.synchronize do
app(:rack, Rack::File.new(Dir.pwd)) {
listen sock
listen "#{host}:#{port}"
}
end
logger(Logger.new(err.path))
end
pid = mkserver(cfg)
gplv3 = File.read("COPYING")
Net::HTTP.start(host, port) do |http|
res = http.request(Net::HTTP::Get.new("/COPYING"))
assert_equal gplv3, res.body
req = Net::HTTP::Get.new("/COPYING", "Range" => "bytes=5-46")
res = http.request(req)
assert_match %r{bytes 5-46/\d+\z}, res["Content-Range"]
assert_equal gplv3[5..46], res.body
end
# ensure sendfile works on Unix sockets
s = UNIXSocket.new(sock)
s.close_on_exec = true
s.write "GET /COPYING\r\n\r\n"
assert_equal gplv3, Timeout.timeout(30) { s.read }
s.close
ensure
quit_wait(pid)
FileUtils.rm_rf tmpdir
end
def test_serve_static_blocked_header
err = @err
cfg = Yahns::Config.new
host, port = @srv.addr[3], @srv.addr[1]
cfg.instance_eval do
GTL.synchronize do
app(:rack, Rack::File.new(Dir.pwd)) { listen "#{host}:#{port}" }
end
logger(Logger.new(err.path))
end
pid = mkserver(cfg) do
$_tw_blocked = 0
$_tw_block_on = [1]
Yahns::HttpClient.__send__(:include, TrywriteBlocked)
end
gplv3 = File.read("COPYING")
Net::HTTP.start(host, port) do |http|
res = http.request(Net::HTTP::Get.new("/COPYING"))
assert_equal gplv3, res.body
req = Net::HTTP::Get.new("/COPYING", "Range" => "bytes=5-46")
res = http.request(req)
assert_equal gplv3[5..46], res.body
end
ensure
quit_wait(pid)
end
def mksparse(tmpdir)
sparse = "#{tmpdir}/sparse"
off = 100 * 1024 * 1024
File.open(sparse, "w") do |fp|
fp.sysseek(off)
fp.syswrite '.'
end
[ off + 1, sparse ]
end
def test_truncated_sendfile
tmpdir = Dir.mktmpdir
size, sparse = mksparse(tmpdir)
err, cfg, host, port = @err, Yahns::Config.new, @srv.addr[3], @srv.addr[1]
pid = mkserver(cfg) do
cfg.instance_eval do
app(:rack, Rack::File.new(tmpdir)) { listen "#{host}:#{port}" }
stderr_path err.path
end
end
c = get_tcp_client(host, port)
c.write "GET /sparse HTTP/1.1\r\nHost: example.com\r\n\r\n"
wait_for_full(c)
File.truncate(sparse, 5)
buf = Timeout.timeout(60) { c.read }
c.close
assert_operator buf.size, :<, size
ensure
quit_wait(pid)
FileUtils.rm_rf(tmpdir)
end
def test_expanded_sendfile
tmpdir = Dir.mktmpdir
size, sparse = mksparse(tmpdir)
err, cfg, host, port = @err, Yahns::Config.new, @srv.addr[3], @srv.addr[1]
pid = mkserver(cfg) do
cfg.instance_eval do
app(:rack, Rack::File.new(tmpdir)) { listen "#{host}:#{port}" }
stderr_path err.path
end
end
c = get_tcp_client(host, port)
c.write "GET /sparse\r\n\r\n"
wait_for_full(c)
File.open(sparse, "w") do |fp|
fp.sysseek(size * 2)
fp.syswrite '.'
end
Timeout.timeout(60) do
bytes = IO.copy_stream(c, "/dev/null")
assert_equal bytes, size
assert_raises(EOFError) { c.readpartial 1 }
end
c.close
ensure
quit_wait(pid)
FileUtils.rm_rf(tmpdir)
end
end
|