]> git.plutz.net Git - shcgi/commitdiff
rudimantary support for byte ranges
authorpaul <paul@plutz.net>
Sat, 26 Nov 2016 16:16:49 +0000 (16:16 +0000)
committerpaul <paul@plutz.net>
Sat, 26 Nov 2016 16:16:49 +0000 (16:16 +0000)
svn path=/trunk/; revision=45

static.sh

index ae8de6c3ed1dd1dad3b34f5af1dfd8c268feaec0..6e9ceb773788905a21f092db87d3691397ae42dd 100755 (executable)
--- a/static.sh
+++ b/static.sh
@@ -29,19 +29,37 @@ 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 [ -x "$file" -o \! -r "$file" -o \! -f "$file" ]; then
   printf 'Status:403 Forbidden\r\n\r\n'
 elif [ "$date" = "$cachedate" ]; then
-  debug "Delegating to client cache"
   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 -c %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="${suffix[${file##*.}]:-$(file -bi "$file")}"
 
+  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"