]> git.plutz.net Git - cgilite/blobdiff - static.sh
support static file serving; support null-string default in validate functions
[cgilite] / static.sh
diff --git a/static.sh b/static.sh
new file mode 100755 (executable)
index 0000000..c86ed9f
--- /dev/null
+++ b/static.sh
@@ -0,0 +1,40 @@
+#!/bin/zsh
+
+# Copyright 2016 Paul Hänsch
+#
+# This file is part of shcgi.
+# 
+# shcgi is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+# 
+# shcgi is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+# 
+# You should have received a copy of the GNU Affero General Public License
+# along with shcgi.  If not, see <http://www.gnu.org/licenses/>. 
+
+
+unset length date file
+file="$_EXEC/static/$STATIC"
+date="$(stat -c %Y "$file")"
+
+if [ -x "$file" -o \! -r "$file" -o \! -f "$file" ]; then
+  printf 'HTTP/1.1 403 Forbidden\n\n'
+elif [ "$date" = "$HTTP_IF_NONE_MATCH" ]; then
+  printf 'HTTP/1.1 304 Not Modified\n\n'
+else
+  length="$(stat -c %s "$file")"
+  magic="$(file -bi "$file")"
+
+  printf 'Etag: %s\r\n' "$date"
+  printf 'Content-Type: %s\r\n' "${magic:-all/all}"
+  printf 'Content-Length: %s\r\n' "$length"
+  printf '\r\n'
+
+  cat "$file"
+fi
+