shithub: werc

Download patch

ref: ebebd4107273b27a4dc188a57bbb3534b57e64fa
parent: 73455a9069c38ce7bf503c5a04ea2926ef7d2a91
author: uriel <uriel@engel.se.cat-v.org>
date: Sun Jan 25 08:56:53 EST 2009

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.

--- a/bin/cgilib.rc
+++ b/bin/cgilib.rc
@@ -9,13 +9,15 @@
 
 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 @@
             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 @@
 ##############################################
 # 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 @@
 
 # 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 @@
 
     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
--- a/bin/werc.rc
+++ b/bin/werc.rc
@@ -4,15 +4,6 @@
 
 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,11 +123,11 @@
 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 = ''
 
@@ -143,8 +134,8 @@
 # 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 @@
 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