ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/Coro/myhttpd/diridx.pl
Revision: 1.12
Committed: Wed Aug 29 01:32:50 2001 UTC (22 years, 10 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.11: +12 -2 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     my $avgtime = int (sum(@::wait_time) / @::wait_time);
152    
153 root 1.1 <<EOF;
154     <html>
155     <head><title>$self->{uri}</title></head>
156     <body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#000080" alink="#ff0000">
157     <h1>$data->{path}</h1>
158     $data->{top}
159 root 1.12 <hr />
160     clients waiting for data transfer: $waiters<br />
161     average waiting time until transfer starts: $avgtime seconds <small>(adjust your timeout values)</small><br />
162     <small><div align="right">
163     <tt>$self->{remote_id}/$self->{country} - $::conns connection(s) - uptime $uptime - myhttpd/$VERSION
164     </tt></div></small>
165     <hr />
166 root 1.9 $stat
167 root 1.1 $data->{bot}
168     </body>
169     </html>
170     EOF
171     }
172    
173     sub handle_redirect { # unused
174     if (-f ".redirect") {
175     if (open R, "<.redirect") {
176     while (<R>) {
177     if (/^(?:$host$port)$uri([^ \tr\n]*)[ \t\r\n]+(.*)$/) {
178     my $rem = $1;
179     my $url = $2;
180     print $nph ? "HTTP/1.0 302 Moved\n" : "Status: 302 Moved\n";
181     print <<EOF;
182     Location: $url
183     Content-Type: text/html
184    
185     <html>
186     <head><title>Page Redirection to $url</title></head>
187     <meta http-equiv="refresh" content="0;URL=$url">
188     </head>
189     <body text="black" link="#1010C0" vlink="#101080" alink="red" bgcolor="white">
190     <large>
191     This page has moved to $url.<br />
192     <a href="$url">
193 root 1.7 The automatic redirection has failed. Please try a <i>slightly</i>
194     newer browser next time, and in the meantime <i>please</i> follow this link ;)
195 root 1.1 </a>
196     </large>
197     </body>
198     </html>
199     EOF
200     }
201     }
202     }
203     }
204     }
205    
206     1;