From 167b1ba0671be6297d3d085fcf8957c32331e0f4 Mon Sep 17 00:00:00 2001 From: uriel Date: Wed, 16 Jul 2008 03:05:41 +0200 Subject: [PATCH] Big code reorg: split request handling code into two steps: 1) handler selection 2) handler execution This allows for real 404 errors and in the future more cleanups and features --- bin/controller.rc | 162 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 121 insertions(+), 41 deletions(-) diff --git a/bin/controller.rc b/bin/controller.rc index 1420afe..0b392ab 100755 --- a/bin/controller.rc +++ b/bin/controller.rc @@ -17,9 +17,14 @@ body=index template=_default sidebar=sidebar baseuri=http://$site/ +reqpath=$body # Maybe this is not needed anymore now that the handlers are selected before anybody can mess with $body for(i in siteTitle siteSubTitle title extraHeaders) $i = '' +fn dprint { + echo $* >[1=2] +} + # Title fn gentitle { echo '

' ^ $"siteTitle ^ ' ' ^ $"siteSubTitle ^ '

' @@ -92,63 +97,137 @@ fn blogTitle { echo '##' $"title^' *('By $du(4) Last mod: $du(7 8 9) ')*' } -# Body -fn genbody { + + + +# Handlers +fn set_handler { + dprint XXX $* + handler = $1 + shift + handler_args = $* +} + +fn md_handler { + cat $* | $formatter +} + +fn tpl_handler { + template.awk $1 | rc $rcargs +} + +fn html_handler { + cat $1 | /bin/sed '0,/<[Bb][Oo][Dd][Yy][^>]*>/d; /<\/[Bb][Oo][Dd][Yy]>/,$d' +} + +fn txt_handler { + echo '
'
+    # XXX Insering a blank line between lines in input is good for fortunes.txt, but maybe not for other .txt files
+    # XXX Words are not broken, even if they are way beyond 82 chars long
+    cat $1 |sed 's/$/\n/g; s//\</g' |fmt -l 82 -j
+    echo '
' +} + +fn dir_listing_handler { + body = $1 + echo '

' `{basename -d $body|sed -e 's,.*//,,g' -e 's,/$,,' -e 's,/, / ,g' } '

' + echo '' +} + +fn 404_handler { + template.awk inc/404.tpl | rc $rcargs +} + +fn blog_dir_handler { + blogDirs = $* + + if (! ~ $blogTitle '') + echo '

'$"blogTitle'

' #" stupid vim syntax highlighting ;P + + echo '
(rss feed)
' + + for (f in `{ sortedBlogPostList $blogDirs }) { + #title=`{basename $f | sed 's/^[0-9\-]*_(.*)\.md$/\1/; s/_/ /g' } + #du=`{ls -l $f} + #echo '##' $title '*('By $du(4) Last mod: $du(7 8 9) ')*' + blogTitle $f + cat $f + echo + } | $formatter +} + +fn blog_post_handler { + + blogTitle $1 | $formatter + $formatter < $1 +} + +fn quote_html { + sed 's//\>/g' +} +fn debug_handler { + echo '
'
+    env |quote_html
+    echo '
' +} + + +fn select_handler { if (test -f $body.md) { if (! ~ $#inBlog 0) - blogTitle $body.md | $formatter - $formatter < $body.md + set_handler blog_post_handler $body.md + if not + set_handler md_handler $body.md } + if not if (~ $body */_debug) + set_handler debug_handler if not if (test -f $body.tpl) - template.awk $body.tpl | rc $rcargs + set_handler tpl_handler $body.tpl + if not if (test -f $body.html) - cat $body.html | /bin/sed '0,/<[Bb][Oo][Dd][Yy][^>]*>/d; /<\/[Bb][Oo][Dd][Yy]>/,$d' + set_handler html_handler $body.html + + # Handle eplicit .html urls, this should not happen (the web server will usually handle this anyway) + # XXX We probably should setup a permanent redirect to $body|sed 's/.html$//' here if not if (~ $body *.html && test -f $body) - cat $body | /bin/sed -i '0,/]*>/d;/<\/body>/,$d' # This branch is never taken? + set_handler html_handler $body + + # This should probably be merged with the blog_dir_handler if not if (~ $body */[bB]log/index */[bB]log//index && ~ $#blogDirs 0) blogDirs = `{basename -d $body} + + # Global tpl (eg sitemap.tpl) if not if (test -f pub/^$reqpath^.tpl) - template.awk pub/^$reqpath^.tpl | rc $rcargs - if not if (test -f $body.txt) { - echo '
'
-	# XXX Insering a blank line between lines in input is good for fortunes.txt, but probably not for other .txt files
-	# XXX Words are not broken, even if they are way beyond 82 chars long
-        cat $body.txt |sed 's/$/\n/g; s//\</g' |fmt -l 82 -j
-        echo '
' - } - if not if(~ $body */index && ~ $#blogDirs 0) { - echo '

' `{basename -d $body|sed -e 's,.*//,,g' -e 's,/$,,' -e 's,/, / ,g' } '

' - echo '' - } + set_handler tpl_handler pub/^$reqpath^.tpl + + if not if (test -f $body.txt) + set_handler txt_handler $body.txt + + # Dir listing + if not if(~ $body */index && ~ $#blogDirs 0) + set_handler dir_listing_handler $body + + # File not found if not if(~ $#blogDirs 0) { - #echo 'Status: 404 Not Found\n\n' # should go before starting to print body - template.awk inc/404.tpl | rc $rcargs - dprint 'NOT FOUND: '$SERVER_NAME^$REQUEST_URI^' - '^$"HTTP_REFERER^' - '^$"HTTP_USER_AGENT + set_handler 404_handler $body + dprint 'NOT FOUND: '$SERVER_NAME^$REQUEST_URI^' - '^$"HTTP_REFERER^' - '^$"HTTP_USER_AGENT + echo 'Status: 404 Not Found\n\n' } - if(! ~ $#blogDirs 0) { - if (! ~ $blogTitle '') - echo '

'$"blogTitle'

' - echo '
(rss feed)
' - for (f in `{ sortedBlogPostList $blogDirs }) { - #title=`{basename $f | sed 's/^[0-9\-]*_(.*)\.md$/\1/; s/_/ /g' } - #du=`{ls -l $f} - #echo '##' $title '*('By $du(4) Last mod: $du(7 8 9) ')*' - blogTitle $f - cat $f - echo - } | $formatter - } + if(! ~ $#blogDirs 0) + set_handler blog_dir_handler $blogDirs } + +fn genbody { + # Actually execute request + $handler $handler_args +} . etc/initrc -fn dprint { - echo $* >[1=2] -} if(! ~ $#debug 0) dprint $SERVER_NAME^'/'^$REQUEST_URI^' - '^$"HTTP_USER_AGENT @@ -203,7 +282,6 @@ template=$sitedir/$template.tpl if (! ~ $#sidebar 0) sidebar=tpl/_inc/$sidebar.tpl -reqpath=$body body=$sitedir/$body rssuri=$uri if (test -d $body) { @@ -222,6 +300,8 @@ if(! ~ $#blogDirs 0 || ! ~ $#inBlog 0) { } +select_handler + fn template { template.awk | rc $rcargs | awk '{