blob: ffc816a996b638c31c4ba2c3ed48ffe8c74b247f (
plain) (
blame)
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
|
#!/bin/sh
# Stupid basic http server using OpenBSD netcat
set -e
PORT="${PORT:-8900}"
ADDR="${ADDR:-127.0.0.1}"
ncpid=
send_index() {
cat <<EOF
HTTP/1.1 200 OK
Server: shell
Content-Type: text/html; charset=UTF-8
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Netcat HTTP</title>
</head>
<body>
Minimal HTTP server in posix SH and OpenBSD's netcat
</body>
</html>
EOF
}
cleanup() {
set +e
kill "$ncpid" >/dev/null 2>&1
rm "$_f" "$_inF" >/dev/null 2>&1
exit 0
}
trap cleanup EXIT INT
while true ; do
_f="$(mktemp)"
_inF="$(mktemp)"
rm -f "$_inF"
mkfifo "$_inF"
#shellcheck disable=SC2002
cat "$_inF" | nc -l "$ADDR" "$PORT" > "$_f" & # | tee /dev/fd/2 >"$_f" &
ncpid=$!
counter=0
while [ $counter -lt 100 ] || [ -s "$_f" ] ; do
if grep -qi "^Accept:" "$_f" ; then
pth="$(sed -nre 's@^GET ([^ ][^ ]*) HTTP/1.1.*@\1@gp' < "$_f")"
echo "Request for path: $pth"
case $pth in
/) send_index >"$_inF" ;;
esac
break
fi
sleep .01
[ -s "$_f" ] && counter=$((counter += 1))
ps $ncpid >/dev/null 2>&1 || break
done
kill $ncpid || echo ""
rm "$_f" "$_inF"
done
|