From 107aeaef6e8b3395af41a2cf4da69c71aa2230db Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Wed, 7 Jun 2023 15:26:05 -0500 Subject: Add hcat --- cacophony.sh | 58 ++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 20 deletions(-) diff --git a/cacophony.sh b/cacophony.sh index f893a60..92a5d4b 100755 --- a/cacophony.sh +++ b/cacophony.sh @@ -1,5 +1,5 @@ #!/bin/sh -# cacophony by C. Duckworth --- public domain +# cacophony by C. Duckworth --- hereby released to the public domain SCRIPTDIR=source @@ -8,41 +8,59 @@ crlf() { # crlf < INPUT sed 's/$/ /g' } -http_header() { # header [STATUS] +log() { # log MESSAGE ... + printf >&2 '%s\n' "$*" +} + +http_header() { # http_header [STATUS] [MIMETYPE] [CHARSET] cat <
-copyright (C) $(stat -c %y "$1" | cut -d- -f1) Case Duckworth. -this page is a program +html_footer() { # html_footer + cat < - -EOF + +FOOTER } -hexec() { # hexec PROGRAM [header args] - prog="$1"; shift +hcat() { # hcat [STATUS] < INPUT + case "$(file -)" in + *HTML*) http_header "{1:-200}" ;; + *) http_header "${1:-200}" text/plain ;; # todo: smarter + esac + cat +} + +hexec() { # hexec PROGRAM [http_header args] + prog="$1" + shift http_header "$@" "$prog" - html_footer "$prog" + html_footer } run() { # run + pag="$PWD$PATH_INFO" + src="$PWD/$SCRIPTDIR$PATH_INFO" + if test / = "$PATH_INFO" then PATH_INFO=/index run - else - path="$PWD/$SCRIPTDIR$PATH_INFO" - if ! test -d "$path" && test -x "$path" - then hexec "$path" 200 - else http_header 404 - fi + + elif ! test -d "$pag" && test -x "$src" + then hexec "$src" 200 + + elif ! test -d "$pag" && test -r "$pag" + then hcat 200 < "$pag" + + elif test -d "$pag" && test -r "$pag/index.html" + then hcat 200 < "$pag/index.html" + + else http_header 404 fi } -- cgit 1.4.1-21-gabe81