blob: 92a5d4bed495f18c80f9165ec6a7dae4fa0f3baa (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
#!/bin/sh
# cacophony by C. Duckworth --- hereby released to the public domain
SCRIPTDIR=source
crlf() { # crlf < INPUT
## Convert LF to CRLF
sed 's/$/
/g'
}
log() { # log MESSAGE ...
printf >&2 '%s\n' "$*"
}
http_header() { # http_header [STATUS] [MIMETYPE] [CHARSET]
cat <<HEADER | crlf
HTTP/1.1 ${1:-200}
Content-Type: ${2:-text/html};charset=${3:-utf-8}
HEADER
}
html_footer() { # html_footer
cat <<FOOTER
<footer>this page is a <a href="$SCRIPTDIR/${1##*/}">program</a>
served by <a href="https://git.acdw.net/cacophony">cacophony</a>.
</footer></body></html>
FOOTER
}
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
}
run() { # run
pag="$PWD$PATH_INFO"
src="$PWD/$SCRIPTDIR$PATH_INFO"
if test / = "$PATH_INFO"
then PATH_INFO=/index run
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
}
run
|