about summary refs log tree commit diff stats
path: root/cacophony.sh
diff options
context:
space:
mode:
Diffstat (limited to 'cacophony.sh')
-rwxr-xr-xcacophony.sh58
1 files 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 @@
1#!/bin/sh 1#!/bin/sh
2# cacophony by C. Duckworth --- public domain 2# cacophony by C. Duckworth --- hereby released to the public domain
3 3
4SCRIPTDIR=source 4SCRIPTDIR=source
5 5
@@ -8,41 +8,59 @@ crlf() { # crlf < INPUT
8 sed 's/$/ /g' 8 sed 's/$/ /g'
9} 9}
10 10
11http_header() { # header [STATUS] 11log() { # log MESSAGE ...
12 printf >&2 '%s\n' "$*"
13}
14
15http_header() { # http_header [STATUS] [MIMETYPE] [CHARSET]
12 cat <<HEADER | crlf 16 cat <<HEADER | crlf
13HTTP/1.1 ${1:-200} 17HTTP/1.1 ${1:-200}
14Content-Type: text/html;charset=utf-8 18Content-Type: ${2:-text/html};charset=${3:-utf-8}
15 19
16HEADER 20HEADER
17} 21}
18 22
19html_footer() { # footer PROGRAM 23html_footer() { # html_footer
20 cat <<EOF 24 cat <<FOOTER
21<footer> 25<footer>this page is a <a href="$SCRIPTDIR/${1##*/}">program</a>
22copyright (C) $(stat -c %y "$1" | cut -d- -f1) Case Duckworth.
23this page is a <a href="$SCRIPTDIR/${1##*/}">program</a>
24served by <a href="https://git.acdw.net/cacophony">cacophony</a>. 26served by <a href="https://git.acdw.net/cacophony">cacophony</a>.
25</footer> 27</footer></body></html>
26</body></html> 28FOOTER
27EOF
28} 29}
29 30
30hexec() { # hexec PROGRAM [header args] 31hcat() { # hcat [STATUS] < INPUT
31 prog="$1"; shift 32 case "$(file -)" in
33 *HTML*) http_header "{1:-200}" ;;
34 *) http_header "${1:-200}" text/plain ;; # todo: smarter
35 esac
36 cat
37}
38
39hexec() { # hexec PROGRAM [http_header args]
40 prog="$1"
41 shift
32 http_header "$@" 42 http_header "$@"
33 "$prog" 43 "$prog"
34 html_footer "$prog" 44 html_footer
35} 45}
36 46
37run() { # run 47run() { # run
48 pag="$PWD$PATH_INFO"
49 src="$PWD/$SCRIPTDIR$PATH_INFO"
50
38 if test / = "$PATH_INFO" 51 if test / = "$PATH_INFO"
39 then PATH_INFO=/index run 52 then PATH_INFO=/index run
40 else 53
41 path="$PWD/$SCRIPTDIR$PATH_INFO" 54 elif ! test -d "$pag" && test -x "$src"
42 if ! test -d "$path" && test -x "$path" 55 then hexec "$src" 200
43 then hexec "$path" 200 56
44 else http_header 404 57 elif ! test -d "$pag" && test -r "$pag"
45 fi 58 then hcat 200 < "$pag"
59
60 elif test -d "$pag" && test -r "$pag/index.html"
61 then hcat 200 < "$pag/index.html"
62
63 else http_header 404
46 fi 64 fi
47} 65}
48 66