diff --git a/bin/cgilib.rc b/bin/cgilib.rc index 156dc92..f5c182e 100644 --- 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/&/\&/g; s//\>/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 index 9e992ce..7187a4d 100755 --- 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/ # 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='' 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