Created
February 13, 2020 16:41
-
-
Save dolegi/bb78191cdb72cd9461f3163e9bf6998a to your computer and use it in GitHub Desktop.
Static file server in bash with netcat
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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