ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/Coro/myhttpd/diridx.pl
Revision: 1.38
Committed: Mon Mar 8 07:38:21 2010 UTC (14 years, 3 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-6_0, rel-6_5, rel-6_10, rel-6_09, rel-6_08, rel-6_07, rel-6_06, rel-6_05, rel-6_04, rel-6_03, rel-6_02, rel-6_01, rel-5_371, rel-5_372, rel-6_512, rel-6_513, rel-6_511, rel-6_514, rel-5_22, rel-5_23, rel-5_24, rel-5_25, rel-6_32, rel-6_33, rel-6_31, rel-6_36, rel-6_37, rel-6_38, rel-6_39, rel-5_37, rel-5_36, rel-6_23, rel-6_29, rel-6_28, rel-6_46, rel-6_45, rel-6_51, rel-6_52, rel-6_53, rel-6_54, rel-6_55, rel-6_56, rel-6_57, rel-6_43, rel-6_42, rel-6_41, rel-6_47, rel-6_44, rel-6_49, rel-6_48, HEAD
Changes since 1.37: +8 -6 lines
Log Message:
uri's are difficult stuff

File Contents

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