changelog shortlog tags branches changeset files revisions annotate raw help

Mercurial > hg > plan9front / rc/bin/rc-httpd/handlers/dir-index

changeset 7262: f0621f169310
parent: 8d41ad3cc0ea
child: 40949afe3df8
author: Alex Musolino <alex@musolino.id.au>
date: Wed, 05 Jun 2019 16:04:50 +0930
permissions: -rwxr-xr-x
description: rc-httpd(8): avoid duplicate log output in dir-index handler

Delay calling do_log until just before emitting the response. This avoids
calling do_log before potentially handing control to the static-index
handler, which also calls do_log.
1 #!/bin/rc
2 PATH_INFO=`{echo $PATH_INFO | urlencode -d}
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 builtin cd $full_path
14 if(~ $"NOINDEXFILE ^ $"NOINDEX ''){
15  ifile=index.htm*
16  if(! ~ $ifile(1) *'*'){
17  PATH_INFO=$ifile(1)
18  FS_ROOT=''
19  exec serve-static
20  }
21 }
22 title=`{echo $SITE_TITLE | sed s,%s,^$"PATH_INFO^,}
23 title=$"title
24 lso=()
25 switch($2){
26 case size
27  # ls has no option to sort by size
28  # could pipe it through sort, I suppose
29 case date
30  lso=-t
31 }
32 do_log 200
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>'