changelog shortlog tags branches changeset files revisions annotate raw help

Mercurial > hg > werc / bin/werc.rc

changeset 360: 279b02c9df5a
parent: 031828ebade4
child: b1112b957e36
author: uriel@engel.se.cat-v.org
date: Sun, 25 Jan 2009 13:56:53 +0100
permissions: -rwxr-xr-x
description: Various changes:
- New generic http_redirect function, and two shortcuts for 303: post_redirect and 301: perm_redirect
- Allow get_lib_file to take a default value, and change where master_template is set.
- Fix bugs in auth code and properly trim ^M from post data.
- Other cosmetic/minor improvements.
1 #!/usr/local/plan9/bin/rc
2 . ./cgilib.rc
3 cd ..
4 
5 forbidden_uri_chars='[^a-zA-Z0-9_+\-\/\.]'
6 
7 # Expected input: ls -F style, $sitedir/path/to/files/
8 # <ls -F+x><symlink hack><Useless?><hiden files >
9 dirfilter='s/\*$//; s,/+\./+,/,g; s,^\./,,; /\/[._][^\/]/d; /'^$forbidden_uri_chars^'/d; /^\/(robots|sitemap)\.txt$|\/index\.(md|html|txt|tpl)$/d; /_werc\/?$/d; '
10 dirclean=' s/\.(md|html|txt)$//; '
11 
12 # To be used from config files
13 fn hide_paths {
14  for(i in $*)
15  dirfilter=$dirfilter^'/^'$i'$/d; '
16 }
17 
18 # Sidebar
19 fn nav_tree {
20  if(! ~ $#sideBarNavTitle 0)
21  echo '<p class="sideBarTitle">'$"sideBarNavTitle':</p>'
22  # Ignore stderr, last path element might be a file that doesn't exist (eg., foo for foo.md)
23  # /./ to deal with p9p's ls failure to follow dir symlinks otherwise
24  ls -F $sitedir/./$req_paths_list >[2]/dev/null \
25  | sed 's!^'$sitedir'!!; '^$dirfilter^'/\/[^_.\/][^\/]*(\.(md|txt|html)|\/)$/!d; '^$dirclean \
26  | sort -u | awk -F/ '
27  function p(x, y, s) { for(i=0; i < x-y; i+=1) print s }
28  {
29  d = ""
30  if(match($0, "/$"))
31  d = "/"
32  sub("/$", "") # Strip trailing / for dirs so NF is consistent
33 
34  p(NF, lNF, "<ul class=\"side-bar\">")
35  p(lNF, NF, "</ul>")
36  lNF = NF
37 
38  bname = $NF d
39  path = $0 d
40  gsub("_", " ", bname)
41 
42  if(index(ENVIRON["req_path"] "/", path) == 1)
43  print "<li><a href=\"" path "\" class=\"thisPage\">&raquo;<i> " bname "</i></a>"
44  else
45  print "<li><a href=\"" path "\">&rsaquo; " bname "</a></li>"
46  }
47  END { p(lNF, 0, "</ul>") }'
48 }
49 
50 
51 # Handlers
52 fn md_handler { $formatter < $1 }
53 
54 fn tpl_handler { template $* }
55 
56 fn html_handler {
57  # body states: 0 = no <body> found, 2 = after <body>, 1 = after <body></body>, -1 = after </body>
58  awk 'gsub(".*<[Bb][Oo][Dd][Yy][^>]*>", "") > 0 {body=2}
59  gsub("</ *[Bb][Oo][Dd][Yy][^>]*>.*", "") > 0 {print; body=body-1}
60  body==2 {print}
61  body==0 {buf=buf "\n" $0}
62  END {if(body<=0) {print buf}}' < $1
63 }
64 
65 fn txt_handler {
66  # Note: Words are not broken, even if they are way beyond 82 chars long
67  echo '<pre>' `{ sed 's/</\&lt;/g; s/>/\&gt;/g' < $1 | fmt -l 82 -j } '</pre>'
68 }
69 
70 fn dir_listing_handler {
71  d=`{basename -d $1}
72  echo $d|sed 's,.*//,,g; s,/$,,; s,/, / ,g; s,.*,<h1 class="dir-list-head">&</h1> <ul class="dir-list">,'
73  # Symlinks suck: '/.' forces ls to list the linked dir if $d is a symlink.
74  ls -F $dir_listing_ls_opts $sitedir$d/. | sed $dirfilter$dirclean' s,.*/([^/]+/?)$,<li><a href="\1">\1</a></li>,'
75  echo '</ul>'
76 }
77 
78 fn setup_handlers {
79 
80  if(test -f $local_path.md)
81  handler_body_main=(md_handler $local_path.md)
82  if not if(test -f $local_path.tpl)
83  handler_body_main=(tpl_handler $local_path.tpl)
84  if not if(test -f $local_path.html)
85  handler_body_main=(html_handler $local_path.html)
86  # Global tpl (eg sitemap.tpl), should take precedence over txt handler!
87  if not if(test -f lib^$req_path^.tpl)
88  handler_body_main=(tpl_handler lib^$req_path^.tpl)
89  if not if(test -f $local_path.txt)
90  handler_body_main=(txt_handler $local_path.txt)
91 
92  # XXX Should check that $enabled_apps exist in $werc_apps?
93  # XXX Should split init of apps that provide main handler (eg., blog) and apps that don't (eg., comments)?
94  if(! ~ $#enabled_apps 0)
95  for(a in $enabled_apps)
96  $a^'_init'
97 
98  if(! ~ $#handler_body_main 0)
99  { } # We are done
100  # Dir listing
101  if not if(~ $local_path */index)
102  handler_body_main=(dir_listing_handler $req_path)
103  # Canonize explicit .html urls, the web server might handle this first!
104  if not if(~ $local_path *.html && test -f $local_path)
105  perm_redirect `{ echo $req_path|sed 's/.html$//' }
106  # Fallback static file handler
107  if not if(test -f $local_path)
108  static_file $local_path
109  if not if(~ $req_path /pub/* && test -f .$req_path)
110  static_file .$req_path
111  # File not found
112  if not {
113  handler_body_main=(tpl_handler `{get_lib_file 404.tpl})
114  echo 'Status: 404 Not Found'
115  dprint 'NOT FOUND: '$SERVER_NAME^$"REQUEST_URI^' - '^$"HTTP_REFERER^' - '^$"HTTP_USER_AGENT
116  }
117 }
118 
119 fn run_handlers { for(h in $*) run_handler $$h }
120 fn run_handler { $*(1) $*(2-) }
121 
122 # Careful, the proper p9p path might not be set until initrc.local is sourced
123 path=(. $PLAN9/bin ./bin/ /bin/ /usr/bin)
124 
125 headers=lib/headers.tpl
126 res_tail='</body></html>'
127 ll_add handlers_bar_left nav_tree
128 werc_apps=( apps/* )
129 werc_root=`{pwd}
130 sitesdir=sites
131 for(i in siteTitle siteSubTitle pageTitle extraHeaders)
132  $i = ''
133 
134 # TODO: Per-req variables should move after initrc loading.
135 site=$SERVER_NAME
136 base_url=http://$site/
137 sitedir=$sitesdir/$site
138 master_template=`{get_lib_file default_master.tpl}
139 current_date_time=`{date}
140 
141 . ./etc/initrc
142 
143 if(test -f etc/initrc.local)
144  . ./etc/initrc.local
145 
146 for(a in $werc_apps)
147  . ./$a/app.rc
148 
149 # Parse request URL
150 # NOTE: $REQUEST_URI is not officially in CGI 1.1, but seems to be de-facto
151 req_path=`{echo -n $REQUEST_URI | sed 's/\?.*//; s/'^$forbidden_uri_chars^'//g; s/\.\.*/./g; 1q'}
152 local_path=$sitedir$req_path
153 ifs='/' { args=`{echo -n $req_path} }
154 
155 # Hack: preload post data so we can access it from templates where cgi's stdin is not accesible
156 if(~ $REQUEST_METHOD POST) {
157  load_post_args
158  login_user
159 }
160 
161 if(! ~ $#args 0)
162  pageTitle=`{ echo $args|sed -e 's/ / - /g' -e 's/_/ /g' }
163 
164 if(~ $req_path */index)
165  perm_redirect `{echo $req_path | sed 's,/index$,/,'}
166 
167 if(~ $local_path */) {
168  if(test -d $local_path)
169  local_path=$local_path^'index'
170  if not # XXX: This redir might step on apps with synthetic dirs.
171  perm_redirect `{echo $req_path|sed 's,/+$,,'}
172 }
173 if not if(test -d $local_path)
174  perm_redirect $req_path^'/'
175 
176 cd $sitedir
177 req_paths_list='/' # Note: req_paths_list doesn't include 'stnythetic' dirs.
178 conf_wd='/' # Used in config files to know where we are in the document tree.
179 if(test -f _werc/config)
180  . _werc/config
181 for(i in $args) {
182  conf_wd=$conf_wd^$i
183  req_paths_list=($req_paths_list $conf_wd)
184  if(test -d $i) {
185  conf_wd=$conf_wd^'/'
186  cd $i
187  if(test -f _werc/config)
188  . _werc/config
189  }
190 }
191 cd $werc_root
192 
193 # Redirections and other preprocessing
194 if(~ $#redirectPermanent 1) {
195  perm_redirect $"redirectPermanent
196 }
197 if not if(~ $#redirectPermanent 2) {
198  from='http://'^$SERVER_NAME^$req_path
199  to=`{echo $from|sed 's@'^$redirectPermanent(1)^'@'^$redirectPermanent(2)^'@'}
200  if(! ~ $to $from)
201  perm_redirect $to
202 }
203 
204 # Set Page title
205 if(~ $pageTitle '')
206  pageTitle=$siteTitle^' '^$siteSubTitle
207 if not
208  pageTitle=$"pageTitle^' | '^$"siteTitle^' '^$"siteSubTitle
209 
210 setup_handlers
211 
212 if(! ~ $#debug 0)
213  dprint ' '$"SERVER_NAME^$"REQUEST_URI' - '$"HTTP_USER_AGENT' - '$"REQUEST_METHOD' - '$"handler_body_main - $"master_template
214 
215 template $headers $master_template | awk_buffer
216 echo $res_tail