dir-index (2519B)
1 #!/bin/rc 2 PATH_INFO=`{echo $PATH_INFO | urldecode.awk} 3 full_path=$"FS_ROOT^$"PATH_INFO 4 full_path=$"full_path 5 if(! test -d $full_path){ 6 error 404 7 exit 8 } 9 if(! test -r $full_path -x $full_path){ 10 error 503 11 exit 12 } 13 do_log 200 14 builtin cd $full_path 15 if(~ $"NOINDEXFILE ^ $"NOINDEX ''){ 16 ifile=index.htm* 17 if(! ~ $ifile(1) *'*'){ 18 PATH_INFO=$ifile(1) 19 FS_ROOT='' 20 exec serve-static 21 } 22 } 23 title=`{echo $SITE_TITLE | sed s,%s,^$"PATH_INFO^,} 24 title=$"title 25 lso=() 26 switch($2){ 27 case size 28 # ls has no option to sort by size 29 # could pipe it through sort, I suppose 30 case date 31 lso=-t 32 } 33 echo 'HTTP/1.1 200 OK'^$cr 34 emit_extra_headers 35 echo 'Content-type: text/html'^$cr 36 echo $cr 37 echo '<html> 38 <head> 39 <title>'^$title^'</title> 40 <style type="text/css"> 41 .size { 42 text-align: right; 43 padding-right: 4pt; 44 } 45 .day { 46 text-align: right; 47 padding-right: 3pt; 48 } 49 .datetime { 50 text-align: right; 51 } 52 .name { 53 text-align: right; 54 padding-left: 3pt; 55 } 56 </style> 57 </head> 58 <body>' 59 echo '<h1>'^$title^'</h1>' 60 if(! ~ $PATH_INFO /) 61 echo '<a href="../">Parent directory</a>' 62 echo '<table>' 63 ls -lQ $lso | awk ' 64 function urlencode(loc){ 65 # very minimal encoding, just enough for our static-file purposes 66 url=loc 67 gsub("%", "%25", url) # this one first! 68 gsub("\\$", "%24", url) 69 gsub("&", "%26", url) 70 gsub("\\+", "%2B", url) 71 gsub("\\?", "%3F", url) 72 gsub(" ", "%20", url) 73 gsub("\"", "%22", url) 74 gsub("#", "%23", url) 75 return url 76 } 77 function hrsize(size){ 78 if(size > 1073741824) return sprintf("%.1fGB", size/1073741824) 79 if(size > 10485760) return sprintf("%iMB", size/1048576) 80 if(size > 1048576) return sprintf("%.1fMB", size/1048576) 81 if(size > 10240) return sprintf("%iKB", size/1024) 82 if(size > 1024) return sprintf("%.1fKB", size/1024) 83 return sprintf("%iB", size) 84 } 85 /^(-|a)/ { 86 print "<tr>" 87 print "<td class=\"size\">"hrsize($6)"</td>" 88 print "<td class=\"month\">"$7"</td>" 89 print "<td class=\"day\">"$8"</td>" 90 print "<td class=\"datetime\">"$9"</td>" 91 $1="" ; $2="" ; $3="" ; $4="" ; $5="" ; $6="" ; $7="" ; $8="" ; $9="" 92 sub("^ *?", "") 93 print "<td><a class=\"file name\" href=\""urlencode($0)"\">"$0"</a></td>" 94 print "</tr>" 95 $0="" 96 } 97 /^d/ { 98 print "<tr>" 99 print "<td class=\"size\"> </td>" 100 print "<td class=\"month\">"$7"</td>" 101 print "<td class=\"day\">"$8"</td>" 102 print "<td class=\"datetime\">"$9"</td>" 103 $1="" ; $2="" ; $3="" ; $4="" ; $5="" ; $6="" ; $7="" ; $8="" ; $9="" 104 sub("^ *?", "") 105 print "<td><a class=\"dir name\" href=\""urlencode($0)"/\">"$0"/</a></td>" 106 print "</tr>" 107 }' 108 echo '</table> 109 110 </body> 111 </html>'