3 # Copyright 2016 - 2018 Paul Hänsch
5 # This file is part of cgilite.
7 # cgilite is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # cgilite is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with cgilite. If not, see <http://www.gnu.org/licenses/>.
22 html|html) printf 'text/html';;
23 css) printf 'text/css';;
24 js) printf 'text/javascript';;
25 txt) printf 'text/plain';;
26 sh) printf 'text/shellscript';;
27 jpg|jpeg) printf 'image/jpeg';;
28 png) printf 'image/png';;
29 svg) printf 'image/svg+xml';;
30 gif) printf 'image/gif';;
31 webm) printf 'video/webm';;
32 mp4) printf 'video/mp4';;
33 ogg) printf 'audio/ogg';;
34 xml) printf 'application/xml';;
35 *) printf 'application/octet-stream';;
40 unset range file_size file_date http_date cachedate
43 if ! [ -f "$file" ]; then
44 printf 'Status: 404 Not Found\r\n\r\n'
46 elif ! [ -r "$file" ]; then
47 printf 'Status: 403 Forbidden\r\n\r\n'
51 file_size="$(stat -Lc %s "$file")"
52 file_date="$(stat -Lc %Y "$file")"
53 http_date="$(date -uRd @$file_date)"
54 http_date="${http_date%+0000}GMT"
56 # Parse the allowable date formats from Section 3.3.1 of
57 # https://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html
58 HEADER If-Modified-Since \
59 | sed -r 's;^[^ ]+, ([0-9]{2}) (...) ([0-9]{4}) (..:..:..) GMT$;\3-\2-\1 \4;;
60 s;^[^ ]+, ([0-9]{2})-(...)-([789][0-9]) (..:..:..) GMT$;19\3-\2-\1 \4;;
61 s;^[^ ]+, ([0-9]{2})-(...)-([0-6][0-9]) (..:..:..) GMT$;20\3-\2-\1 \4;;
62 s;^[^ ]+ (...) ([0-9]{2}) (..:..:..) ([0-9]{4})$;\4-\1-\2 \3;;
63 s;^[^ ]+ (...) ([0-9]) (..:..:..) ([0-9]{4})$;\4-\1-\2 \3;;
64 s;Jan;01;; s;Feb;02;; s;Mar;03;; s;Apr;04;; s;May;05;; s;Jun;06;;
65 s;Jul;07;; s;Aug;08;; s;Sep;09;; s;Oct;10;; s;Nov;11;; s;Dec;12;;' \
66 | xargs -0 date +%s -ud 2>&-
69 range="$(HEADER Range |sed -nr 's;^bytes=([0-9]+-[0-9]*|-[0-9]+)$;\1;p;q;')"
71 *-) range="${range}$((file_size - 1))";;
72 -*) [ ${range#-} -le $file_size ] \
73 && range="$((file-size - ${rang#-}))-$((file_size - 1))" \
74 || range="0-$((file_size - 1))";;
75 *-*) [ ${range#*-} -ge $file_size ] \
76 && range="${range%-*}-$((file_size - 1))";;
79 if [ "$file_date" -lt "$cachedate" ] 2>&-; then
81 Status '304 Not Modified' \
82 Last-Modified "$http_date"
85 elif [ -z "$range" ]; then
89 Last-Modified "$http_date" \
90 Content-Type $(file_type "$file") \
91 Content-Length $file_size
94 [ "$REQUEST_METHOD" != HEAD ] && cat "$file"
96 elif [ "${range%-*}" -le "${range#*-}" ]; then
98 Status "206 Partial Content" \
100 Last-Modified "$http_date" \
101 Content-Type $(file_type "$file") \
102 Content-Range "bytes ${range}/${file_size}" \
103 Content-Length "$((${range#*-} - ${range%-*} + 1))"
106 [ "$REQUEST_METHOD" != HEAD ] \
107 && tail -c+$((${range%-*} + 1)) "$file" \
108 | head -c "$((${range#*-} - ${range%-*} + 1))"
110 elif [ "${range%-*}" -gt "${range#*-}" ]; then
111 printf '%s: %s\r\n' \
112 Status "216 Range Not Satisfiable" \
113 Content-Range \*/${file_size}