]> git.plutz.net Git - cgilite/blob - file.sh
State Content-Length in error responses to allow connection reuse
[cgilite] / file.sh
1 #!/bin/zsh
2
3 # Copyright 2016 - 2018 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 file_type(){
21   case ${1##*.} in
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';;
36   esac
37 }
38
39 FILE(){
40   unset range file_size file_date http_date cachedate
41   file="$1"
42
43   if ! [ -f "$file" ]; then
44     printf 'Content-Length: 0\r\nStatus: 404 Not Found\r\n\r\n'
45     exit 0
46   elif ! [ -r "$file" ]; then
47     printf 'Content-Length: 0\r\nStatus: 403 Forbidden\r\n\r\n'
48     exit 0
49   fi
50
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"
55   cachedate="$(
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>&-
67   )"
68
69   range="$(HEADER Range |sed -nr 's;^bytes=([0-9]+-[0-9]*|-[0-9]+)$;\1;p;q;')"
70   case "$range" in
71     *-) range="${range}$((file_size - 1))";;
72     -*) [ ${range#-} -le $file_size ] \
73         && range="$((file_size - ${range#-}))-$((file_size - 1))" \
74         || range="0-$((file_size - 1))";;
75     *-*) [ ${range#*-} -ge $file_size ] \
76          && range="${range%-*}-$((file_size - 1))";;
77   esac
78
79   if [ "$file_date" -lt "$cachedate" ] 2>&-; then
80     printf '%s: %s\r\n' \
81       Status '304 Not Modified' \
82       Content-Length 0 \
83       Last-Modified "$http_date"
84     printf '\r\n'
85   
86   elif [ -z "$range" ]; then
87     printf '%s: %s\r\n' \
88       Status "200 OK" \
89       Accept-Ranges bytes \
90       Last-Modified "$http_date" \
91       Content-Type $(file_type "$file") \
92       Content-Length $file_size
93     printf '\r\n'
94   
95     [ "$REQUEST_METHOD" != HEAD ] && cat "$file"
96
97   elif [ "${range%-*}" -le "${range#*-}" ]; then
98     printf '%s: %s\r\n' \
99       Status "206 Partial Content" \
100       Accept-Ranges bytes \
101       Last-Modified "$http_date" \
102       Content-Type $(file_type "$file") \
103       Content-Range "bytes ${range}/${file_size}" \
104       Content-Length "$((${range#*-} - ${range%-*} + 1))"
105     printf '\r\n'
106   
107     [ "$REQUEST_METHOD" != HEAD ] \
108     && tail -c+$((${range%-*} + 1)) "$file" \
109        | head -c "$((${range#*-} - ${range%-*} + 1))"
110
111   elif [ "${range%-*}" -gt "${range#*-}" ]; then
112     printf '%s: %s\r\n' \
113       Status "216 Range Not Satisfiable" \
114       Content-Length 0 \
115       Content-Range \*/${file_size}
116     printf '\r\n'
117   fi
118 }