Compare commits
No commits in common. "edccec4ad07e2d8fe27b323bf7294b7ef02e59d7" and "8e596d4a21d3497dff04792444aa564102fe5579" have entirely different histories.
edccec4ad0
...
8e596d4a21
45
server.sh
45
server.sh
|
|
@ -6,23 +6,30 @@ 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)"
|
||||||
trap 'rm -r "$tmpdir"' EXIT
|
trap 'rm -r "$tmpdir"' EXIT
|
||||||
trap 'exit' INT
|
trap 'exit' INT
|
||||||
|
|
||||||
|
stdhead() {
|
||||||
|
code="$1"
|
||||||
|
head="$2"
|
||||||
|
echo "HTTP/1.0 $code $head"
|
||||||
|
echo "Content-Type: text/plain"
|
||||||
|
echo "Connection: close"
|
||||||
|
}
|
||||||
|
|
||||||
error() {
|
error() {
|
||||||
code="$1"
|
code="$1"
|
||||||
head="$2"
|
head="$2"
|
||||||
message="$3"
|
message="$3"
|
||||||
echo "HTTP/1.0 $code $head$CR"
|
echo "HTTP/1.0 $code $head"
|
||||||
echo "Content-Type: text/plain$CR"
|
echo "Content-Type: text/plain"
|
||||||
echo "Connection: close$CR"
|
echo "Connection: close"
|
||||||
echo "$CR"
|
echo
|
||||||
echo "$code $head$CR"
|
echo "$code $head"
|
||||||
echo "$CR"
|
echo
|
||||||
echo "$message"
|
echo "$message"
|
||||||
exit
|
exit
|
||||||
}
|
}
|
||||||
|
|
@ -32,7 +39,7 @@ handle() {
|
||||||
response_pipe="$tmpdir/response-$connection_id.pipe"
|
response_pipe="$tmpdir/response-$connection_id.pipe"
|
||||||
event_pipe="$tmpdir/event-$connection_id.pipe"
|
event_pipe="$tmpdir/event-$connection_id.pipe"
|
||||||
mkfifo -m 0600 "$response_pipe" "$event_pipe"
|
mkfifo -m 0600 "$response_pipe" "$event_pipe"
|
||||||
gnetcat --listen --source "$ADDR" --local-port "$PORT" --close < "$response_pipe" | sed -E -u 's/\r$//' | (
|
gnetcat -c -l "$ADDR" -p "$PORT" < "$response_pipe" | sed -E -u 's/\r$//' | (
|
||||||
read method url version || {
|
read method url version || {
|
||||||
echo "Connection broken" > "$event_pipe"
|
echo "Connection broken" > "$event_pipe"
|
||||||
exit
|
exit
|
||||||
|
|
@ -45,7 +52,6 @@ handle() {
|
||||||
if ! [ "$method" = "GET" ]; then
|
if ! [ "$method" = "GET" ]; then
|
||||||
error 501 'Not Implemented' "Method $method is not implemented.${LF}Only GET is supported."
|
error 501 'Not Implemented' "Method $method is not implemented.${LF}Only GET is supported."
|
||||||
fi
|
fi
|
||||||
url="$(echo "$url" | urlencode -d)"
|
|
||||||
if ! echo "$url" | grep -q '^/'; then
|
if ! echo "$url" | grep -q '^/'; then
|
||||||
error 400 'Bad Request' "URL must be host-relative"
|
error 400 'Bad Request' "URL must be host-relative"
|
||||||
fi
|
fi
|
||||||
|
|
@ -55,24 +61,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$CR"
|
echo "HTTP/1.0 200 OK"
|
||||||
echo "Connection: close$CR"
|
echo "Connection: close"
|
||||||
echo "$CR"
|
echo
|
||||||
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$CR"
|
echo "HTTP/1.0 307 Temporary Redirect"
|
||||||
echo "Connection: close$CR"
|
echo "Connection: close"
|
||||||
echo "Location: $url/$CR"
|
echo "Location: $url/"
|
||||||
exit
|
exit
|
||||||
fi
|
fi
|
||||||
ls "$target" 2>/dev/null | {
|
ls "$target" 2>/dev/null | {
|
||||||
echo "HTTP/1.0 200 OK$CR"
|
echo "HTTP/1.0 200 OK"
|
||||||
echo "Content-Type: text/html; charset=utf-8$CR"
|
echo "Content-Type: text/html; charset=utf-8"
|
||||||
echo "Connection: close$CR"
|
echo "Connection: close"
|
||||||
echo "$CR"
|
echo
|
||||||
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>"
|
||||||
|
|
@ -99,7 +105,6 @@ handle() {
|
||||||
rm "$event_pipe"
|
rm "$event_pipe"
|
||||||
}
|
}
|
||||||
|
|
||||||
echo "Listening at $ADDR:$PORT"
|
|
||||||
echo "Serving $ROOT"
|
echo "Serving $ROOT"
|
||||||
|
|
||||||
for((i=0;;i++)); do
|
for((i=0;;i++)); do
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user