ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/Coro/myhttpd/diridx.pl
Revision: 1.37
Committed: Sun Jan 24 14:19:28 2010 UTC (14 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.36: +1 -1 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 use List::Util qw(sum);
2
3 use Coro::AIO ();
4 use Storable ();
5
6 my $SD_VERSION = 1;
7
8 my $ignore = qr/ ^(?:robots.txt$|\.) /x;
9
10 our %diridx;
11
12 if ($db_env) {
13 tie %diridx, BerkeleyDB::Hash,
14 -Env => $db_env,
15 -Filename => "directory",
16 -Flags => DB_CREATE,
17 or die "unable to create database index";
18 }
19
20 sub conn::gen_statdata {
21 my $self = shift;
22 my $data;
23
24 {
25 my $path = "";
26 my $prefix = "";
27
28 for ("http://".$self->server_hostport, split /\//, substr $self->{name}, 1) {
29 next if $_ eq ".";
30 $path .= "<a href='".escape_uri("$prefix$_")."/'>$_</a> / ";
31 $prefix .= "$_/";
32 }
33 $data->{path} = $path;
34 }
35
36 {
37 my $path = $self->{path};
38 do {
39 Coro::AIO::aio_load "$path.dols/top", $data->{top}
40 unless Coro::AIO::aio_stat "$path.dols/top";
41 Coro::AIO::aio_load "$path.dols/bot", $data->{bot}
42 unless Coro::AIO::aio_stat "$path.dols/bot";
43 $path =~ s/[^\/]*\/+$//
44 or die "malformed path: $path";
45 } while $path ne "";
46 }
47
48 my $entries = Coro::AIO::aio_readdir $self->{path};
49
50 {
51 my $dlen = 0;
52 my $flen = 0;
53 my $slen = 0;
54
55 for (sort @$entries) {
56 next if /$ignore/;
57
58 Coro::AIO::aio_stat "$self->{path}$_";
59 if (-d _) {
60 next unless 0555 == ((stat _)[2] & 0555);
61 $dlen = length $_ if length $_ > $dlen;
62 push @{$data->{d}}, $_;
63 } else {
64 next unless 0444 == ((stat _)[2] & 0444);
65 my $s = -s _;
66 $flen = length $_ if length $_ > $dlen;
67 $slen = length $s if length $s > $dlen;
68 push @{$data->{f}}, [$_, $s];
69 }
70 }
71 $data->{dlen} = $dlen;
72 $data->{flen} = $flen;
73 $data->{slen} = $slen;
74 }
75
76 $data
77 }
78
79 sub conn::get_statdata {
80 my $self = shift;
81
82 my $mtime = $self->{stat}[9];
83 my $statdata;
84
85 # $statdata = $diridx{$self->{path}};
86 #
87 # if (defined $statdata) {
88 # $$statdata = Storable::thaw $statdata;
89 # return $$statdata
90 # if $$statdata->{version} == $SD_VERSION
91 # && $$statdata->{mtime} == $mtime;
92 # }
93
94 # $self->slog(8, "creating index cache for $self->{path}");
95
96 $$statdata = $self->gen_statdata;
97 $$statdata->{version} = $SD_VERSION;
98 $$statdata->{mtime} = $mtime;
99
100 # $diridx{$self->{path}} = Storable::freeze $$statdata;
101 # (tied %diridx)->db_sync;
102
103 $$statdata
104 }
105
106 sub handle_redirect { # unused
107 if (-f ".redirect") {
108 if (open my $fh, "<.redirect") {
109 while (<$fh>) {
110 if (/^(?:$host$port)$uri([^ \tr\n]*)[ \t\r\n]+(.*)$/) {
111 my $rem = $1;
112 my $url = $2;
113 print $nph ? "HTTP/1.0 302 Moved\n" : "Status: 302 Moved\n";
114 print <<EOF;
115 Location: $url
116 Content-Type: text/html
117
118 <html>
119 <head><title>Page Redirection to $url</title></head>
120 <meta http-equiv="refresh" content="0;URL=$url">
121 </head>
122 <body text="black" link="#1010C0" vlink="#101080" alink="red" bgcolor="white">
123 <large>
124 This page has moved to $url.<br />
125 <a href="$url">
126 The automatic redirection has failed. Please try a <i>slightly</i>
127 newer browser next time, and in the meantime <i>please</i> follow this link ;)
128 </a>
129 </large>
130 </body>
131 </html>
132 EOF
133 }
134 }
135 }
136 }
137 }
138
139 sub format_time {
140 if ($_[0] < 0) {
141 "--:--:--";
142 } elsif ($_[0] >= 60*60*24) {
143 sprintf "%dd&#160;%02d:%02d:%02d",
144 int ($_[0] / (60 * 60 * 24)),
145 int ($_[0] / (60 * 60)) % 24,
146 int ($_[0] / 60) % 60,
147 int ($_[0]) % 60;
148 } else {
149 sprintf "%02d:%02d:%02d",
150 int ($_[0] / (60 * 60)) % 24,
151 int ($_[0] / 60) % 60,
152 int ($_[0]) % 60;
153 }
154 }
155
156 sub conn::diridx {
157 my $self = shift;
158
159 my $data = $self->get_statdata;
160
161 my $stat;
162 if ($data->{dlen}) {
163 $stat .= "<table><tr><th>Directories</th></tr>";
164 $data->{dlen} += 1;
165 my $cols = int ((79 + $data->{dlen}) / $data->{dlen});
166 $cols = @{$data->{d}} if @{$data->{d}} < $cols;
167 my $col = $cols;
168 for (@{$data->{d}}) {
169 if (++$col >= $cols) {
170 $stat .= "<tr>";
171 $col = 0;
172 }
173 if ("$self->{path}$_" =~ $conn::blockuri{$self->{country}}) {
174 $stat .= "<td>$_ ";
175 } else {
176 $stat .= "<td><a href='".escape_uri("$_/")."'>$_</a> ";
177 }
178 }
179 $stat .= "</table>";
180 }
181 if ($data->{flen}) {
182 $data->{flen} += 1 + $data->{slen} + 1 + 3;
183 my $cols = int ((79 + $data->{flen}) / $data->{flen});
184 $cols = @{$data->{f}} if @{$data->{f}} < $cols;
185 my $col = $cols;
186 $stat .= "<table><tr>". ("<th align='left'>File<th>Size<th>&nbsp;" x $cols);
187 for (@{$data->{f}}) {
188 if (++$col >= $cols) {
189 $stat .= "<tr>";
190 $col = 0;
191 }
192 $stat .= "<td><a href='".escape_uri($_->[0])."'>$_->[0]</a><td align='right'>$_->[1]<td>&nbsp;";
193 }
194 $stat .= "</table>";
195 }
196
197 <<EOF;
198 <html>
199 <head><title>$self->{uri}</title></head>
200 <body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#000080" alink="#ff0000">
201 <h1>$data->{path}</h1>
202 $data->{top}
203 <hr />
204 <a href="/internal/status">Server Status Page &amp; Queueing Info</a>
205 <hr />
206 $stat
207 $data->{bot}
208 </body>
209 </html>
210 EOF
211 }
212
213 1;