werc-1.5.0-tweaks

Tweaks for the werc website builder created by the mad architect Uriel
Log | Files | Refs | README

werc.rc (4382B)


      1 #!/usr/lib/plan9/bin/rc
      2 . ./cgilib.rc
      3 . ./werclib.rc
      4 . ./wercconf.rc
      5 . ./corehandlers.rc
      6 . ./fltr_cache.rc
      7 cd ..
      8 
      9 forbidden_uri_chars='[^a-zA-Z0-9_+\-\/\.,:]'
     10 difs=$ifs # Used to restore default ifs when needed 
     11 
     12 # Expected input: ls -F style, $sitedir/path/to/files/
     13 #          <ls -F+x><symlink hack><Useless?><hiden files  >
     14 dirfilter='s/\*$//; s,/+\./+,/,g; s,^\./,,; /\/[._][^\/]/d; /'$forbidden_uri_chars'/d; /\/sitemap\.xml$/d; /\/index\.(md|html|txt|tpl)$/d; /\/(robots|sitemap)\.txt$/d; /_werc\/?$/d; '
     15 dirclean=' s/\.(md|html|txt)$//; '
     16 
     17 # Careful, the proper p9p path might not be set until initrc.local is sourced
     18 path=(. /bin ./bin)
     19 
     20 res_tail='</body></html>'
     21 http_content_type='text/html'
     22 ll_add handlers_bar_left nav_tree
     23 werc_apps=( apps/* )
     24 werc_root=`{pwd}
     25 sitesdir=sites
     26 
     27  . ./etc/initrc
     28 
     29 if(test -f etc/initrc.local)
     30     . ./etc/initrc.local
     31 
     32 for(a in $werc_apps)
     33     . ./$a/app.rc
     34 
     35 fn werc_exec_request {
     36     site=$SERVER_NAME
     37     base_url=https://$site
     38     # base_url=https://$site:$SERVER_PORT
     39     #  ^ causes malformed rss to be generated
     40     #  could be useful for debugging, but
     41     #  otherwise not worth it
     42     sitedir=$sitesdir/$site
     43     req_path=`{echo -n $REQUEST_URI | sed 's/\?.*//; s!//+!/!g; s/%5[Ff]/_/g; s/'^$forbidden_uri_chars^'//g; s/\.\.*/./g; 1q'}
     44     req_url=$base_url^$req_path
     45     local_path=$sitedir$req_path
     46     local_file=''
     47     ifs='/' { args=`{echo -n $req_path} }
     48     
     49     headers=`{get_lib_file headers.tpl}
     50     master_template=`{get_lib_file default_master.tpl}
     51     current_date_time=`{date}
     52 
     53     # Note: $REQUEST_URI is not officially in CGI 1.1, but seems to be de-facto
     54     # Note: We only urldecode %5F->'_' because some sites (stackoverflow.com?) urlencode it in their links,
     55     # perhaps we should completel urldecode the whole url.
     56     
     57     # Preload post args for templates where cgi's stdin is not accessible
     58     if(~ $REQUEST_METHOD POST) {
     59         load_post_args
     60         login_user
     61     }
     62 
     63     if(~ $req_path */index)
     64         perm_redirect `{echo $req_path | sed 's,/index$,/,'}
     65 
     66     if(~ $local_path */) {
     67         if(test -d $local_path)
     68             local_path=$local_path^'index'
     69         # XXX: This redir might step on apps with synthetic dirs.
     70         if not if(ls `{basename -d $local_path}^* >/dev/null >[2]/dev/null)
     71             perm_redirect `{echo $req_path|sed 's,/+$,,'}
     72     }
     73     if not if(~ $req_path *'.' *',' *';' *':')
     74         perm_redirect `{echo $req_path | sed 's/[.,;:)]$//'}
     75     if not if(test -d $local_path)
     76         perm_redirect $req_path^'/'
     77 
     78     if(! ~ $#args 0)
     79         ifs=$NEW_LINE { pageTitle=`{ echo $args|sed -e 's/ / - /g' -e 's/([a-z])-([a-z])/\1 \2/g' -e 's/_/ /g' } }
     80 
     81     cd $sitedir
     82     req_paths_list='/' # Note: req_paths_list doesn't include 'stnythetic' dirs.
     83     conf_wd='/' # Used in config files to know where we are in the document tree.
     84     if(test -f _werc/config)
     85         . _werc/config
     86     for(i in $args) {
     87         conf_wd=$conf_wd^$i
     88         req_paths_list=($req_paths_list $conf_wd)
     89         if(test -d $i) {
     90             conf_wd=$conf_wd'/'
     91             cd $i
     92             if(test -f _werc/config)
     93                 . _werc/config
     94         }
     95     }
     96     cd $werc_root
     97 
     98     if(~ $#perm_redir_to 1)
     99         perm_redirect $perm_redir_to
    100     for(l in $perm_redir_patterns) {
    101         p=$$l
    102         r=$p(1)
    103         # If target is absolute, then patern must match whole string
    104         if(~ $p(2) http://* https://*)
    105             r='^'$r 
    106         t=`{ echo $req_path | sed 's!'^$r^'!'^$p(2)^'!' } # Malicious danger!
    107 
    108         if(! ~ $"t '' $req_path)
    109             perm_redirect $t
    110     }
    111 
    112     setup_handlers
    113 
    114 
    115     # Set Page title
    116     if(! ~ $local_file '') {
    117         t=`{get_file_title $local_file}
    118         if(! ~ $"t '')
    119             pageTitle=$t
    120     }
    121 
    122     # XXX Is this never true? because we set pageTitle earlier based on url.
    123     if(~ $"pageTitle '') 
    124         pageTitle=$"siteTitle' '$"siteSubTitle
    125 #    if not
    126 #        pageTitle=$"pageTitle' | '$"siteTitle' '$"siteSubTitle
    127 
    128     for(h in $extraHttpHeaders)
    129         echo $h
    130     echo Content-Type: $http_content_type
    131     echo # End of HTTP headers
    132 
    133     if(! ~ $#debug 0)
    134 		dprint $"SERVER_NAME^$"REQUEST_URI - $"HTTP_USER_AGENT - $"REQUEST_METHOD - $"handler_body_main - $"master_template
    135 
    136     if(~ $REQUEST_METHOD HEAD)
    137         exit
    138 
    139     template $headers $master_template # | awk_buffer
    140     echo $res_tail
    141 }
    142 
    143 werc_exec_request