ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/Coro/myhttpd/diridx.pl
Revision: 1.3
Committed: Sat Aug 11 20:04:37 2001 UTC (22 years, 11 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.2: +1 -1 lines
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
110 my $st = sql_exec \my($statdata),
111 "select statdata from diridx where mtime = ? and path = ?",
112 $mtime, $self->{path};
113
114 if ($st->fetch) {
115 $statdata = Storable::thaw $statdata;
116 return $statdata if $statdata->{version} == $SD_VERSION;
117 }
118
119 $self->slog(8, "creating index cache for $self->{path}");
120
121 $statdata = $self->gen_statdata;
122 $statdata->{version} = $SD_VERSION;
123
124 sql_exec "delete from diridx where path = ?", $self->{path};
125 sql_exec "insert into diridx (path, mtime, statdata) values (?, ?, ?)",
126 $self->{path}, $mtime, Storable::freeze $statdata;
127
128 $statdata;
129 }
130
131 sub conn::diridx {
132 my $self = shift;
133
134 my $data = $self->get_statdata;
135
136 <<EOF;
137 <html>
138 <head><title>$self->{uri}</title></head>
139 <body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#000080" alink="#ff0000">
140 <h1>$data->{path}</h1>
141 $data->{top}
142 <small><div align="right"><tt>$self->{remote_addr}/$self->{country} - myhttpd/$VERSION</tt></div></small>
143 <hr>
144 $data->{stat}
145 $data->{bot}
146 </body>
147 </html>
148 EOF
149 }
150
151 sub handle_redirect { # unused
152 if (-f ".redirect") {
153 if (open R, "<.redirect") {
154 while (<R>) {
155 if (/^(?:$host$port)$uri([^ \tr\n]*)[ \t\r\n]+(.*)$/) {
156 my $rem = $1;
157 my $url = $2;
158 print $nph ? "HTTP/1.0 302 Moved\n" : "Status: 302 Moved\n";
159 print <<EOF;
160 Location: $url
161 Content-Type: text/html
162
163 <html>
164 <head><title>Page Redirection to $url</title></head>
165 <meta http-equiv="refresh" content="0;URL=$url">
166 </head>
167 <body text="black" link="#1010C0" vlink="#101080" alink="red" bgcolor="white">
168 <large>
169 This page has moved to $url.<br />
170 <a href="$url">
171 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 ;)
172 </a>
173 </large>
174 </body>
175 </html>
176 EOF
177 }
178 }
179 }
180 }
181 }
182
183 1;