summary refs log tree commit diff stats
path: root/yawp.sh
diff options
context:
space:
mode:
Diffstat (limited to 'yawp.sh')
-rwxr-xr-xyawp.sh71
1 files changed, 71 insertions, 0 deletions
diff --git a/yawp.sh b/yawp.sh new file mode 100755 index 0000000..b74febb --- /dev/null +++ b/yawp.sh
@@ -0,0 +1,71 @@
1#!/bin/sh
2# yawp!
3
4crlf() { # crlf < INPUT
5 ## Convert LF to CRLF
6 sed 's/$/ /g'
7}
8
9http_response() { # http_response [STATUS:-200 OK] < CONTENT
10 cat <<HEADER | crlf
11HTTP/1.1 ${1:-200 OK}
12Date: $(date -R)
13Content-Type: text/html; charset=utf-8
14HEADER
15 echo
16 cat
17}
18
19main() {
20 case "$REQUEST_URI" in
21 /) new_post ;;
22 *) display_posts ;;
23 esac
24}
25
26debug_env() {
27 http_response<<EOF
28<pre>$(env)</pre>
29$(cat | tee /dev/shm/yawp)
30<hr>
31<pre>
32$(form_decode < /dev/shm/yawp)
33</pre>
34EOF
35}
36
37form_decode() { # form_decode << INPUT (application/x-www-form-urlencoded)
38 # TODO: multipart/form-data ..?
39 awk '/%[0-9A-Fa-f][0-9A-Fa-f]/ {
40 while (s = match($0, "%[0-9A-Fa-f][0-9a-fA-F]") > 0) {
41 chr = "";
42 hexdigits = "0123456789abcdef";
43 value = 16 * (index(hexdigits,tolower(substr($0,RSTART+1,1))) - 1);
44 value += index(hexdigits,tolower(substr($0,RSTART+2,1))) - 1;
45 chr = sprintf("%c", value);
46 value = 0;
47 $0 = substr($0, 1, RSTART - 1) chr substr($0, RSTART + RLENGTH);
48 }
49}
50{gsub(/+/," ");gsub(/&/,"\n");print}
51'
52}
53
54new_post() {
55 http_response <<EOF
56<!DOCTYPE html>
57<title>YAWP!</title>
58<form method="POST" action="/newpost">
59<button type="submit" name="yawp" value="yawp">YAWP!</button>
60<hr>
61<textarea id="content" name="content" required rows="40" cols="60">
62</textarea>
63</form>
64EOF
65}
66
67display_posts() {
68 debug_env
69}
70
71main "$@"