Replace $redirectPermanent with new conf_perm_redirect function. New notices_handler for error/note/success notification boxes. Rename hide_paths to conf_hide_paths. Reorder variable setting, all req-specifc vars are set after initrc and app sourcing, moved request handling code to its own function, this makes us ready to do scgi and other optimizations.
This commit is contained in:
parent
2ee4609eb1
commit
590816c6d4
1 changed files with 91 additions and 74 deletions
79
bin/werc.rc
79
bin/werc.rc
|
|
@ -6,23 +6,30 @@ forbidden_uri_chars='[^a-zA-Z0-9_+\-\/\.]'
|
||||||
|
|
||||||
# Expected input: ls -F style, $sitedir/path/to/files/
|
# Expected input: ls -F style, $sitedir/path/to/files/
|
||||||
# <ls -F+x><symlink hack><Useless?><hiden files >
|
# <ls -F+x><symlink hack><Useless?><hiden files >
|
||||||
dirfilter='s/\*$//; s,/+\./+,/,g; s,^\./,,; /\/[._][^\/]/d; /'^$forbidden_uri_chars^'/d; /^\/(robots|sitemap)\.txt$|\/index\.(md|html|txt|tpl)$/d; /_werc\/?$/d; '
|
dirfilter='s/\*$//; s,/+\./+,/,g; s,^\./,,; /\/[._][^\/]/d; /'$forbidden_uri_chars'/d; /^\/(robots|sitemap)\.txt$|\/index\.(md|html|txt|tpl)$/d; /_werc\/?$/d; '
|
||||||
dirclean=' s/\.(md|html|txt)$//; '
|
dirclean=' s/\.(md|html|txt)$//; '
|
||||||
|
|
||||||
# To be used from config files
|
# To be used from config files
|
||||||
fn hide_paths {
|
fn conf_perm_redirect {
|
||||||
|
if(~ $#* 1)
|
||||||
|
perm_redirect $1
|
||||||
|
if not
|
||||||
|
perm_redir_patterns=($perm_redir_patterns $1 $2)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn conf_hide_paths {
|
||||||
for(i in $*)
|
for(i in $*)
|
||||||
dirfilter=$dirfilter^'/^'$i'$/d; '
|
dirfilter=$dirfilter^'/^'$i'$/d; '
|
||||||
}
|
}
|
||||||
|
|
||||||
# Sidebar
|
# Standard handlers
|
||||||
fn nav_tree {
|
fn nav_tree {
|
||||||
if(! ~ $#sideBarNavTitle 0)
|
if(! ~ $#sideBarNavTitle 0)
|
||||||
echo '<p class="sideBarTitle">'$"sideBarNavTitle':</p>'
|
echo '<p class="sideBarTitle">'$"sideBarNavTitle':</p>'
|
||||||
# Ignore stderr, last path element might be a file that doesn't exist (eg., foo for foo.md)
|
# Ignore stderr, last path element might be a file that doesn't exist (eg., foo for foo.md)
|
||||||
# /./ to deal with p9p's ls failure to follow dir symlinks otherwise
|
# /./ to deal with p9p's ls failure to follow dir symlinks otherwise
|
||||||
ls -F $sitedir/./$req_paths_list >[2]/dev/null \
|
ls -F $sitedir/./$req_paths_list >[2]/dev/null \
|
||||||
| sed 's!^'$sitedir'!!; '^$dirfilter^'/\/[^_.\/][^\/]*(\.(md|txt|html)|\/)$/!d; '^$dirclean \
|
| sed 's!^'$sitedir'!!; '$dirfilter'/\/[^_.\/][^\/]*(\.(md|txt|html)|\/)$/!d; '$dirclean \
|
||||||
| sort -u | awk -F/ '
|
| sort -u | awk -F/ '
|
||||||
function p(x, y, s) { for(i=0; i < x-y; i+=1) print s }
|
function p(x, y, s) { for(i=0; i < x-y; i+=1) print s }
|
||||||
{
|
{
|
||||||
|
|
@ -48,7 +55,6 @@ fn nav_tree {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
# Handlers
|
|
||||||
fn md_handler { $formatter < $1 }
|
fn md_handler { $formatter < $1 }
|
||||||
|
|
||||||
fn tpl_handler { template $* }
|
fn tpl_handler { template $* }
|
||||||
|
|
@ -77,6 +83,12 @@ fn dir_listing_handler {
|
||||||
echo '</ul>'
|
echo '</ul>'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn notices_handler {
|
||||||
|
for(type in notify_errors notify_notes notify_success)
|
||||||
|
for(n in $$type)
|
||||||
|
echo '<div class="'$type'"><b>'$"n'</b></div>'
|
||||||
|
}
|
||||||
|
|
||||||
fn setup_handlers {
|
fn setup_handlers {
|
||||||
|
|
||||||
if(test -f $local_path.md)
|
if(test -f $local_path.md)
|
||||||
|
|
@ -133,13 +145,6 @@ sitesdir=sites
|
||||||
for(i in siteTitle siteSubTitle pageTitle extraHeaders)
|
for(i in siteTitle siteSubTitle pageTitle extraHeaders)
|
||||||
$i = ''
|
$i = ''
|
||||||
|
|
||||||
# TODO: Per-req variables should move after initrc loading.
|
|
||||||
site=$SERVER_NAME
|
|
||||||
base_url=http://$site/
|
|
||||||
sitedir=$sitesdir/$site
|
|
||||||
master_template=`{get_lib_file default_master.tpl}
|
|
||||||
current_date_time=`{date}
|
|
||||||
|
|
||||||
. ./etc/initrc
|
. ./etc/initrc
|
||||||
|
|
||||||
if(test -f etc/initrc.local)
|
if(test -f etc/initrc.local)
|
||||||
|
|
@ -148,21 +153,24 @@ if(test -f etc/initrc.local)
|
||||||
for(a in $werc_apps)
|
for(a in $werc_apps)
|
||||||
. ./$a/app.rc
|
. ./$a/app.rc
|
||||||
|
|
||||||
# Parse request URL
|
fn werc_exec_request {
|
||||||
# NOTE: $REQUEST_URI is not officially in CGI 1.1, but seems to be de-facto
|
site=$SERVER_NAME
|
||||||
req_path=`{echo -n $REQUEST_URI | sed 's/\?.*//; s/'^$forbidden_uri_chars^'//g; s/\.\.*/./g; 1q'}
|
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
|
local_path=$sitedir$req_path
|
||||||
ifs='/' { args=`{echo -n $req_path} }
|
ifs='/' { args=`{echo -n $req_path} }
|
||||||
|
|
||||||
# Hack: preload post data so we can access it from templates where cgi's stdin is not accesible
|
# Preload post args for templates where cgi's stdin is not accessible
|
||||||
if(~ $REQUEST_METHOD POST) {
|
if(~ $REQUEST_METHOD POST) {
|
||||||
load_post_args
|
load_post_args
|
||||||
login_user
|
login_user
|
||||||
}
|
}
|
||||||
|
|
||||||
if(! ~ $#args 0)
|
|
||||||
pageTitle=`{ echo $args|sed -e 's/ / - /g' -e 's/_/ /g' }
|
|
||||||
|
|
||||||
if(~ $req_path */index)
|
if(~ $req_path */index)
|
||||||
perm_redirect `{echo $req_path | sed 's,/index$,/,'}
|
perm_redirect `{echo $req_path | sed 's,/index$,/,'}
|
||||||
|
|
||||||
|
|
@ -173,7 +181,10 @@ if(~ $local_path */) {
|
||||||
perm_redirect `{echo $req_path|sed 's,/+$,,'}
|
perm_redirect `{echo $req_path|sed 's,/+$,,'}
|
||||||
}
|
}
|
||||||
if not if(test -d $local_path)
|
if not if(test -d $local_path)
|
||||||
perm_redirect $req_path^'/'
|
perm_redirect $base_url^$req_path^'/'
|
||||||
|
|
||||||
|
if(! ~ $#args 0)
|
||||||
|
pageTitle=`{ echo $args|sed -e 's/ / - /g' -e 's/_/ /g' }
|
||||||
|
|
||||||
cd $sitedir
|
cd $sitedir
|
||||||
req_paths_list='/' # Note: req_paths_list doesn't include 'stnythetic' dirs.
|
req_paths_list='/' # Note: req_paths_list doesn't include 'stnythetic' dirs.
|
||||||
|
|
@ -184,7 +195,7 @@ for(i in $args) {
|
||||||
conf_wd=$conf_wd^$i
|
conf_wd=$conf_wd^$i
|
||||||
req_paths_list=($req_paths_list $conf_wd)
|
req_paths_list=($req_paths_list $conf_wd)
|
||||||
if(test -d $i) {
|
if(test -d $i) {
|
||||||
conf_wd=$conf_wd^'/'
|
conf_wd=$conf_wd'/'
|
||||||
cd $i
|
cd $i
|
||||||
if(test -f _werc/config)
|
if(test -f _werc/config)
|
||||||
. _werc/config
|
. _werc/config
|
||||||
|
|
@ -192,27 +203,33 @@ for(i in $args) {
|
||||||
}
|
}
|
||||||
cd $werc_root
|
cd $werc_root
|
||||||
|
|
||||||
# Redirections and other preprocessing
|
f=();t=()
|
||||||
if(~ $#redirectPermanent 1) {
|
for(i in $perm_redir_patterns) {
|
||||||
perm_redirect $"redirectPermanent
|
if(~ $#f 0)
|
||||||
}
|
f=$i
|
||||||
if not if(~ $#redirectPermanent 2) {
|
if not {
|
||||||
from='http://'^$SERVER_NAME^$req_path
|
t=$i
|
||||||
to=`{echo $from|sed 's@'^$redirectPermanent(1)^'@'^$redirectPermanent(2)^'@'}
|
from=$base_url^$req_path
|
||||||
|
to=`{ echo $from | sed 's!'$f'!'$t'!' }
|
||||||
if(! ~ $to $from)
|
if(! ~ $to $from)
|
||||||
perm_redirect $to
|
perm_redirect $to
|
||||||
|
f=()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# Set Page title
|
# Set Page title
|
||||||
if(~ $pageTitle '')
|
if(~ $pageTitle '')
|
||||||
pageTitle=$siteTitle^' '^$siteSubTitle
|
pageTitle=$siteTitle' '$siteSubTitle
|
||||||
if not
|
if not
|
||||||
pageTitle=$"pageTitle^' | '^$"siteTitle^' '^$"siteSubTitle
|
pageTitle=$"pageTitle' | '$"siteTitle' '$"siteSubTitle
|
||||||
|
|
||||||
setup_handlers
|
setup_handlers
|
||||||
|
|
||||||
if(! ~ $#debug 0)
|
if(! ~ $#debug 0)
|
||||||
dprint ' '$"SERVER_NAME^$"REQUEST_URI' - '$"HTTP_USER_AGENT' - '$"REQUEST_METHOD' - '$"handler_body_main - $"master_template
|
dprint $"SERVER_NAME^$"REQUEST_URI - $"HTTP_USER_AGENT - $"REQUEST_METHOD - $"handler_body_main - $"master_template
|
||||||
|
|
||||||
template $headers $master_template | awk_buffer
|
template $headers $master_template | awk_buffer
|
||||||
echo $res_tail
|
echo $res_tail
|
||||||
|
}
|
||||||
|
|
||||||
|
werc_exec_request
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue