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