summary refs log tree commit diff stats
path: root/yawp.sh
blob: f8198626eca7009e4ffb7bda2dd5514abe1ede3d (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
68
69
70
71
#!/bin/sh
# yawp!

crlf() { # crlf < INPUT
	## Convert LF to CRLF
	sed 's/$/
/g'
}

http_response() { # http_response [STATUS:-200 OK] < CONTENT
	cat <<HEADER | crlf
HTTP/1.1 ${1:-200 OK}
Date: $(date -R)
Content-Type: text/html; charset=utf-8
HEADER
	echo
	cat
}

main() {
	case "$REQUEST_URI" in
		/) new_post ;;
		*) display_posts ;;
	esac
}

debug_env() {
	http_response<<EOF
<pre>$(env)</pre>
$(cat | tee /dev/shm/yawp)
<hr>
<pre>
$(form_decode < /dev/shm/yawp)
</pre>
EOF
}

form_decode() { # form_decode << INPUT (application/x-www-form-urlencoded)
	# TODO: multipart/form-data ..?
	crlf | awk '/%[0-9A-Fa-f][0-9A-Fa-f]/ {
    while (s = match($0, "%[0-9A-Fa-f][0-9a-fA-F]") > 0) {
	chr = "";
	hexdigits = "0123456789abcdef";
	value = 16 * (index(hexdigits,tolower(substr($0,RSTART+1,1))) - 1);
	value += index(hexdigits,tolower(substr($0,RSTART+2,1))) - 1;
	chr = sprintf("%c", value);
	value = 0;
	$0 = substr($0, 1, RSTART - 1) chr substr($0, RSTART + RLENGTH);
    }
}
{gsub(/+/," ");gsub(/&/,"\n");print}
'
}

new_post() {
	http_response <<EOF
<!DOCTYPE html>
<title>YAWP!</title>
<form method="POST" action="/newpost">
<button type="submit" name="yawp" value="yawp">YAWP!</button>
<hr>
<textarea id="content" name="content" required rows="40" cols="60">
</textarea>
</form>
EOF
}

display_posts() {
	debug_env
}

main "$@"