#!/bin/bash -e
# see: https://github.com/izabera/single_serve/blob/develop/single_serve
[ -n "${TARGETFILE}" ] || TARGETFILE=${1?missing file} exec ncat -e "$0" -kl "${2?missing port}"
IFS=$' \r' read -t1 -rn100 request uri _; [ "${uri}" = / ]
# shellcheck disable=SC2312
length=$(wc -c < "${TARGETFILE}") type=$(file --mime-type --mime-encoding -r -- "${TARGETFILE}")
printf '%s\r\n' 'HTTP/1.1 200 OK' "${type/"${TARGETFILE}"/Content-Type}" "Content-Length: ${length}" ''
[ "${request}" = GET ] && cat -- "${TARGETFILE}"
