nicer formatting
[tridge/junkcode.git] / cgibrowse
1 #!/bin/sh
2 # file browser in shell
3 # tridge@samba.org, July 2007
4
5 #######################
6 # spit out a http header
7 http_header() {
8 ctype="$1"
9     cat <<EOF
10 Content-Type: $ctype; charset=iso-8859-1
11
12 EOF
13 }
14
15 #######################
16 # called on fatal error
17 fatal() {
18 ecode="$1"
19 http_header "text/html";
20     cat <<EOF
21 <HTML><HEAD><TITLE>$ecode</TITLE></HEAD>
22 <BODY>
23 <H1>$ecode</H1>
24 Server error
25 </BODY></HTML>
26 EOF
27 exit 0
28 }
29
30 ###################
31 # directory listing
32 directory_list() {
33     http_header "text/html";
34     cat <<EOF
35 Index of $1<p>
36 <ul>
37 EOF
38     /bin/ls "$1" | while read fname; do
39         echo "<li><a href=\"$1/$fname\"> $fname</a>"
40     done
41     cat <<EOF
42 </ul></body></html>
43 EOF
44     exit 0
45 }
46
47 [ -z "$REQUEST_URI" ] && {
48     fatal "404 REQUEST_URI not set";
49 }
50
51
52 [ -d "$REQUEST_URI" ] && {
53     directory_list "$REQUEST_URI";
54 }
55
56 [ -r "$REQUEST_URI" ] || {
57     fatal "500 access denied";
58 }
59
60 # its a file download
61 http_header "text/plain";
62 /bin/cat "$REQUEST_URI"