2007-05-27 05:25:55 -06:00
|
|
|
#!/usr/local/plan9/bin/rc
|
2008-09-26 06:23:19 -06:00
|
|
|
. ./cgilib.rc
|
2009-02-01 12:06:51 -07:00
|
|
|
. ./werclib.rc
|
|
|
|
. ./wercconf.rc
|
|
|
|
. ./corehandlers.rc
|
2009-02-06 14:09:32 -07:00
|
|
|
. ./fltr_cache.rc
|
2007-05-27 05:25:55 -06:00
|
|
|
cd ..
|
|
|
|
|
2009-02-22 22:25:08 -07:00
|
|
|
forbidden_uri_chars='[^a-zA-Z0-9_+\-\/\.,:]'
|
2008-09-25 03:06:14 -06:00
|
|
|
|
2009-01-23 08:46:48 -07:00
|
|
|
# Expected input: ls -F style, $sitedir/path/to/files/
|
2009-01-03 23:57:13 -07:00
|
|
|
# <ls -F+x><symlink hack><Useless?><hiden files >
|
2009-02-14 00:26:52 -07:00
|
|
|
dirfilter='s/\*$//; s,/+\./+,/,g; s,^\./,,; /\/[._][^\/]/d; /'$forbidden_uri_chars'/d; /\/index\.(md|html|txt|tpl)$/d; /\/(robots|sitemap)\.txt$/d; /_werc\/?$/d; '
|
2009-01-02 18:02:50 -07:00
|
|
|
dirclean=' s/\.(md|html|txt)$//; '
|
2008-07-04 20:32:29 -06:00
|
|
|
|
2008-09-16 18:16:33 -06:00
|
|
|
# Careful, the proper p9p path might not be set until initrc.local is sourced
|
|
|
|
path=(. $PLAN9/bin ./bin/ /bin/ /usr/bin)
|
|
|
|
|
2008-09-24 12:21:32 -06:00
|
|
|
headers=lib/headers.tpl
|
2009-01-12 19:18:19 -07:00
|
|
|
res_tail='</body></html>'
|
2009-01-16 19:05:19 -07:00
|
|
|
ll_add handlers_bar_left nav_tree
|
2009-01-22 01:22:15 -07:00
|
|
|
werc_apps=( apps/* )
|
2009-01-04 14:49:05 -07:00
|
|
|
werc_root=`{pwd}
|
2009-01-25 05:56:53 -07:00
|
|
|
sitesdir=sites
|
2008-09-16 18:16:33 -06:00
|
|
|
|
2009-02-01 12:06:51 -07:00
|
|
|
. ./etc/initrc
|
2008-07-15 19:05:41 -06:00
|
|
|
|
2008-09-16 18:16:33 -06:00
|
|
|
if(test -f etc/initrc.local)
|
2008-10-25 22:29:42 -06:00
|
|
|
. ./etc/initrc.local
|
2008-09-16 18:16:33 -06:00
|
|
|
|
2009-01-22 01:22:15 -07:00
|
|
|
for(a in $werc_apps)
|
|
|
|
. ./$a/app.rc
|
|
|
|
|
2009-01-30 08:18:07 -07:00
|
|
|
fn werc_exec_request {
|
|
|
|
site=$SERVER_NAME
|
|
|
|
base_url=http://$site
|
|
|
|
sitedir=$sitesdir/$site
|
|
|
|
master_template=`{get_lib_file default_master.tpl}
|
|
|
|
current_date_time=`{date}
|
|
|
|
|
|
|
|
# Note: $REQUEST_URI is not officially in CGI 1.1, but seems to be de-facto
|
|
|
|
req_path=`{echo -n $REQUEST_URI | sed 's/\?.*//; s!//+!/!g; s/'^$forbidden_uri_chars^'//g; s/\.\.*/./g; 1q'}
|
|
|
|
local_path=$sitedir$req_path
|
|
|
|
ifs='/' { args=`{echo -n $req_path} }
|
|
|
|
|
|
|
|
# Preload post args for templates where cgi's stdin is not accessible
|
|
|
|
if(~ $REQUEST_METHOD POST) {
|
|
|
|
load_post_args
|
|
|
|
login_user
|
|
|
|
}
|
2009-01-04 15:21:55 -07:00
|
|
|
|
2009-01-30 08:18:07 -07:00
|
|
|
if(~ $req_path */index)
|
|
|
|
perm_redirect `{echo $req_path | sed 's,/index$,/,'}
|
2009-01-04 15:21:55 -07:00
|
|
|
|
2009-01-30 08:18:07 -07:00
|
|
|
if(~ $local_path */) {
|
|
|
|
if(test -d $local_path)
|
|
|
|
local_path=$local_path^'index'
|
|
|
|
if not # XXX: This redir might step on apps with synthetic dirs.
|
|
|
|
perm_redirect `{echo $req_path|sed 's,/+$,,'}
|
|
|
|
}
|
2009-02-22 22:25:08 -07:00
|
|
|
if not if(~ $req_path *'.' *',' *';' *':')
|
|
|
|
perm_redirect `{echo $req_path | sed 's/[.,;:)]$//'}
|
2009-01-30 08:18:07 -07:00
|
|
|
if not if(test -d $local_path)
|
2009-02-22 22:25:08 -07:00
|
|
|
perm_redirect $req_path^'/'
|
2009-01-30 08:18:07 -07:00
|
|
|
|
|
|
|
if(! ~ $#args 0)
|
|
|
|
pageTitle=`{ echo $args|sed -e 's/ / - /g' -e 's/_/ /g' }
|
|
|
|
|
|
|
|
cd $sitedir
|
|
|
|
req_paths_list='/' # Note: req_paths_list doesn't include 'stnythetic' dirs.
|
|
|
|
conf_wd='/' # Used in config files to know where we are in the document tree.
|
|
|
|
if(test -f _werc/config)
|
|
|
|
. _werc/config
|
|
|
|
for(i in $args) {
|
|
|
|
conf_wd=$conf_wd^$i
|
|
|
|
req_paths_list=($req_paths_list $conf_wd)
|
|
|
|
if(test -d $i) {
|
|
|
|
conf_wd=$conf_wd'/'
|
|
|
|
cd $i
|
|
|
|
if(test -f _werc/config)
|
|
|
|
. _werc/config
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cd $werc_root
|
|
|
|
|
2009-02-14 00:09:27 -07:00
|
|
|
if(~ $#perm_redir_to 1)
|
|
|
|
perm_redirect $perm_redir_to
|
2009-01-30 08:18:07 -07:00
|
|
|
f=();t=()
|
|
|
|
for(i in $perm_redir_patterns) {
|
|
|
|
if(~ $#f 0)
|
|
|
|
f=$i
|
|
|
|
if not {
|
|
|
|
t=$i
|
2009-02-20 17:05:30 -07:00
|
|
|
# TODO: should handle absolute and relative urls differently.
|
2009-01-30 08:18:07 -07:00
|
|
|
from=$base_url^$req_path
|
|
|
|
to=`{ echo $from | sed 's!'$f'!'$t'!' }
|
|
|
|
if(! ~ $to $from)
|
|
|
|
perm_redirect $to
|
|
|
|
f=()
|
|
|
|
}
|
2008-12-27 03:53:58 -07:00
|
|
|
}
|
2007-06-02 23:36:01 -06:00
|
|
|
|
2009-01-30 08:18:07 -07:00
|
|
|
# Set Page title
|
2009-02-01 12:06:51 -07:00
|
|
|
if(~ $"pageTitle '')
|
|
|
|
pageTitle=$"siteTitle' '$"siteSubTitle
|
2009-01-30 08:18:07 -07:00
|
|
|
if not
|
|
|
|
pageTitle=$"pageTitle' | '$"siteTitle' '$"siteSubTitle
|
2008-06-27 16:53:24 -06:00
|
|
|
|
2009-01-30 08:18:07 -07:00
|
|
|
setup_handlers
|
2008-06-27 16:53:24 -06:00
|
|
|
|
2009-01-30 08:18:07 -07:00
|
|
|
if(! ~ $#debug 0)
|
|
|
|
dprint $"SERVER_NAME^$"REQUEST_URI - $"HTTP_USER_AGENT - $"REQUEST_METHOD - $"handler_body_main - $"master_template
|
2007-06-04 12:57:18 -06:00
|
|
|
|
2009-02-06 14:09:32 -07:00
|
|
|
template $headers $master_template | awk_buffer
|
2009-01-30 08:18:07 -07:00
|
|
|
echo $res_tail
|
|
|
|
}
|
2008-10-26 17:41:23 -06:00
|
|
|
|
2009-01-30 08:18:07 -07:00
|
|
|
werc_exec_request
|