ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/Coro/myhttpd/diridx.pl
Revision: 1.5
Committed: Tue Aug 14 02:28:51 2001 UTC (22 years, 11 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.4: +7 -1 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.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 root 1.5
136     my $uptime = int (time - $::starttime);
137     $uptime = sprintf "%02dd %02d:%02d",
138     int ($uptime / (60 * 60 * 24)),
139     int ($uptime / (60 * 60)) % 24,
140     int ($uptime / 60) % 60;
141 root 1.1
142     <<EOF;
143     <html>
144     <head><title>$self->{uri}</title></head>
145     <body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#000080" alink="#ff0000">
146     <h1>$data->{path}</h1>
147     $data->{top}
148 root 1.5 <small><div align="right"><tt>$self->{remote_addr}/$self->{country} - $::conns connection(s) - uptime $uptime - myhttpd/$VERSION</tt></div></small>
149 root 1.1 <hr>
150     $data->{stat}
151     $data->{bot}
152     </body>
153     </html>
154     EOF
155     }
156    
157     sub handle_redirect { # unused
158     if (-f ".redirect") {
159     if (open R, "<.redirect") {
160     while (<R>) {
161     if (/^(?:$host$port)$uri([^ \tr\n]*)[ \t\r\n]+(.*)$/) {
162     my $rem = $1;
163     my $url = $2;
164     print $nph ? "HTTP/1.0 302 Moved\n" : "Status: 302 Moved\n";
165     print <<EOF;
166     Location: $url
167     Content-Type: text/html
168    
169     <html>
170     <head><title>Page Redirection to $url</title></head>
171     <meta http-equiv="refresh" content="0;URL=$url">
172     </head>
173     <body text="black" link="#1010C0" vlink="#101080" alink="red" bgcolor="white">
174     <large>
175     This page has moved to $url.<br />
176     <a href="$url">
177     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 ;)
178     </a>
179     </large>
180     </body>
181     </html>
182     EOF
183     }
184     }
185     }
186     }
187     }
188    
189     1;