diff options
| author | Mitchell Riedstra <mitch@riedstra.dev> | 2021-10-29 21:53:53 -0400 |
|---|---|---|
| committer | Mitchell Riedstra <mitch@riedstra.dev> | 2021-10-29 21:53:53 -0400 |
| commit | 5b25c5155312f626813e0d36b7933f5eba801dd2 (patch) | |
| tree | 6e264a92dbe13ed77821a5c5bd536b6701768442 /scripts/httpsvr | |
| parent | e9b933ea6ce85f5d4f4653b7e5e6a7c836fcc893 (diff) | |
| download | dotfiles-5b25c5155312f626813e0d36b7933f5eba801dd2.tar.gz dotfiles-5b25c5155312f626813e0d36b7933f5eba801dd2.tar.xz | |
Major dotfile cleanup
Diffstat (limited to 'scripts/httpsvr')
| -rwxr-xr-x | scripts/httpsvr | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/scripts/httpsvr b/scripts/httpsvr new file mode 100755 index 0000000..ffc816a --- /dev/null +++ b/scripts/httpsvr @@ -0,0 +1,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 |
