1 |
root |
1.12 |
use List::Util qw(sum); |
2 |
|
|
|
3 |
root |
1.1 |
use Storable (); |
4 |
|
|
|
5 |
|
|
my $SD_VERSION = 1; |
6 |
|
|
|
7 |
|
|
my $ignore = qr/ ^(?:robots.txt$|\.) /x; |
8 |
|
|
|
9 |
root |
1.9 |
our %diridx; |
10 |
|
|
|
11 |
root |
1.10 |
if ($db_env) { |
12 |
|
|
tie %diridx, BerkeleyDB::Hash, |
13 |
|
|
-Env => $db_env, |
14 |
|
|
-Filename => "directory", |
15 |
|
|
-Flags => DB_CREATE, |
16 |
|
|
or die "unable to create database index"; |
17 |
root |
1.9 |
} |
18 |
|
|
|
19 |
root |
1.1 |
sub conn::gen_statdata { |
20 |
|
|
my $self = shift; |
21 |
|
|
my $data; |
22 |
|
|
|
23 |
|
|
{ |
24 |
|
|
my $path = ""; |
25 |
|
|
my $prefix = ""; |
26 |
|
|
|
27 |
|
|
for ("http://".$self->server_hostport, split /\//, substr $self->{name}, 1) { |
28 |
|
|
next if $_ eq "."; |
29 |
root |
1.7 |
$path .= "<a href='".escape_uri("$prefix$_")."/'>$_</a> / "; |
30 |
root |
1.1 |
$prefix .= "$_/"; |
31 |
|
|
} |
32 |
|
|
$data->{path} = $path; |
33 |
|
|
} |
34 |
|
|
|
35 |
|
|
sub read_file { |
36 |
|
|
local ($/, *X); |
37 |
|
|
(open X, "<$_[0]\x00") ? <X> : (); |
38 |
|
|
} |
39 |
|
|
|
40 |
|
|
{ |
41 |
|
|
my $path = $self->{path}; |
42 |
|
|
do { |
43 |
|
|
$data->{top} ||= read_file "$path.dols/top"; |
44 |
|
|
$data->{bot} ||= read_file "$path.dols/bot"; |
45 |
|
|
$path =~ s/[^\/]*\/+$// |
46 |
|
|
or die "malformed path: $path"; |
47 |
|
|
} while $path ne ""; |
48 |
|
|
} |
49 |
|
|
|
50 |
|
|
local *DIR; |
51 |
|
|
if (opendir DIR, $self->{path}) { |
52 |
|
|
my $dlen = 0; |
53 |
|
|
my $flen = 0; |
54 |
|
|
my $slen = 0; |
55 |
|
|
for (sort readdir DIR) { |
56 |
|
|
next if /$ignore/; |
57 |
|
|
stat "$self->{path}$_"; |
58 |
|
|
if (-d _) { |
59 |
root |
1.15 |
next unless 0555 == ((stat _)[2] & 0555); |
60 |
root |
1.1 |
$dlen = length $_ if length $_ > $dlen; |
61 |
root |
1.9 |
push @{$data->{d}}, $_; |
62 |
root |
1.1 |
} else { |
63 |
root |
1.15 |
next unless 0444 == ((stat _)[2] & 0444); |
64 |
root |
1.1 |
my $s = -s _; |
65 |
|
|
$flen = length $_ if length $_ > $dlen; |
66 |
|
|
$slen = length $s if length $s > $dlen; |
67 |
root |
1.9 |
push @{$data->{f}}, [$_, $s]; |
68 |
root |
1.1 |
} |
69 |
|
|
} |
70 |
root |
1.9 |
$data->{dlen} = $dlen; |
71 |
|
|
$data->{flen} = $flen; |
72 |
|
|
$data->{slen} = $slen; |
73 |
root |
1.1 |
} |
74 |
|
|
|
75 |
|
|
$data; |
76 |
|
|
} |
77 |
|
|
|
78 |
|
|
sub conn::get_statdata { |
79 |
|
|
my $self = shift; |
80 |
|
|
|
81 |
|
|
my $mtime = $self->{stat}[9]; |
82 |
|
|
|
83 |
root |
1.9 |
$statdata = $diridx{$self->{path}}; |
84 |
root |
1.6 |
|
85 |
root |
1.9 |
if (defined $statdata) { |
86 |
|
|
$$statdata = Storable::thaw $statdata; |
87 |
|
|
return $$statdata |
88 |
|
|
if $$statdata->{version} == $SD_VERSION |
89 |
|
|
&& $$statdata->{mtime} == $mtime; |
90 |
root |
1.1 |
} |
91 |
|
|
|
92 |
|
|
$self->slog(8, "creating index cache for $self->{path}"); |
93 |
|
|
|
94 |
root |
1.6 |
$$statdata = $self->gen_statdata; |
95 |
|
|
$$statdata->{version} = $SD_VERSION; |
96 |
root |
1.9 |
$$statdata->{mtime} = $mtime; |
97 |
root |
1.1 |
|
98 |
root |
1.9 |
$diridx{$self->{path}} = Storable::freeze $$statdata; |
99 |
root |
1.11 |
(tied %diridx)->db_sync; |
100 |
root |
1.1 |
|
101 |
root |
1.6 |
$$statdata; |
102 |
root |
1.1 |
} |
103 |
|
|
|
104 |
root |
1.16 |
sub handle_redirect { # unused |
105 |
|
|
if (-f ".redirect") { |
106 |
|
|
if (open R, "<.redirect") { |
107 |
|
|
while (<R>) { |
108 |
|
|
if (/^(?:$host$port)$uri([^ \tr\n]*)[ \t\r\n]+(.*)$/) { |
109 |
|
|
my $rem = $1; |
110 |
|
|
my $url = $2; |
111 |
|
|
print $nph ? "HTTP/1.0 302 Moved\n" : "Status: 302 Moved\n"; |
112 |
|
|
print <<EOF; |
113 |
|
|
Location: $url |
114 |
|
|
Content-Type: text/html |
115 |
|
|
|
116 |
|
|
<html> |
117 |
|
|
<head><title>Page Redirection to $url</title></head> |
118 |
|
|
<meta http-equiv="refresh" content="0;URL=$url"> |
119 |
|
|
</head> |
120 |
|
|
<body text="black" link="#1010C0" vlink="#101080" alink="red" bgcolor="white"> |
121 |
|
|
<large> |
122 |
|
|
This page has moved to $url.<br /> |
123 |
|
|
<a href="$url"> |
124 |
|
|
The automatic redirection has failed. Please try a <i>slightly</i> |
125 |
|
|
newer browser next time, and in the meantime <i>please</i> follow this link ;) |
126 |
|
|
</a> |
127 |
|
|
</large> |
128 |
|
|
</body> |
129 |
|
|
</html> |
130 |
|
|
EOF |
131 |
|
|
} |
132 |
|
|
} |
133 |
|
|
} |
134 |
|
|
} |
135 |
|
|
} |
136 |
|
|
|
137 |
|
|
sub format_time { |
138 |
|
|
sprintf "%02dd %02d:%02d:%02d", |
139 |
|
|
int ($_[0] / (60 * 60 * 24)), |
140 |
|
|
int ($_[0] / (60 * 60)) % 24, |
141 |
|
|
int ($_[0] / 60) % 60, |
142 |
|
|
int ($_[0]) % 60; |
143 |
|
|
} |
144 |
|
|
|
145 |
root |
1.1 |
sub conn::diridx { |
146 |
|
|
my $self = shift; |
147 |
|
|
|
148 |
|
|
my $data = $self->get_statdata; |
149 |
root |
1.5 |
|
150 |
root |
1.9 |
my $stat; |
151 |
|
|
if ($data->{dlen}) { |
152 |
|
|
$stat .= "<table><tr><th>Directories</th></tr>"; |
153 |
|
|
$data->{dlen} += 1; |
154 |
|
|
my $cols = int ((79 + $data->{dlen}) / $data->{dlen}); |
155 |
|
|
$cols = @{$data->{d}} if @{$data->{d}} < $cols; |
156 |
|
|
my $col = $cols; |
157 |
|
|
for (@{$data->{d}}) { |
158 |
|
|
if (++$col >= $cols) { |
159 |
|
|
$stat .= "<tr>"; |
160 |
|
|
$col = 0; |
161 |
|
|
} |
162 |
|
|
if ("$self->{path}$_" =~ $conn::blockuri{$self->{country}}) { |
163 |
|
|
$stat .= "<td>$_ "; |
164 |
|
|
} else { |
165 |
|
|
$stat .= "<td><a href='".escape_uri("$_/")."'>$_</a> "; |
166 |
|
|
} |
167 |
|
|
} |
168 |
|
|
$stat .= "</table>"; |
169 |
|
|
} |
170 |
|
|
if ($data->{flen}) { |
171 |
|
|
$data->{flen} += 1 + $data->{slen} + 1 + 3; |
172 |
|
|
my $cols = int ((79 + $data->{flen}) / $data->{flen}); |
173 |
|
|
$cols = @{$data->{f}} if @{$data->{f}} < $cols; |
174 |
|
|
my $col = $cols; |
175 |
|
|
$stat .= "<table><tr>". ("<th align='left'>File<th>Size<th> " x $cols); |
176 |
|
|
for (@{$data->{f}}) { |
177 |
|
|
if (++$col >= $cols) { |
178 |
|
|
$stat .= "<tr>"; |
179 |
|
|
$col = 0; |
180 |
|
|
} |
181 |
|
|
$stat .= "<td><a href='".escape_uri($_->[0])."'>$_->[0]</a><td align='right'>$_->[1]<td> "; |
182 |
|
|
} |
183 |
|
|
$stat .= "</table>"; |
184 |
|
|
} |
185 |
|
|
|
186 |
root |
1.1 |
<<EOF; |
187 |
|
|
<html> |
188 |
|
|
<head><title>$self->{uri}</title></head> |
189 |
|
|
<body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#000080" alink="#ff0000"> |
190 |
|
|
<h1>$data->{path}</h1> |
191 |
|
|
$data->{top} |
192 |
root |
1.13 |
<hr /> |
193 |
root |
1.16 |
<a href="/internal/status">Server Status Page & Queueing Info</a> |
194 |
root |
1.12 |
<hr /> |
195 |
root |
1.9 |
$stat |
196 |
root |
1.1 |
$data->{bot} |
197 |
|
|
</body> |
198 |
|
|
</html> |
199 |
|
|
EOF |
200 |
|
|
} |
201 |
|
|
|
202 |
root |
1.16 |
$::internal{status} = sub { |
203 |
|
|
my $self = shift; |
204 |
root |
1.1 |
|
205 |
root |
1.16 |
my $uptime = format_time ($::NOW - $::starttime); |
206 |
|
|
|
207 |
|
|
my $content = <<EOF; |
208 |
root |
1.1 |
<html> |
209 |
root |
1.16 |
<head><title>Server Status Page</title></head> |
210 |
|
|
<body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#000080" alink="#ff0000"> |
211 |
|
|
<h1>Server Status Page</h1> |
212 |
|
|
<h2>Myhttpd</h2> |
213 |
root |
1.20 |
version <b>$VERSION</b>; current/max connection count: <b>$::conns</b>:<b>$::maxconns</b>; uptime: <b>$uptime</b>;<br /> |
214 |
root |
1.17 |
client-id <b>$self->{remote_id}</b>, client country <b>$self->{country}</b>;<br /> |
215 |
root |
1.16 |
<h2>Queue Statistics</h2> |
216 |
|
|
<ul> |
217 |
root |
1.1 |
EOF |
218 |
root |
1.16 |
|
219 |
root |
1.19 |
for ( |
220 |
|
|
["small files queue", $queue_small], |
221 |
|
|
["large files queue", $queue_large], |
222 |
|
|
["misc files queue" , $queue_index], |
223 |
|
|
) { |
224 |
|
|
my ($name, $queue) = @$_; |
225 |
|
|
if ($queue->waiters) { |
226 |
|
|
if (0) { |
227 |
|
|
$content .= "<li>$name<table border='1' width='100%'><tr><th>Remote ID</th><th>CN</th><th>Waiting</th><th>URI</th></tr>"; |
228 |
|
|
for ($queue->waiters) { |
229 |
|
|
if (defined $queue) { |
230 |
|
|
my $conn = $queue->{conn}; |
231 |
|
|
my $time = format_time ($::NOW - $conn->{time}); |
232 |
|
|
$content .= "<tr>". |
233 |
|
|
"<td>$conn->{remote_id}</td>". |
234 |
|
|
"<td>$conn->{country}</td>". |
235 |
|
|
"<td>$time</td>". |
236 |
|
|
"<td>".escape_html($conn->{name})."</td>". |
237 |
|
|
"</tr>"; |
238 |
|
|
} else { |
239 |
|
|
$content .= "<tr><td colspan='4'>premature ejaculation</td></tr>"; |
240 |
|
|
} |
241 |
root |
1.18 |
} |
242 |
root |
1.19 |
$content .= "</table></li>"; |
243 |
|
|
} else { |
244 |
|
|
my @waiters = grep defined $_, $queue->waiters; |
245 |
|
|
$content .= "<li>$name<br />(".(scalar @waiters). |
246 |
|
|
" client(s), waiting since " |
247 |
|
|
.(format_time $::NOW - ($waiters[0]{conn}{time} || $::NOW)). |
248 |
|
|
")</li>"; |
249 |
root |
1.1 |
} |
250 |
root |
1.16 |
} else { |
251 |
|
|
$content .= "<li>$name<br />(empty)</li>"; |
252 |
root |
1.1 |
} |
253 |
|
|
} |
254 |
root |
1.16 |
|
255 |
|
|
$content .= <<EOF; |
256 |
|
|
</ul> |
257 |
|
|
</body> |
258 |
|
|
</html> |
259 |
|
|
EOF |
260 |
|
|
|
261 |
|
|
$self->response(200, "ok", |
262 |
|
|
{ |
263 |
|
|
"Content-Type" => "text/html", |
264 |
|
|
"Content-Length" => length $content, |
265 |
|
|
}, |
266 |
|
|
$content); |
267 |
|
|
}; |
268 |
root |
1.1 |
|
269 |
|
|
1; |