]> git.plutz.net Git - cgilite/blob - static.sh
externalize stabndalone server mode into file
[cgilite] / static.sh
1 #!/bin/zsh
2
3 # Copyright 2016 Paul Hänsch
4 #
5 # This file is part of shcgi.
6
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.
11
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.
16
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/>. 
19
20
21 unset length date file suffix
22 file="$_EXEC/static/$STATIC"
23 date="$(stat -c %Y "$file")"
24
25 # allow overriding magic file recognition
26 # don't use this if `file` returns type correctly
27 declare -A suffix
28 suffix[css]="text/css"
29
30 if [ -x "$file" -o \! -r "$file" -o \! -f "$file" ]; then
31   printf 'HTTP/1.1 403 Forbidden\r\n\r\n'
32 elif [ "$date" = "$HTTP_IF_NONE_MATCH" ]; then
33   printf 'HTTP/1.1 304 Not Modified\r\n\r\n'
34 else
35   length="$(stat -c %s "$file")"
36   magic="${suffix[${file##*.}]:-$(file -bi "$file")}"
37
38   printf 'Etag: %s\r\n' "$date"
39   printf 'Content-Type: %s\r\n' "${magic:-all/all}"
40   printf 'Content-Length: %s\r\n' "$length"
41   printf '\r\n'
42
43   cat "$file"
44 fi
45