diff options
-rwxr-xr-x | cacophony.sh | 58 |
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 | ||
4 | SCRIPTDIR=source | 4 | SCRIPTDIR=source |
5 | 5 | ||
@@ -8,41 +8,59 @@ crlf() { # crlf < INPUT | |||
8 | sed 's/$/ /g' | 8 | sed 's/$/ /g' |
9 | } | 9 | } |
10 | 10 | ||
11 | http_header() { # header [STATUS] | 11 | log() { # log MESSAGE ... |
12 | printf >&2 '%s\n' "$*" | ||
13 | } | ||
14 | |||
15 | http_header() { # http_header [STATUS] [MIMETYPE] [CHARSET] | ||
12 | cat <<HEADER | crlf | 16 | cat <<HEADER | crlf |
13 | HTTP/1.1 ${1:-200} | 17 | HTTP/1.1 ${1:-200} |
14 | Content-Type: text/html;charset=utf-8 | 18 | Content-Type: ${2:-text/html};charset=${3:-utf-8} |
15 | 19 | ||
16 | HEADER | 20 | HEADER |
17 | } | 21 | } |
18 | 22 | ||
19 | html_footer() { # footer PROGRAM | 23 | html_footer() { # html_footer |
20 | cat <<EOF | 24 | cat <<FOOTER |
21 | <footer> | 25 | <footer>this page is a <a href="$SCRIPTDIR/${1##*/}">program</a> |
22 | copyright (C) $(stat -c %y "$1" | cut -d- -f1) Case Duckworth. | ||
23 | this page is a <a href="$SCRIPTDIR/${1##*/}">program</a> | ||
24 | served by <a href="https://git.acdw.net/cacophony">cacophony</a>. | 26 | served by <a href="https://git.acdw.net/cacophony">cacophony</a>. |
25 | </footer> | 27 | </footer></body></html> |
26 | </body></html> | 28 | FOOTER |
27 | EOF | ||
28 | } | 29 | } |
29 | 30 | ||
30 | hexec() { # hexec PROGRAM [header args] | 31 | hcat() { # 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 | |||
39 | hexec() { # 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 | ||
37 | run() { # run | 47 | run() { # 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 | ||