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
This commit is contained in:
Uriel 2008-07-16 03:05:41 +02:00
parent 9723047b26
commit 38f3ab815b

View file

@ -17,9 +17,14 @@ body=index
template=_default template=_default
sidebar=sidebar sidebar=sidebar
baseuri=http://$site/ 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) for(i in siteTitle siteSubTitle title extraHeaders)
$i = '' $i = ''
fn dprint {
echo $* >[1=2]
}
# Title # Title
fn gentitle { fn gentitle {
echo '<h1 class="headerTitle"><a href="/">' ^ $"siteTitle ^ ' <span id="headerSubTitle">' ^ $"siteSubTitle ^ '</span></a></h1>' echo '<h1 class="headerTitle"><a href="/">' ^ $"siteTitle ^ ' <span id="headerSubTitle">' ^ $"siteSubTitle ^ '</span></a></h1>'
@ -92,46 +97,57 @@ fn blogTitle {
echo '##<a href="' $"permlink '">' $"title^'</a> *('By $du(4) Last mod: $du(7 8 9) ')*' echo '##<a href="' $"permlink '">' $"title^'</a> *('By $du(4) Last mod: $du(7 8 9) ')*'
} }
# Body
fn genbody {
if (test -f $body.md) {
if (! ~ $#inBlog 0) # Handlers
blogTitle $body.md | $formatter fn set_handler {
$formatter < $body.md dprint XXX $*
handler = $1
shift
handler_args = $*
} }
if not if (test -f $body.tpl)
template.awk $body.tpl | rc $rcargs fn md_handler {
if not if (test -f $body.html) cat $* | $formatter
cat $body.html | /bin/sed '0,/<[Bb][Oo][Dd][Yy][^>]*>/d; /<\/[Bb][Oo][Dd][Yy]>/,$d' }
if not if (~ $body *.html && test -f $body)
cat $body | /bin/sed -i '0,/<body[^>]*>/d;/<\/body>/,$d' # This branch is never taken? fn tpl_handler {
if not if (~ $body */[bB]log/index */[bB]log//index && ~ $#blogDirs 0) template.awk $1 | rc $rcargs
blogDirs = `{basename -d $body} }
if not if (test -f pub/^$reqpath^.tpl)
template.awk pub/^$reqpath^.tpl | rc $rcargs fn html_handler {
if not if (test -f $body.txt) { cat $1 | /bin/sed '0,/<[Bb][Oo][Dd][Yy][^>]*>/d; /<\/[Bb][Oo][Dd][Yy]>/,$d'
}
fn txt_handler {
echo '<pre>' echo '<pre>'
# XXX Insering a blank line between lines in input is good for fortunes.txt, but probably not for other .txt files # 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 # XXX Words are not broken, even if they are way beyond 82 chars long
cat $body.txt |sed 's/$/\n/g; s/</\&gt;/g; s/>/\&lt;/g' |fmt -l 82 -j cat $1 |sed 's/$/\n/g; s/</\&gt;/g; s/>/\&lt;/g' |fmt -l 82 -j
echo '</pre>' echo '</pre>'
} }
if not if(~ $body */index && ~ $#blogDirs 0) {
fn dir_listing_handler {
body = $1
echo '<h1 style="text-transform: capitalize;">' `{basename -d $body|sed -e 's,.*//,,g' -e 's,/$,,' -e 's,/, / ,g' } '</h1>' echo '<h1 style="text-transform: capitalize;">' `{basename -d $body|sed -e 's,.*//,,g' -e 's,/$,,' -e 's,/, / ,g' } '</h1>'
echo '<ul style="text-transform: capitalize;">' echo '<ul style="text-transform: capitalize;">'
ls -F `{ basename -d $body } | sed -e $dirfilter' s,^'$sitedir'/.*/([^$].*),<li><a href="\1">\1</a></li>,' ls -F `{ basename -d $body } | sed -e $dirfilter' s,^'$sitedir'/.*/([^$].*),<li><a href="\1">\1</a></li>,'
echo '</ul>' echo '</ul>'
} }
if not if(~ $#blogDirs 0) {
#echo 'Status: 404 Not Found\n\n' # should go before starting to print body fn 404_handler {
template.awk inc/404.tpl | rc $rcargs template.awk inc/404.tpl | rc $rcargs
dprint 'NOT FOUND: '$SERVER_NAME^$REQUEST_URI^' - '^$"HTTP_REFERER^' - '^$"HTTP_USER_AGENT
} }
if(! ~ $#blogDirs 0) { fn blog_dir_handler {
blogDirs = $*
if (! ~ $blogTitle '') if (! ~ $blogTitle '')
echo '<h1>'$"blogTitle'</h1>' echo '<h1>'$"blogTitle'</h1>' #" stupid vim syntax highlighting ;P
echo '<div style="text-align:right">(<a href="index.rss">rss feed</a>)</div>' echo '<div style="text-align:right">(<a href="index.rss">rss feed</a>)</div>'
for (f in `{ sortedBlogPostList $blogDirs }) { for (f in `{ sortedBlogPostList $blogDirs }) {
#title=`{basename $f | sed 's/^[0-9\-]*_(.*)\.md$/\1/; s/_/ /g' } #title=`{basename $f | sed 's/^[0-9\-]*_(.*)\.md$/\1/; s/_/ /g' }
#du=`{ls -l $f} #du=`{ls -l $f}
@ -141,14 +157,77 @@ fn genbody {
echo echo
} | $formatter } | $formatter
} }
fn blog_post_handler {
blogTitle $1 | $formatter
$formatter < $1
}
fn quote_html {
sed 's/</\&lt;/g; s/>/\&gt;/g'
}
fn debug_handler {
echo '<pre>'
env |quote_html
echo '</pre>'
} }
fn select_handler {
if (test -f $body.md) {
if (! ~ $#inBlog 0)
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)
set_handler tpl_handler $body.tpl
if not if (test -f $body.html)
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)
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)
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) {
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)
set_handler blog_dir_handler $blogDirs
}
fn genbody {
# Actually execute request
$handler $handler_args
}
. etc/initrc . etc/initrc
fn dprint {
echo $* >[1=2]
}
if(! ~ $#debug 0) if(! ~ $#debug 0)
dprint $SERVER_NAME^'/'^$REQUEST_URI^' - '^$"HTTP_USER_AGENT dprint $SERVER_NAME^'/'^$REQUEST_URI^' - '^$"HTTP_USER_AGENT
@ -203,7 +282,6 @@ template=$sitedir/$template.tpl
if (! ~ $#sidebar 0) if (! ~ $#sidebar 0)
sidebar=tpl/_inc/$sidebar.tpl sidebar=tpl/_inc/$sidebar.tpl
reqpath=$body
body=$sitedir/$body body=$sitedir/$body
rssuri=$uri rssuri=$uri
if (test -d $body) { if (test -d $body) {
@ -222,6 +300,8 @@ if(! ~ $#blogDirs 0 || ! ~ $#inBlog 0) {
} }
select_handler
fn template { fn template {
template.awk | rc $rcargs | template.awk | rc $rcargs |
awk '{ awk '{