CRLF the protocol, as per the RFC
This commit is contained in:
parent
7d051690f5
commit
af70e91d0e
33
server.sh
33
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 "<!DOCTYPE html>"
|
||||
echo "<title>Directory listing for $url</title>"
|
||||
echo "<h1>$name</h1>"
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user