ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/Coro/myhttpd/diridx.pl
Revision: 1.13
Committed: Wed Aug 29 01:40:44 2001 UTC (22 years, 10 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.12: +6 -4 lines
Log Message:
*** empty log message ***

File Contents

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