ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/Coro/myhttpd/diridx.pl
Revision: 1.11
Committed: Tue Aug 28 02:43:02 2001 UTC (22 years, 10 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.10: +1 -0 lines
Log Message:
*** empty log message ***

File Contents

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