diff --git a/server.sh b/server.sh index 958635c..c3c68a2 100755 --- a/server.sh +++ b/server.sh @@ -6,6 +6,7 @@ ADDR=127.0.0.1 PORT=1234 ROOT="$(realpath "${1:-$PWD}")" +CR=$'\r' LF=$'\n' tmpdir="$(mktemp -d)" @@ -16,12 +17,12 @@ error() { code="$1" head="$2" message="$3" - echo "HTTP/1.0 $code $head" - echo "Content-Type: text/plain" - echo "Connection: close" - echo - echo "$code $head" - echo + echo "HTTP/1.0 $code $head$CR" + echo "Content-Type: text/plain$CR" + echo "Connection: close$CR" + echo "$CR" + echo "$code $head$CR" + echo "$CR" echo "$message" exit } @@ -54,24 +55,24 @@ handle() { target="$ROOT$url" name=$(basename "$url") if [ -f "$target" ]; then - echo "HTTP/1.0 200 OK" - echo "Connection: close" - echo + echo "HTTP/1.0 200 OK$CR" + echo "Connection: close$CR" + echo "$CR" cat "$target" exit fi if [ -d "$target" ]; then if ! echo "$url" | grep -q '/$'; then - echo "HTTP/1.0 307 Temporary Redirect" - echo "Connection: close" - echo "Location: $url/" + echo "HTTP/1.0 307 Temporary Redirect$CR" + echo "Connection: close$CR" + echo "Location: $url/$CR" exit fi ls "$target" 2>/dev/null | { - echo "HTTP/1.0 200 OK" - echo "Content-Type: text/html; charset=utf-8" - echo "Connection: close" - echo + echo "HTTP/1.0 200 OK$CR" + echo "Content-Type: text/html; charset=utf-8$CR" + echo "Connection: close$CR" + echo "$CR" echo "" echo "