2016-02-04 13:05:05 -07:00
|
|
|
#!/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_+\-\/\.,:]'
|
2009-03-03 13:23:00 -07:00
|
|
|
difs=$ifs # Used to restore default ifs when needed
|
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 >
|
2010-04-25 05:58:40 -06:00
|
|
|
dirfilter='s/\*$//; s,/+\./+,/,g; s,^\./,,; /\/[._][^\/]/d; /'$forbidden_uri_chars'/d; /\/sitemap\.xml$/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
|
2016-02-04 13:05:05 -07:00
|
|
|
path=(. /bin ./bin)
|
2008-09-16 18:16:33 -06:00
|
|
|
|
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-03-13 06:04:25 -06:00
|
|
|
http_content_type='text/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
|
2016-02-04 13:05:05 -07:00
|
|
|
base_url=http://$site:$SERVER_PORT
|
2009-01-30 08:18:07 -07:00
|
|
|
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
|
2009-06-09 18:58:36 -06:00
|
|
|
# Note: We only urldecode %5F->'_' because some sites (stackoverflow.com?) urlencode it in their links,
|
2016-02-04 13:05:05 -07:00
|
|
|
# perhaps we should completel urldecode the whole url.
|
2009-06-09 18:58:36 -06:00
|
|
|
req_path=`{echo -n $REQUEST_URI | sed 's/\?.*//; s!//+!/!g; s/%5[Ff]/_/g; s/'^$forbidden_uri_chars^'//g; s/\.\.*/./g; 1q'}
|
2009-03-03 06:15:46 -07:00
|
|
|
req_url=$base_url^$req_path
|
2009-01-30 08:18:07 -07:00
|
|
|
local_path=$sitedir$req_path
|
2009-07-28 12:28:35 -06:00
|
|
|
local_file=''
|
2009-01-30 08:18:07 -07:00
|
|
|
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'
|
2009-03-06 09:32:33 -07:00
|
|
|
# XXX: This redir might step on apps with synthetic dirs.
|
2016-02-04 13:05:05 -07:00
|
|
|
if not if(ls `{basename -d $local_path}^* >/dev/null >[2]/dev/null)
|
|
|
|
perm_redirect `{echo $req_path|sed 's,/+$,,'}
|
2009-01-30 08:18:07 -07:00
|
|
|
}
|
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)
|
2009-07-19 00:01:42 -06:00
|
|
|
ifs=$NEW_LINE { pageTitle=`{ echo $args|sed -e 's/ / - /g' -e 's/([a-z])-([a-z])/\1 \2/g' -e 's/_/ /g' } }
|
2009-01-30 08:18:07 -07:00
|
|
|
|
|
|
|
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-03-13 17:28:34 -06:00
|
|
|
for(l in $perm_redir_patterns) {
|
|
|
|
p=$$l
|
|
|
|
r=$p(1)
|
|
|
|
# If target is absolute, then patern must match whole string
|
2010-05-24 17:35:08 -06:00
|
|
|
if(~ $p(2) http://* https://*)
|
2009-03-13 17:28:34 -06:00
|
|
|
r='^'$r
|
|
|
|
t=`{ echo $req_path | sed 's!'^$r^'!'^$p(2)^'!' } # Malicious danger!
|
|
|
|
|
|
|
|
if(! ~ $"t '' $req_path)
|
|
|
|
perm_redirect $t
|
2008-12-27 03:53:58 -07:00
|
|
|
}
|
2007-06-02 23:36:01 -06:00
|
|
|
|
2009-07-28 12:28:35 -06:00
|
|
|
setup_handlers
|
|
|
|
|
|
|
|
|
2009-01-30 08:18:07 -07:00
|
|
|
# Set Page title
|
2009-07-28 12:28:35 -06:00
|
|
|
if(! ~ $local_file '') {
|
|
|
|
t=`{get_file_title $local_file}
|
|
|
|
if(! ~ $"t '')
|
|
|
|
pageTitle=$t
|
|
|
|
}
|
|
|
|
|
|
|
|
# XXX Is this never true? because we set pageTitle earlier based on url.
|
|
|
|
if(~ $"pageTitle '')
|
2009-02-01 12:06:51 -07:00
|
|
|
pageTitle=$"siteTitle' '$"siteSubTitle
|
2010-01-18 17:30:24 -07:00
|
|
|
# if not
|
|
|
|
# pageTitle=$"pageTitle' | '$"siteTitle' '$"siteSubTitle
|
2008-06-27 16:53:24 -06:00
|
|
|
|
2009-03-13 06:04:25 -06:00
|
|
|
for(h in $extraHttpHeaders)
|
|
|
|
echo $h
|
2016-02-04 13:05:05 -07:00
|
|
|
echo Content-Type: $http_content_type
|
2009-03-21 14:08:49 -06:00
|
|
|
echo # End of HTTP headers
|
2009-03-13 06:04:25 -06:00
|
|
|
|
2009-01-30 08:18:07 -07:00
|
|
|
if(! ~ $#debug 0)
|
2016-02-04 13:05:05 -07:00
|
|
|
dprint $"SERVER_NAME^$"REQUEST_URI - $"HTTP_USER_AGENT - $"REQUEST_METHOD - $"handler_body_main - $"master_template
|
2007-06-04 12:57:18 -06:00
|
|
|
|
2009-08-09 10:39:54 -06:00
|
|
|
if(~ $REQUEST_METHOD HEAD)
|
|
|
|
exit
|
2016-02-04 13:05:05 -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
|
|
|
|
2016-02-04 13:05:05 -07:00
|
|
|
werc_exec_request
|