Last active
March 14, 2024 06:55
-
-
Save maple3142/6f99a117c5554b5f4ff9e1c50235ff98 to your computer and use it in GitHub Desktop.
one line webserver, tested in zsh and bash
This file contains hidden or 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
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