swerc

anselm's simpler werc fork
git clone git://git.suckless.org/swerc
Log | Files | Refs | README

commit 5c39801466fb1bb6a0e94913465801f08a213072
parent f6ce1c8d3bb2df527691bd3c353202f9bfc9cbfe
Author: uriel@engel.se.cat-v.org <unknown>
Date:   Sun, 25 Jan 2009 13:56:53 +0100

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.
Diffstat:
Mbin/cgilib.rc | 39+++++++++++++++++++++++++++++----------
Mbin/werc.rc | 17++++-------------
2 files changed, 33 insertions(+), 23 deletions(-)

diff --git a/bin/cgilib.rc b/bin/cgilib.rc @@ -9,13 +9,15 @@ fn dprintvars { { for(v in $*) { echo -n $v^'#'^$#$v^'=' $$v '; ' }; echo } >[1 fn escape_html { sed 's/&/\&amp;/g; s/</\&lt;/g; s/>/\&gt;/g' $* } -fn perm_redirect { - echo 'Status: 301 Moved Permanantly +fn http_redirect { + echo 'Status: '^$2^' Location: '^$1^' ' exit } +fn perm_redirect { http_redirect $1 '301 Moved Permanantly' } +fn post_redirect { http_redirect $1 '303 See Other' } fn static_file { echo 'Content-Type: '`{select_mime $1} @@ -33,7 +35,7 @@ fn load_post_args { ifs='=' { pair=`{echo -n $pair} } n='post_arg_'^`{echo $pair(1)|tr -cd 'a-zA-Z0-9_'} post_args=( $post_args $n ) - $n=`{echo -n $pair(2) | urldecode} + ifs=() { $n=`{echo -n $pair(2)|urldecode|tr -d ' '} } } pair=() } @@ -171,6 +173,19 @@ fn ll_add { ############################################## # Werc-specific functions +fn get_lib_file { + if(! ~ $#sitedir 0 && test -f $sitedir/_werc/lib/$1) + echo -n $sitedir/_werc/lib/$1 + if not if(! ~ $#masterSite 0 && test -f $sitesdir/$masterSite/_werc/lib/$1) + echo -n $sitesdir/$masterSite/_werc/lib/$1 + if not if(test -f lib/$1) + echo -n lib/$1 + if not if(~ $#* 2) + echo -n $2 + if not + status='Can''t find lib file: '$1 +} + fn template { awk -f bin/template.awk $* | rc $rcargs } # Auth code @@ -185,10 +200,14 @@ fn login_user { # Check loggin status, if called with group arg we check membership too fn check_user { - if(! get_user) - _status='Not logged in:' $status - if not if(! ~ $#* 0 && ! grep -s '^'^$logged_user^'$' etc/groups/$*) - _status=User $logged_user not in groups $* + get_user + _status=$status + if(! ~ $#_status 0 ) + _status=(Not logged in: $"_status) + if not if(! ~ $#* 0 && ! grep -s '^'^$logged_user^'$' etc/groups/$*) { + dprint NOT IN GROUP + _status=(User $logged_user not in groups $*) + } status=$_status } @@ -223,11 +242,11 @@ fn auth_user { pfile='etc/users/'^$"user_name^'/password' if(~ $#user_name 0 || ~ $#user_password 0) - status='Auth: missing user name or pass: '^$"user_name^' / '^$"user_password + status=('Auth: missing user name or pass: '^$"user_name^' / '^$"user_password) if not if(! test -f $pfile) - status='Auth: cant find '^$pfile + status=('Auth: cant find '^$pfile) if not if(! ~ $user_password `{cat $pfile}) - status='Auth: Pass '$user_password' doesnt match '^`{cat $pfile} + status=('Auth: Pass '$user_password' doesnt match '^`{cat $pfile}) if not { logged_user=$user_name logged_password=$user_password diff --git a/bin/werc.rc b/bin/werc.rc @@ -4,15 +4,6 @@ cd .. forbidden_uri_chars='[^a-zA-Z0-9_+\-\/\.]' -fn get_lib_file { - if(test -f $sitedir/_werc/lib/$1) - echo -n $sitedir/_werc/lib/$1 - if not if(! ~ $#masterSite 0 && test -f $sitesdir/$masterSite/_werc/lib/$1) - echo -n $sitesdir/$masterSite/_werc/lib/$1 - if not if(test -f lib/$1) - echo -n lib/$1 -} - # Expected input: ls -F style, $sitedir/path/to/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; ' @@ -132,19 +123,19 @@ fn run_handler { $*(1) $*(2-) } path=(. $PLAN9/bin ./bin/ /bin/ /usr/bin) headers=lib/headers.tpl -master_template=default_master.tpl res_tail='</body></html>' ll_add handlers_bar_left nav_tree werc_apps=( apps/* ) werc_root=`{pwd} +sitesdir=sites for(i in siteTitle siteSubTitle pageTitle extraHeaders) $i = '' # TODO: Per-req variables should move after initrc loading. site=$SERVER_NAME base_url=http://$site/ -sitesdir=sites sitedir=$sitesdir/$site +master_template=`{get_lib_file default_master.tpl} current_date_time=`{date} . ./etc/initrc @@ -219,7 +210,7 @@ if not setup_handlers if(! ~ $#debug 0) - dprint ' '$"SERVER_NAME^$"REQUEST_URI' - '$"HTTP_USER_AGENT' - '$"REQUEST_METHOD' - '$"handler_body_main + dprint ' '$"SERVER_NAME^$"REQUEST_URI' - '$"HTTP_USER_AGENT' - '$"REQUEST_METHOD' - '$"handler_body_main - $"master_template -template $headers `{get_lib_file $master_template} | awk_buffer +template $headers $master_template | awk_buffer echo $res_tail