ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/Coro/myhttpd/diridx.pl
Revision: 1.36
Committed: Sun Jan 24 14:16:56 2010 UTC (14 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.35: +26 -24 lines
Log Message:
use aio for directories, and no directory database

File Contents

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