Skip to content

Instantly share code, notes, and snippets.

@felixSchl
Last active January 26, 2019 19:11
Show Gist options
  • Save felixSchl/ce6143c06d3a6ff360d6397c8f71349a to your computer and use it in GitHub Desktop.
Save felixSchl/ce6143c06d3a6ff360d6397c8f71349a to your computer and use it in GitHub Desktop.
Simple netcat HTTP server (without -c/-e)
#!/bin/bash
set -e
function run_handler {
: # whatever
}
while :; do
rm -f pipe
mkfifo pipe
cat pipe | nc -l 8888 | (
set +e
read
echo -n 'running handler... '
result=$(run_handler 2>&1)
if [[ "$?" -eq 0 ]]; then
echo 'OK'
{
echo 'HTTP/1.1 200 OK'
echo 'Connection: Close'
} &> pipe
else
echo 'FAIL'
echo "$result"
{
echo 'HTTP/1.1 500 Internal Server Error'
echo 'Connection: Close'
} &> pipe
fi
)
done
# vim: set noet:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment