Skip to content

Instantly share code, notes, and snippets.

@dolegi
Created February 13, 2020 16:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dolegi/bb78191cdb72cd9461f3163e9bf6998a to your computer and use it in GitHub Desktop.
Save dolegi/bb78191cdb72cd9461f3163e9bf6998a to your computer and use it in GitHub Desktop.
Static file server in bash with netcat
#!/bin/bash
RESP=/tmp/server-response && rm -rf $RESP && mkfifo $RESP
while true; do
cat $RESP | nc -l 9090 | (
REQ=$(while read -r line && [[ $line != Host* ]]; do echo "$line" ; done)
url="${REQ% HTTP/*}"
filedata=$(<"$(pwd)${url#* }")
cat >$RESP <<EOF
HTTP/1.1 $([ -z "${#filedata}" ] && echo '200 OK' || echo '404 NOT FOUND')
Content-Length: ${#filedata}
${filedata}
EOF
)
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment