#!/bin/sh BASE_DIR="/var/nex" send_error() { code="$1" message="$2" printf "HTTP/1.0 %s %s\r\nContent-Type: text/plain\r\nConnection: close\r\n\r\n%s %s\n" "$code" "$message" "$code" "$message" exit 0 } IFS=' ' read -r method fullpath _ path="${fullpath%%[\?#]*}" path_noslash="${path#/}" CR=$(printf '\r') while IFS= read -r line; do [ "$line" = "" ] || [ "$line" = "$CR" ] && break done [ "$method" != "GET" ] && send_error 405 "Method Not Allowed" case "$path_noslash" in *..*) send_error 400 "Bad Request" ;; esac [ -z "$path_noslash" ] && path_noslash="index.html" FILE="$BASE_DIR/$path_noslash" case "$path" in */) FILE="${FILE%/}" ;; esac [ -f "$FILE" ] || send_error 404 "Not Found" case "$path_noslash" in favicon.ico) mime="image/x-icon" ;; index.html) mime="text/html" ;; *) mime="text/plain" ;; esac printf "HTTP/1.0 200 OK\r\nContent-Type: %s\r\nConnection: close\r\n\r\n" "$mime" cat "$FILE" exit 0