X-Git-Url: http://git.plutz.net/?p=cgilite;a=blobdiff_plain;f=static.sh;h=bf109ed26959e708d6d675ba57d38e829da517da;hp=c86ed9f971b334a5f374f5feb9857d38765a0ab9;hb=1a1d111199b770c092e1b16a7e03568d3d911b79;hpb=456423f02f12ac71d2d14e1abf959dafdc5eb041 diff --git a/static.sh b/static.sh index c86ed9f..bf109ed 100755 --- a/static.sh +++ b/static.sh @@ -18,21 +18,48 @@ # along with shcgi. If not, see . -unset length date file -file="$_EXEC/static/$STATIC" -date="$(stat -c %Y "$file")" - -if [ -x "$file" -o \! -r "$file" -o \! -f "$file" ]; then - printf 'HTTP/1.1 403 Forbidden\n\n' -elif [ "$date" = "$HTTP_IF_NONE_MATCH" ]; then - printf 'HTTP/1.1 304 Not Modified\n\n' +unset length date file suffix +file="$1" +date="$(stat -Lc %Y "$file")" + +# allow overriding magic file recognition +# don't use this if `file` returns type correctly +declare -A suffix +suffix[css]="text/css" + +[ -n "$HTTP_IF_MODIFIED_SINCE" ] && cachedate="$(date -d "$HTTP_IF_MODIFIED_SINCE" +%s)" + +if printf '%s' "${HTTP_RANGE}" |grep -qE '^bytes=[0-9]+-[0-9]*\r?$'; then + _range="${HTTP_RANGE#bytes=}" + _bstart="${_range%-*}" + _bend="${_range#*-}" +fi + +if ! [ -f "$file" ] || [ -x "$file" ] || ! grep -qm1 '' "$file" ; then + printf 'Status:403 Forbidden\r\n\r\n' +elif [ "$date" = "$cachedate" ]; then + printf 'Status:304 Not Modified\r\n' + printf 'Last-Modified: %s\r\n\r\n' "$(date -Rd "@$date")" +elif [ "$_bstart" -gt 0 ] ; then + length="$(stat -Lc %s "$file")" + magic="${suffix[${file##*.}]:-$(file -bi "$file")}" + + printf 'Status:206 Partial Content\r\n' + printf 'Last-Modified: %s\r\n' "$(date -Rd "@$date")" + printf 'Content-Type: %s\r\n' "${magic:-all/all}" + printf 'Content-Range: bytes %i-%i/%i\r\n' $_bstart $(($length - 1)) $length |debug + printf 'Content-Length: %i\r\n' $(($length - $_bstart)) + printf '\r\n' + + tail -c+"$(($_bstart + 1))" "$file" else - length="$(stat -c %s "$file")" - magic="$(file -bi "$file")" + length="$(stat -Lc %s "$file")" + magic="${suffix[${file##*.}]:-$(file -bi "$file")}" - printf 'Etag: %s\r\n' "$date" + printf 'Accept-Ranges: bytes\r\n' + printf 'Last-Modified: %s\r\n' "$(date -Rd "@$date")" printf 'Content-Type: %s\r\n' "${magic:-all/all}" - printf 'Content-Length: %s\r\n' "$length" + printf 'Content-Length: %i\r\n' "$length" printf '\r\n' cat "$file"