1 |
use PApp::SQL; |
2 |
use Storable (); |
3 |
|
4 |
my $SD_VERSION = 1; |
5 |
|
6 |
my $ignore = qr/ ^(?:robots.txt$|\.) /x; |
7 |
|
8 |
sub conn::gen_statdata { |
9 |
my $self = shift; |
10 |
my $data; |
11 |
|
12 |
{ |
13 |
my $path = ""; |
14 |
my $prefix = ""; |
15 |
|
16 |
for ("http://".$self->server_hostport, split /\//, substr $self->{name}, 1) { |
17 |
next if $_ eq "."; |
18 |
$path .= "<a href='".escape_uri("$prefix$_")."/'>$_</a> / "; |
19 |
$prefix .= "$_/"; |
20 |
} |
21 |
$data->{path} = $path; |
22 |
} |
23 |
|
24 |
sub read_file { |
25 |
local ($/, *X); |
26 |
(open X, "<$_[0]\x00") ? <X> : (); |
27 |
} |
28 |
|
29 |
{ |
30 |
my $path = $self->{path}; |
31 |
do { |
32 |
$data->{top} ||= read_file "$path.dols/top"; |
33 |
$data->{bot} ||= read_file "$path.dols/bot"; |
34 |
$path =~ s/[^\/]*\/+$// |
35 |
or die "malformed path: $path"; |
36 |
} while $path ne ""; |
37 |
} |
38 |
|
39 |
local *DIR; |
40 |
if (opendir DIR, $self->{path}) { |
41 |
my $stat; |
42 |
|
43 |
my (@files, @dirs); |
44 |
my $dlen = 0; |
45 |
my $flen = 0; |
46 |
my $slen = 0; |
47 |
for (sort readdir DIR) { |
48 |
next if /$ignore/; |
49 |
stat "$self->{path}$_"; |
50 |
next unless -r _; |
51 |
if (-d _) { |
52 |
$dlen = length $_ if length $_ > $dlen; |
53 |
push @dirs, "$_/"; |
54 |
} else { |
55 |
my $s = -s _; |
56 |
$flen = length $_ if length $_ > $dlen; |
57 |
$slen = length $s if length $s > $dlen; |
58 |
push @files, [$_, $s]; |
59 |
} |
60 |
} |
61 |
if (@dirs) { |
62 |
$stat .= "<table><tr><th>Directories</th></tr>"; |
63 |
$dlen += 1; |
64 |
my $cols = int ((79 + $dlen) / $dlen); |
65 |
my $col = $cols; |
66 |
$cols = @dirs if @dirs < $cols; |
67 |
for (@dirs) { |
68 |
if (++$col >= $cols) { |
69 |
$stat .= "<tr>"; |
70 |
$col = 0; |
71 |
} |
72 |
$stat .= "<td><a href='".escape_uri($_)."'>$_</a> "; |
73 |
} |
74 |
$stat .= "</table>"; |
75 |
} |
76 |
if (@files) { |
77 |
$flen = $flen + 1 + $slen + 1 + 3; |
78 |
my $cols = int ((79 + $flen) / $flen); |
79 |
my $col = $cols; |
80 |
$cols = @files if @files < $cols; |
81 |
$stat .= "<table><tr>". ("<th align='left'>File<th>Size<th> " x $cols); |
82 |
for (@files) { |
83 |
if (++$col >= $cols) { |
84 |
$stat .= "<tr>"; |
85 |
$col = 0; |
86 |
} |
87 |
$stat .= "<td><a href='".escape_uri($_->[0])."'>$_->[0]</a><td align='right'>$_->[1]<td> "; |
88 |
} |
89 |
$stat .= "</table>"; |
90 |
} |
91 |
$data->{stat} = $stat; |
92 |
} else { |
93 |
$data->{stat} = "Unable to index $uri: $!<br>"; |
94 |
} |
95 |
|
96 |
$data; |
97 |
} |
98 |
|
99 |
use Tie::Cache; |
100 |
tie %statdata_cache, Tie::Cache::, 70; |
101 |
|
102 |
sub conn::get_statdata { |
103 |
my $self = shift; |
104 |
|
105 |
my $mtime = $self->{stat}[9]; |
106 |
|
107 |
my $statdata = \$statdata_cache{$self->{path}, $mtime}; |
108 |
|
109 |
return $$statdata if $$statdata; |
110 |
|
111 |
my $st = sql_exec $statdata, |
112 |
"select statdata from diridx where mtime = ? and path = ?", |
113 |
$mtime, $self->{path}; |
114 |
|
115 |
if ($st->fetch) { |
116 |
$$statdata = Storable::thaw $$statdata; |
117 |
return $$statdata if $$statdata->{version} == $SD_VERSION; |
118 |
} |
119 |
|
120 |
$self->slog(8, "creating index cache for $self->{path}"); |
121 |
|
122 |
$$statdata = $self->gen_statdata; |
123 |
$$statdata->{version} = $SD_VERSION; |
124 |
|
125 |
sql_exec "delete from diridx where path = ?", $self->{path}; |
126 |
sql_exec "insert into diridx (path, mtime, statdata) values (?, ?, ?)", |
127 |
$self->{path}, $mtime, Storable::freeze $$statdata; |
128 |
|
129 |
$$statdata; |
130 |
} |
131 |
|
132 |
sub conn::diridx { |
133 |
my $self = shift; |
134 |
|
135 |
my $data = $self->get_statdata; |
136 |
|
137 |
my $uptime = int (time - $::starttime); |
138 |
$uptime = sprintf "%02dd %02d:%02d", |
139 |
int ($uptime / (60 * 60 * 24)), |
140 |
int ($uptime / (60 * 60)) % 24, |
141 |
int ($uptime / 60) % 60; |
142 |
|
143 |
<<EOF; |
144 |
<html> |
145 |
<head><title>$self->{uri}</title></head> |
146 |
<body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#000080" alink="#ff0000"> |
147 |
<h1>$data->{path}</h1> |
148 |
$data->{top} |
149 |
<small><div align="right"><tt>$self->{remote_id}/$self->{country} - $::conns connection(s) - uptime $uptime - myhttpd/$VERSION</tt></div></small> |
150 |
<hr> |
151 |
$data->{stat} |
152 |
$data->{bot} |
153 |
</body> |
154 |
</html> |
155 |
EOF |
156 |
} |
157 |
|
158 |
sub handle_redirect { # unused |
159 |
if (-f ".redirect") { |
160 |
if (open R, "<.redirect") { |
161 |
while (<R>) { |
162 |
if (/^(?:$host$port)$uri([^ \tr\n]*)[ \t\r\n]+(.*)$/) { |
163 |
my $rem = $1; |
164 |
my $url = $2; |
165 |
print $nph ? "HTTP/1.0 302 Moved\n" : "Status: 302 Moved\n"; |
166 |
print <<EOF; |
167 |
Location: $url |
168 |
Content-Type: text/html |
169 |
|
170 |
<html> |
171 |
<head><title>Page Redirection to $url</title></head> |
172 |
<meta http-equiv="refresh" content="0;URL=$url"> |
173 |
</head> |
174 |
<body text="black" link="#1010C0" vlink="#101080" alink="red" bgcolor="white"> |
175 |
<large> |
176 |
This page has moved to $url.<br /> |
177 |
<a href="$url"> |
178 |
The automatic redirection has failed. Please try a <i>slightly</i> |
179 |
newer browser next time, and in the meantime <i>please</i> follow this link ;) |
180 |
</a> |
181 |
</large> |
182 |
</body> |
183 |
</html> |
184 |
EOF |
185 |
} |
186 |
} |
187 |
} |
188 |
} |
189 |
} |
190 |
|
191 |
1; |