ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/myhttpd/diridx.pl
Revision: 1.15
Committed: Mon Sep 10 22:16:20 2001 UTC (22 years, 10 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.14: +2 -1 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     if (-d _) {
59 root 1.15 next unless 0555 == ((stat _)[2] & 0555);
60 root 1.1 $dlen = length $_ if length $_ > $dlen;
61 root 1.9 push @{$data->{d}}, $_;
62 root 1.1 } else {
63 root 1.15 next unless 0444 == ((stat _)[2] & 0444);
64 root 1.1 my $s = -s _;
65     $flen = length $_ if length $_ > $dlen;
66     $slen = length $s if length $s > $dlen;
67 root 1.9 push @{$data->{f}}, [$_, $s];
68 root 1.1 }
69     }
70 root 1.9 $data->{dlen} = $dlen;
71     $data->{flen} = $flen;
72     $data->{slen} = $slen;
73 root 1.1 }
74    
75     $data;
76     }
77    
78     sub conn::get_statdata {
79     my $self = shift;
80    
81     my $mtime = $self->{stat}[9];
82    
83 root 1.9 $statdata = $diridx{$self->{path}};
84 root 1.6
85 root 1.9 if (defined $statdata) {
86     $$statdata = Storable::thaw $statdata;
87     return $$statdata
88     if $$statdata->{version} == $SD_VERSION
89     && $$statdata->{mtime} == $mtime;
90 root 1.1 }
91    
92     $self->slog(8, "creating index cache for $self->{path}");
93    
94 root 1.6 $$statdata = $self->gen_statdata;
95     $$statdata->{version} = $SD_VERSION;
96 root 1.9 $$statdata->{mtime} = $mtime;
97 root 1.1
98 root 1.9 $diridx{$self->{path}} = Storable::freeze $$statdata;
99 root 1.11 (tied %diridx)->db_sync;
100 root 1.1
101 root 1.6 $$statdata;
102 root 1.1 }
103    
104     sub conn::diridx {
105     my $self = shift;
106    
107     my $data = $self->get_statdata;
108 root 1.5
109     my $uptime = int (time - $::starttime);
110     $uptime = sprintf "%02dd %02d:%02d",
111     int ($uptime / (60 * 60 * 24)),
112     int ($uptime / (60 * 60)) % 24,
113     int ($uptime / 60) % 60;
114 root 1.1
115 root 1.9 my $stat;
116     if ($data->{dlen}) {
117     $stat .= "<table><tr><th>Directories</th></tr>";
118     $data->{dlen} += 1;
119     my $cols = int ((79 + $data->{dlen}) / $data->{dlen});
120     $cols = @{$data->{d}} if @{$data->{d}} < $cols;
121     my $col = $cols;
122     for (@{$data->{d}}) {
123     if (++$col >= $cols) {
124     $stat .= "<tr>";
125     $col = 0;
126     }
127     if ("$self->{path}$_" =~ $conn::blockuri{$self->{country}}) {
128     $stat .= "<td>$_ ";
129     } else {
130     $stat .= "<td><a href='".escape_uri("$_/")."'>$_</a> ";
131     }
132     }
133     $stat .= "</table>";
134     }
135     if ($data->{flen}) {
136     $data->{flen} += 1 + $data->{slen} + 1 + 3;
137     my $cols = int ((79 + $data->{flen}) / $data->{flen});
138     $cols = @{$data->{f}} if @{$data->{f}} < $cols;
139     my $col = $cols;
140     $stat .= "<table><tr>". ("<th align='left'>File<th>Size<th>&nbsp;" x $cols);
141     for (@{$data->{f}}) {
142     if (++$col >= $cols) {
143     $stat .= "<tr>";
144     $col = 0;
145     }
146     $stat .= "<td><a href='".escape_uri($_->[0])."'>$_->[0]</a><td align='right'>$_->[1]<td>&nbsp;";
147     }
148     $stat .= "</table>";
149     }
150    
151 root 1.14 my $waiters = sprintf "%d/%d", $::transfers[0][0]->waiters+0, $::transfers[1][0]->waiters+0;
152     my $avgtime = sprintf "%d/%d second(s)", $::transfers[0][1], $::transfers[1][1];
153 root 1.12
154 root 1.1 <<EOF;
155     <html>
156     <head><title>$self->{uri}</title></head>
157     <body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#000080" alink="#ff0000">
158     <h1>$data->{path}</h1>
159     $data->{top}
160 root 1.12 <small><div align="right">
161     <tt>$self->{remote_id}/$self->{country} - $::conns connection(s) - uptime $uptime - myhttpd/$VERSION
162     </tt></div></small>
163 root 1.13 <hr />
164     clients waiting for data transfer: $waiters<br />
165     average waiting time until transfer starts: $avgtime <small>(adjust your timeout values)</small><br />
166 root 1.12 <hr />
167 root 1.9 $stat
168 root 1.1 $data->{bot}
169     </body>
170     </html>
171     EOF
172     }
173    
174     sub handle_redirect { # unused
175     if (-f ".redirect") {
176     if (open R, "<.redirect") {
177     while (<R>) {
178     if (/^(?:$host$port)$uri([^ \tr\n]*)[ \t\r\n]+(.*)$/) {
179     my $rem = $1;
180     my $url = $2;
181     print $nph ? "HTTP/1.0 302 Moved\n" : "Status: 302 Moved\n";
182     print <<EOF;
183     Location: $url
184     Content-Type: text/html
185    
186     <html>
187     <head><title>Page Redirection to $url</title></head>
188     <meta http-equiv="refresh" content="0;URL=$url">
189     </head>
190     <body text="black" link="#1010C0" vlink="#101080" alink="red" bgcolor="white">
191     <large>
192     This page has moved to $url.<br />
193     <a href="$url">
194 root 1.7 The automatic redirection has failed. Please try a <i>slightly</i>
195     newer browser next time, and in the meantime <i>please</i> follow this link ;)
196 root 1.1 </a>
197     </large>
198     </body>
199     </html>
200     EOF
201     }
202     }
203     }
204     }
205     }
206    
207     1;