]> git.plutz.net Git - cgilite/blob - file.sh
Implemented pandoc grid tables
[cgilite] / file.sh
1 #!/bin/sh
2
3 # Copyright 2016 - 2019 Paul Hänsch
4 #
5 # This file is part of cgilite.
6
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.
11
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.
16
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/>. 
19
20 [ -n "$include_fileserve" ] && return 0
21 include_fileserve="$0"
22
23 file_type(){
24   case ${1##*.} in
25     css)       printf 'text/css';;
26     gif)       printf 'image/gif';;
27     html|html) printf 'text/html';;
28     jpg|jpeg)  printf 'image/jpeg';;
29     js)        printf 'text/javascript';;
30     m3u8)      printf 'application/x-mpegURL';;
31     m4a)       printf 'audio/mp4';;
32     m4s)       printf 'video/iso.segment';;
33     m4v|mp4)   printf 'video/mp4';;
34     mpd)       printf 'application/dash+xml';;
35     ogg)       printf 'audio/ogg';;
36     pdf)       printf 'application/pdf';;
37     png)       printf 'image/png';;
38     sh)        printf 'text/x-shellscript';;
39     svg)       printf 'image/svg+xml';;
40     tex)       printf 'text/x-tex';;
41     txt)       printf 'text/plain';;
42     short)     printf 'text/prs.shorthand';;
43     ts)        printf 'video/MP2T';;
44     webm)      printf 'video/webm';;
45     xml)       printf 'application/xml';;
46     *)         printf 'application/octet-stream';;
47   esac
48 }
49
50 FILE(){
51   local file file_size file_date http_date cachedate range mime
52   file="$1" mime="$2"
53
54   if ! [ -f "$file" ]; then
55     printf 'Content-Length: 0\r\nStatus: 404 Not Found\r\n\r\n'
56     exit 0
57   elif ! [ -r "$file" ]; then
58     printf 'Content-Length: 0\r\nStatus: 403 Forbidden\r\n\r\n'
59     exit 0
60   fi
61
62   file_size="$(stat -Lc %s "$file")"
63   file_date="$(stat -Lc %Y "$file")"
64   http_date="$(date -ud "@$file_date" +"%a, %d %b %Y %T GMT")"
65   cachedate="$(
66     # Parse the allowable date formats from Section 3.3.1 of
67     # https://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html
68     HEADER If-Modified-Since \
69     | sed -E 's;^[^ ]+, ([0-9]{2}) (...) ([0-9]{4}) (..:..:..) GMT$;\3-\2-\1 \4;;
70               s;^[^ ]+, ([0-9]{2})-(...)-([789][0-9]) (..:..:..) GMT$;19\3-\2-\1 \4;;
71               s;^[^ ]+, ([0-9]{2})-(...)-([0-6][0-9]) (..:..:..) GMT$;20\3-\2-\1 \4;;
72               s;^[^ ]+ (...) ([0-9]{2}) (..:..:..) ([0-9]{4})$;\4-\1-\2 \3;;
73               s;^[^ ]+ (...)  ([0-9]) (..:..:..) ([0-9]{4})$;\4-\1-\2 \3;;
74               s;Jan;01;; s;Feb;02;; s;Mar;03;; s;Apr;04;; s;May;05;; s;Jun;06;;
75               s;Jul;07;; s;Aug;08;; s;Sep;09;; s;Oct;10;; s;Nov;11;; s;Dec;12;;' \
76     | xargs -r0 date +%s -ud 2>&-
77   )"
78
79   range="$(HEADER Range |sed -nE 's;^bytes=([0-9]+-[0-9]*|-[0-9]+)$;\1;p;q;')"
80   case "$range" in
81     *-) range="${range}$((file_size - 1))";;
82     -*) [ ${range#-} -le $file_size ] \
83         && range="$((file_size - ${range#-}))-$((file_size - 1))" \
84         || range="0-$((file_size - 1))";;
85     *-*) [ ${range#*-} -ge $file_size ] \
86          && range="${range%-*}-$((file_size - 1))";;
87   esac
88
89   if [ "$file_date" -lt "$cachedate" ] 2>&-; then
90     printf '%s: %s\r\n' \
91       Status '304 Not Modified' \
92       Content-Length 0 \
93       Last-Modified "$http_date"
94     printf '\r\n'
95   
96   elif [ -z "$range" ]; then
97     printf '%s: %s\r\n' \
98       Status "200 OK" \
99       Accept-Ranges bytes \
100       Last-Modified "$http_date" \
101       Content-Type "${mime:-$(file_type "$file")}" \
102       Content-Length $file_size
103     printf '\r\n'
104   
105     [ "$REQUEST_METHOD" != HEAD ] && cat "$file"
106
107   elif [ "${range%-*}" -le "${range#*-}" ]; then
108     printf '%s: %s\r\n' \
109       Status "206 Partial Content" \
110       Accept-Ranges bytes \
111       Last-Modified "$http_date" \
112       Content-Type "${mime:-$(file_type "$file")}" \
113       Content-Range "bytes ${range}/${file_size}" \
114       Content-Length "$((${range#*-} - ${range%-*} + 1))"
115     printf '\r\n'
116   
117     [ "$REQUEST_METHOD" != HEAD ] \
118     && tail -c+$((${range%-*} + 1)) "$file" \
119      | head -c "$((${range#*-} - ${range%-*} + 1))"
120
121   elif [ "${range%-*}" -gt "${range#*-}" ]; then
122     printf '%s: %s\r\n' \
123       Status "216 Range Not Satisfiable" \
124       Content-Length 0 \
125       Content-Range \*/${file_size}
126     printf '\r\n'
127   fi
128 }