diff options
author | dan <[email protected]> | 2023-05-28 16:33:10 -0400 |
---|---|---|
committer | dan <[email protected]> | 2023-05-28 16:33:10 -0400 |
commit | 1d1300ef90b6b6bf70d18a81d1050a7ac0e89ea8 (patch) | |
tree | 277a45839772a6dd5593d4fff718fcc8c6b6e62b | |
parent | 354616cc3536d7f0c58795373192f6b1e5a1cb0b (diff) | |
download | dump-1d1300ef90b6b6bf70d18a81d1050a7ac0e89ea8.tar.gz dump-1d1300ef90b6b6bf70d18a81d1050a7ac0e89ea8.tar.bz2 dump-1d1300ef90b6b6bf70d18a81d1050a7ac0e89ea8.zip |
refactor: sh http server simplify and cleanup
-rw-r--r-- | http-server.sh | 18 |
1 files changed, 8 insertions, 10 deletions
diff --git a/http-server.sh b/http-server.sh index aa5e17e..3e47a4e 100644 --- a/http-server.sh +++ b/http-server.sh @@ -5,8 +5,8 @@ # Sends a greeting message and the date in the response. -# Info: Relies on the OpenBSD version of Netcat -# GNU netcat may or may not work. +# Warning: Relies on the OpenBSD version of Netcat. +# GNU netcat may or may not work. # Creating a FIFO so that the output of nc can be processed and used to determine the input. # Allows for "passing values back round" @@ -16,25 +16,23 @@ mkfifo "$outfifo" # Run forever while true; do - x="$(date)" - len="$(echo "$x" | wc -c)" - cat "$outfifo" \ - | nc -l localhost 1500 \ - | while read -r line; do + cat "$outfifo" | \ + nc -l localhost 1500 | \ + while read -r line; do line=$(echo "$line" | tr -d '\r\n') echo "< $line" if echo "$line" | grep -qE '^GET /'; then # on reading the request line, extract a value from the request path and build the response - name="$(echo $line | sed 's:GET /\([^ ]*\).*:\1:')" + name="$(echo "$line" | sed 's:GET /\([^ ]*\).*:\1:')" content="Hello $name, the date and time is $(date)." # Without Content-Length header, connection will not close properly - len="$(echo "$content" | wc -c)" + len="${#content}" response="HTTP/1.1 200 OK\nContent-Length: $len\nContent-Type: plain/text\n\n$content" elif [ -z "$line" ]; then # end of request # send response once all of the request has been read - echo -e "$response" > "$outfifo" + printf "%b" "$response" > "$outfifo" fi done done |