ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/Coro/myhttpd/diridx.pl
Revision: 1.1
Committed: Sat Aug 11 16:34:47 2001 UTC (22 years, 11 months ago) by root
Content type: text/plain
Branch: MAIN
Log Message:
*** empty log message ***

File Contents

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