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/>.
20 [ -n "$include_fileserve" ] && return 0
21 include_fileserve="$0"
25 html|html) printf 'text/html';;
26 css) printf 'text/css';;
27 js) printf 'text/javascript';;
28 txt) printf 'text/plain';;
29 sh) printf 'text/shellscript';;
30 jpg|jpeg) printf 'image/jpeg';;
31 png) printf 'image/png';;
32 svg) printf 'image/svg+xml';;
33 gif) printf 'image/gif';;
34 webm) printf 'video/webm';;
35 mp4) printf 'video/mp4';;
36 ogg) printf 'audio/ogg';;
37 xml) printf 'application/xml';;
38 *) printf 'application/octet-stream';;
43 local file file_size file_date http_date cachedate range
46 if ! [ -f "$file" ]; then
47 printf 'Content-Length: 0\r\nStatus: 404 Not Found\r\n\r\n'
49 elif ! [ -r "$file" ]; then
50 printf 'Content-Length: 0\r\nStatus: 403 Forbidden\r\n\r\n'
54 file_size="$(stat -Lc %s "$file")"
55 file_date="$(stat -Lc %Y "$file")"
56 http_date="$(date -uRd @$file_date)"
57 http_date="${http_date%+0000}GMT"
59 # Parse the allowable date formats from Section 3.3.1 of
60 # https://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html
61 HEADER If-Modified-Since \
62 | sed -r 's;^[^ ]+, ([0-9]{2}) (...) ([0-9]{4}) (..:..:..) GMT$;\3-\2-\1 \4;;
63 s;^[^ ]+, ([0-9]{2})-(...)-([789][0-9]) (..:..:..) GMT$;19\3-\2-\1 \4;;
64 s;^[^ ]+, ([0-9]{2})-(...)-([0-6][0-9]) (..:..:..) GMT$;20\3-\2-\1 \4;;
65 s;^[^ ]+ (...) ([0-9]{2}) (..:..:..) ([0-9]{4})$;\4-\1-\2 \3;;
66 s;^[^ ]+ (...) ([0-9]) (..:..:..) ([0-9]{4})$;\4-\1-\2 \3;;
67 s;Jan;01;; s;Feb;02;; s;Mar;03;; s;Apr;04;; s;May;05;; s;Jun;06;;
68 s;Jul;07;; s;Aug;08;; s;Sep;09;; s;Oct;10;; s;Nov;11;; s;Dec;12;;' \
69 | xargs -r0 date +%s -ud 2>&-
72 range="$(HEADER Range |sed -nr 's;^bytes=([0-9]+-[0-9]*|-[0-9]+)$;\1;p;q;')"
74 *-) range="${range}$((file_size - 1))";;
75 -*) [ ${range#-} -le $file_size ] \
76 && range="$((file_size - ${range#-}))-$((file_size - 1))" \
77 || range="0-$((file_size - 1))";;
78 *-*) [ ${range#*-} -ge $file_size ] \
79 && range="${range%-*}-$((file_size - 1))";;
82 if [ "$file_date" -lt "$cachedate" ] 2>&-; then
84 Status '304 Not Modified' \
86 Last-Modified "$http_date"
89 elif [ -z "$range" ]; then
93 Last-Modified "$http_date" \
94 Content-Type $(file_type "$file") \
95 Content-Length $file_size
98 [ "$REQUEST_METHOD" != HEAD ] && cat "$file"
100 elif [ "${range%-*}" -le "${range#*-}" ]; then
101 printf '%s: %s\r\n' \
102 Status "206 Partial Content" \
103 Accept-Ranges bytes \
104 Last-Modified "$http_date" \
105 Content-Type $(file_type "$file") \
106 Content-Range "bytes ${range}/${file_size}" \
107 Content-Length "$((${range#*-} - ${range%-*} + 1))"
110 [ "$REQUEST_METHOD" != HEAD ] \
111 && tail -c+$((${range%-*} + 1)) "$file" \
112 | head -c "$((${range#*-} - ${range%-*} + 1))"
114 elif [ "${range%-*}" -gt "${range#*-}" ]; then
115 printf '%s: %s\r\n' \
116 Status "216 Range Not Satisfiable" \
118 Content-Range \*/${file_size}