3 # Copyright 2016 Paul Hänsch
5 # This file is part of shcgi.
7 # shcgi 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 # shcgi 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 shcgi. If not, see <http://www.gnu.org/licenses/>.
21 unset length date file suffix
23 date="$(stat -c %Y "$file")"
25 # allow overriding magic file recognition
26 # don't use this if `file` returns type correctly
28 suffix[css]="text/css"
30 [ -n "$HTTP_IF_MODIFIED_SINCE" ] && cachedate="$(date -d "$HTTP_IF_MODIFIED_SINCE" +%s)"
32 if [ -x "$file" -o \! -r "$file" -o \! -f "$file" ]; then
33 printf 'Status:403 Forbidden\r\n\r\n'
34 elif [ "$date" = "$cachedate" ]; then
35 debug "Delegating to client cache"
36 printf 'Status:304 Not Modified\r\n'
37 printf 'Last-Modified: %s\r\n\r\n' "$(date -Rd "@$date")"
39 length="$(stat -c %s "$file")"
40 magic="${suffix[${file##*.}]:-$(file -bi "$file")}"
42 printf 'Last-Modified: %s\r\n' "$(date -Rd "@$date")"
43 printf 'Content-Type: %s\r\n' "${magic:-all/all}"
44 printf 'Content-Length: %s\r\n' "$length"