CRLF the protocol, as per the RFC

This commit is contained in:
numzero 2026-05-03 00:20:14 +03:00
parent 7d051690f5
commit af70e91d0e

View File

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