changelog shortlog tags branches files raw gz bz2 help

Mercurial > hg > werc / changeset: Various changes:

changeset 360: 279b02c9df5a
parent 359: 5ab742b6c3ee
child 361: 7255e7519b86
author: uriel@engel.se.cat-v.org
date: Sun, 25 Jan 2009 13:56:53 +0100
files: bin/cgilib.rc bin/werc.rc
description: 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.
     1.1--- a/bin/cgilib.rc	Sat Jan 24 07:22:43 2009 +0100
     1.2+++ b/bin/cgilib.rc	Sun Jan 25 13:56:53 2009 +0100
     1.3@@ -9,13 +9,15 @@
     1.4 
     1.5 fn escape_html { sed 's/&/\&amp;/g; s/</\&lt;/g; s/>/\&gt;/g' $* }
     1.6 
     1.7-fn perm_redirect {
     1.8-    echo 'Status: 301 Moved Permanantly
     1.9+fn http_redirect {
    1.10+    echo 'Status: '^$2^'
    1.11 Location: '^$1^'
    1.12 
    1.13 '
    1.14     exit
    1.15 }
    1.16+fn perm_redirect { http_redirect $1 '301 Moved Permanantly' }
    1.17+fn post_redirect { http_redirect $1 '303 See Other' }
    1.18 
    1.19 fn static_file {
    1.20     echo 'Content-Type: '`{select_mime $1}
    1.21@@ -33,7 +35,7 @@
    1.22             ifs='=' { pair=`{echo -n $pair} }
    1.23             n='post_arg_'^`{echo $pair(1)|tr -cd 'a-zA-Z0-9_'}
    1.24             post_args=( $post_args $n )
    1.25-            $n=`{echo -n $pair(2) | urldecode}
    1.26+            ifs=() { $n=`{echo -n $pair(2)|urldecode|tr -d '
'} }
    1.27         }
    1.28         pair=()
    1.29     }
    1.30@@ -171,6 +173,19 @@
    1.31 ##############################################
    1.32 # Werc-specific functions
    1.33 
    1.34+fn get_lib_file {
    1.35+    if(! ~ $#sitedir 0 && test -f $sitedir/_werc/lib/$1)
    1.36+        echo -n $sitedir/_werc/lib/$1
    1.37+    if not if(! ~ $#masterSite 0 && test -f $sitesdir/$masterSite/_werc/lib/$1)
    1.38+        echo -n $sitesdir/$masterSite/_werc/lib/$1
    1.39+    if not if(test -f lib/$1)
    1.40+        echo -n lib/$1
    1.41+    if not if(~ $#* 2)
    1.42+        echo -n $2
    1.43+    if not
    1.44+        status='Can''t find lib file: '$1
    1.45+}
    1.46+
    1.47 fn template { awk -f bin/template.awk $* | rc $rcargs }
    1.48 
    1.49 # Auth code
    1.50@@ -185,10 +200,14 @@
    1.51 
    1.52 # Check loggin status, if called with group arg we check membership too
    1.53 fn check_user {
    1.54-    if(! get_user)
    1.55-        _status='Not logged in:' $status
    1.56-    if not if(! ~ $#* 0 && ! grep -s '^'^$logged_user^'$' etc/groups/$*)
    1.57-        _status=User $logged_user not in groups $*
    1.58+    get_user
    1.59+    _status=$status
    1.60+    if(! ~ $#_status 0 )
    1.61+        _status=(Not logged in: $"_status)
    1.62+    if not if(! ~ $#* 0 && ! grep -s '^'^$logged_user^'$' etc/groups/$*) {
    1.63+        dprint NOT IN GROUP
    1.64+        _status=(User $logged_user not in groups $*)
    1.65+    }
    1.66     status=$_status
    1.67 }
    1.68 
    1.69@@ -223,11 +242,11 @@
    1.70 
    1.71     pfile='etc/users/'^$"user_name^'/password'
    1.72     if(~ $#user_name 0 || ~ $#user_password 0)
    1.73-        status='Auth: missing user name or pass: '^$"user_name^' / '^$"user_password
    1.74+        status=('Auth: missing user name or pass: '^$"user_name^' / '^$"user_password)
    1.75     if not if(! test -f $pfile)
    1.76-        status='Auth: cant find '^$pfile
    1.77+        status=('Auth: cant find '^$pfile)
    1.78     if not if(! ~ $user_password `{cat $pfile})
    1.79-        status='Auth: Pass '$user_password' doesnt match '^`{cat $pfile}
    1.80+        status=('Auth: Pass '$user_password' doesnt match '^`{cat $pfile})
    1.81     if not {
    1.82         logged_user=$user_name
    1.83         logged_password=$user_password
     2.1--- a/bin/werc.rc	Sat Jan 24 07:22:43 2009 +0100
     2.2+++ b/bin/werc.rc	Sun Jan 25 13:56:53 2009 +0100
     2.3@@ -4,15 +4,6 @@
     2.4 
     2.5 forbidden_uri_chars='[^a-zA-Z0-9_+\-\/\.]'
     2.6 
     2.7-fn get_lib_file {
     2.8-    if(test -f $sitedir/_werc/lib/$1)
     2.9-        echo -n $sitedir/_werc/lib/$1
    2.10-    if not if(! ~ $#masterSite 0 && test -f $sitesdir/$masterSite/_werc/lib/$1)
    2.11-        echo -n $sitesdir/$masterSite/_werc/lib/$1
    2.12-    if not if(test -f lib/$1)
    2.13-        echo -n lib/$1
    2.14-}
    2.15-
    2.16 # Expected input: ls -F style, $sitedir/path/to/files/
    2.17 #          <ls -F+x><symlink hack><Useless?><hiden files  >
    2.18 dirfilter='s/\*$//; s,/+\./+,/,g; s,^\./,,; /\/[._][^\/]/d; /'^$forbidden_uri_chars^'/d; /^\/(robots|sitemap)\.txt$|\/index\.(md|html|txt|tpl)$/d; /_werc\/?$/d; '
    2.19@@ -132,19 +123,19 @@
    2.20 path=(. $PLAN9/bin ./bin/ /bin/ /usr/bin) 
    2.21 
    2.22 headers=lib/headers.tpl
    2.23-master_template=default_master.tpl
    2.24 res_tail='</body></html>'
    2.25 ll_add handlers_bar_left nav_tree
    2.26 werc_apps=( apps/* )
    2.27 werc_root=`{pwd}
    2.28+sitesdir=sites
    2.29 for(i in siteTitle siteSubTitle pageTitle extraHeaders)
    2.30     $i = ''
    2.31 
    2.32 # TODO: Per-req variables should move after initrc loading.
    2.33 site=$SERVER_NAME
    2.34 base_url=http://$site/
    2.35-sitesdir=sites
    2.36 sitedir=$sitesdir/$site
    2.37+master_template=`{get_lib_file default_master.tpl}
    2.38 current_date_time=`{date}
    2.39 
    2.40 . ./etc/initrc
    2.41@@ -219,7 +210,7 @@
    2.42 setup_handlers
    2.43 
    2.44 if(! ~ $#debug 0)
    2.45-    dprint '  '$"SERVER_NAME^$"REQUEST_URI' - '$"HTTP_USER_AGENT' - '$"REQUEST_METHOD' - '$"handler_body_main
    2.46+    dprint '  '$"SERVER_NAME^$"REQUEST_URI' - '$"HTTP_USER_AGENT' - '$"REQUEST_METHOD' - '$"handler_body_main - $"master_template
    2.47 
    2.48-template $headers `{get_lib_file $master_template} | awk_buffer
    2.49+template $headers $master_template | awk_buffer
    2.50 echo $res_tail