Skip to content

Instantly share code, notes, and snippets.

@maple3142
Last active March 14, 2024 06:55
Show Gist options
  • Save maple3142/6f99a117c5554b5f4ff9e1c50235ff98 to your computer and use it in GitHub Desktop.
Save maple3142/6f99a117c5554b5f4ff9e1c50235ff98 to your computer and use it in GitHub Desktop.
one line webserver, tested in zsh and bash
while true; do mkfifo pipe; stdbuf -o0 cat pipe | nc -lv 7777 | ( read line; reqpath="$(echo "$line" | sed -nE 's/GET ([^ ]*) HTTP.*/\1/p')"; echoparam="$(echo "$reqpath" | cut -d '?' -f2 | awk 'BEGIN { RS="&"; FS="=" } { if ($1 == "echo") print $2 }')"; response=$echoparam; if [[ $reqpath == '/' ]]; then response='<form><input name="echo"><button>Echo</button></form>'; fi; printf 'HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n'; printf '%s' "$response" ) > pipe; rm pipe; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment